23种设计模式之中介者模式

news/2024/7/7 18:38:50

这里写图片描述
中介者模式本质

  • 解耦多个部门对象之间的交互关系。每个对象都持有中介者对象的引用,只跟中介者对象打交道。我们通过中介者对象统一管理这些交互关系

android最常见的场景

  • MVC模式(其中的C),控制器就是一个中介者对象。M和V都和它打交道

总经理接口:总经理有两件事情:注册每个部门,向部门发送命令

//总经理接口
public interface Mediator {
    void register(String dname,Department d);
    void command(String dname);
}

部门接口:每个部门也只干两件事情,做自己本职工作和向总经理发送请求

public interface Department {
    void selfAction();//本职工作
    void outAction();//向总经理发送请求
}

科研部门

public class Development implements Department {
    Mediator mediator;

    public Development(Mediator mediator) {
        this.mediator = mediator;
        mediator.register("development",this);
    }

    @Override
    public void selfAction() {
        System.out.println("专心科研,开发好资源!");
    }

    @Override
    public void outAction() {

        System.out.println("汇报工作!没钱了,需要资金支持!");
        mediator.command("finacial");
    }
}

市场部部门

public class Market implements Department {
    Mediator mediator;//总经理,中间者

    public Market(Mediator mediator) {
        this.mediator = mediator;
        mediator.register("market",this);//注册当前
    }

    @Override
    public void selfAction() {

        System.out.println("市场部跑业务,拉妹子去了");
    }

    @Override
    public void outAction() {
        System.out.println("报告老板!市场部没钱了,请求发工资");
        mediator.command("finacial");//向财务部发命令
    }
}

财务部部门

public class Finacial implements Department {
    Mediator mediator;

    public Finacial(Mediator mediator) {
        this.mediator = mediator;
        mediator.register("finacial", this);
    }

    @Override
    public void selfAction() {

        System.out.println("我是财务部,马上发钱!");
    }

    @Override
    public void outAction() {
        System.out.println("汇报工作!没钱了,赶紧找人跑业务啊");
        mediator.command("market");
    }
}

总经理实现

public class President implements Mediator {
    private Map<String,Department> map=new HashMap<>();
    @Override
    public void register(String dname, Department d) {
        map.put(dname,d);
    }

    @Override
    public void command(String dname) {
       map.get(dname).selfAction();
    }
}

测试

       Mediator mediator = new President();//总经理
        Market market = new Market(mediator);//市场部
        Development development = new Development(mediator);//科研部
        Finacial finacial = new Finacial(mediator);//财务部

        development.selfAction();//
        development.outAction();


        finacial.outAction();
        market.outAction();

http://www.niftyadmin.cn/n/3648933.html

相关文章

gatsby_使用gatsby-awesome-pagination在Gatsby中进行分页

gatsbyDespite Gatsby’s amazing performance, it would be best not to force the user to load every single post onto the same page. Instead, we’ll explore the gatsby-awesome-pagination plugin to segment our post archive into more manageable sections. 尽管Ga…

[收藏]伟大架构师的秘密

伟大架构师的秘密发布日期&#xff1a; 10/13/2004| 更新日期&#xff1a; 10/13/2004By Don Awalt and Rick McUmberRDA Corporationhttp://www.microsoft.com/china/msdn/library/architecture/architecture/architecturetopic/USdnmajgreatarchitect.mspx摘要&#xff1a;所…

23种设计模式之命令模式

定义:将一个请求封装为一个对象&#xff0c;从而使我们可用不同的请求对客户进行参数化&#xff1b;对请求排队或者记录请求日志&#xff0c;以及支持可以撤销的操作&#xff0c;也称之为&#xff1a;动作Action模式&#xff0c;事务transaction模式。 结构&#xff1a; Comm…

Genymotion常见问题整合与解决方案

为什么说是常见问题整合呢&#xff0c;因为我就是Genymotion最悲剧的使用者&#xff0c;该见过的问题&#xff0c;我基本都见过了&#xff0c;在此总结出这血的教训&#xff0c;望大家不要重蹈覆辙。常见问题1&#xff1a;Genymotion在开启模拟器时卡在了starting virtual devi…

day.js使用_使用Day.js解析,验证,处理和显示JavaScript中的日期和时间

day.js使用With it’s last release nearly a year ago, the most recent commit over 6 months ago, and hundreds of open bugs and pull requests, it’s starting to seem like Moment.js is slowing down and it’s time to shop for more actively maintained alternativ…

23种设计模式之解释器模式

介绍 是一种不常用的设计模式用于描述如何构成一个简单的语言解释器&#xff0c;主要用于使用面向对象语言开发的编译器和解释器设计。当我们需要开发一种新的语言时&#xff0c;可以考虑使用解释器模式。 开发中常见的场景 EL表达式式的处理正则表达式解释器。SQL语法的计数…

字符串indexof方法_探索JavaScript中的字符串和数组的indexOf方法

字符串indexof方法When you need to find an element in an array or a string, indexOf() is one of your best friends. 当您需要在数组或字符串中查找元素时&#xff0c; indexOf()是您最好的朋友之一。 数组中的indexOf (indexOf in Arrays) Code first, explanation la…

Android(Lollipop/5.0) Material Design(八) 保持兼容性

Define Alternative Styles 定义替代样式 让你的app&#xff0c;使用Material Design的主题运行在支持它的设备上&#xff0c;并在早期版本的设备上可以运行较早的主题&#xff1a;1. 在res/values/styles.xml 定义一个主题继承较早的主题2. 在res/values-v21/styles.xml 定义…