设计模式之中介者模式---Mediator Pattern

模式的定义

中介者模式定义如下:

Define an object that encapsulates how a set of objects interact.Mediator promotes loose coupling by keeping objects from referring to each other explicitly,and it lets you vary their interaction independently.

用一个中介对象封装一系列的对象交互,中介者使各对象不需要显示地相互作用,从而使其耦合松散,而且可以独立的改变它们之间的交互。

类型

行为类

模式的使用场景

中介者模式适合用于多个对象之间紧密耦合的情况,紧密耦合的标准是:在类图中出现了蜘蛛网状结构。这有利于把蜘蛛网状结构梳理为星型结构,使原本复杂混乱的关系变得清晰简单。

实际使用

中介者模式也叫调停者模式,一个对象要与N个对象交流,就非常混乱。如果加入一个调度中心,所有的类都和中心交流,中心说怎么处理就怎么处理,这是不是非常方便。

  • 机场调度中心

    这是一个具体的中介者

  • MVC框架

    MVC框架中,C(Controller)就是一个中介者,它的作用就是把M(Model,业务逻辑)和V(View,视图)隔离开,协同M和V工作,M运行结果和V代表的视图交互,减少M和V的依赖关系。MVC框架已经非常流行,这也是中中介者模式的优点的一个体现

  • 中介服务

    现在中介服务十分的多,比如租房中介,出国中介,这些都是中介模式的具体体现。比如,如果你要租房子,如果没有房屋中介,你就必须一个一个小区去找,还是和房东签约,检查家具,水电煤等等,如果有了中介,那你只需要知会中介你的要求,看完房子就直接签约,而找房子,与房东谈价格,检查家具,水电煤,网络电视等等,可以全部由中介帮你处理了,是不是十分的方便。

UML类图

这里写图片描述

角色介绍

Mediator–抽象中介者角色
抽象中介者角色定义统一的接口,用于各同事角色之间的通信。

Concrete Mediator–具体中介者角色
具体中介者角色,通过协调各同事角色实现协作行为,因此它必须依赖于各个同事角色。

Colleague–同事角色
每一个同事角色都知道中介者角色,而且与其它的同事角色通信的时候,一定要通过中介者角色来协作。
每个同事类的行为分为二种:一种是同事本身的行为,这种行为叫做自发行为(Self-Method);第二种是必须依赖中介者才能完成的行为,叫做依赖行为(Dep-Method)。

模式的通用源码

Mediator类:


public abstract class Mediator {

    protected ConcreteColleague1 c1;
    protected ConcreteColleague2 c2;

    public ConcreteColleague1 getC1() {
        return c1;
    }
    public void setC1(ConcreteColleague1 c1) {
        this.c1 = c1;
    }
    public ConcreteColleague2 getC2() {
        return c2;
    }
    public void setC2(ConcreteColleague2 c2) {
        this.c2 = c2;
    }

    public abstract void doSomething1();
    public abstract void doSomething2();
}

ConcreteMediator 类:


public class ConcreteMediator extends Mediator {

    @Override
    public void doSomething1() {
        // TODO Auto-generated method stub
        System.out.println("ConcreteMediator----doSomething1()");
        super.c1.doSelfMethod();
        super.c2.doSelfMethod();
    }

    @Override
    public void doSomething2() {
        // TODO Auto-generated method stub
        System.out.println("ConcreteMediator----doSomething2()");
        super.c1.doSelfMethod();
        super.c2.doSelfMethod();
    }
}

Colleague类:


public abstract class Colleague {

    protected Mediator mediator;

    public Colleague(Mediator _mediator){
        this.mediator = _mediator;
    }

    public abstract void doSelfMethod();
    public abstract void doDepMethod();
}

具体同事类:

ConcreteColleague1:


public class ConcreteColleague1 extends Colleague {

    public ConcreteColleague1(Mediator _mediator) {
        super(_mediator);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void doSelfMethod() {
        // TODO Auto-generated method stub
        System.out.println("ConcreteColleague1----doSelfMethod()");
    }

    @Override
    public void doDepMethod() {
        // TODO Auto-generated method stub
        System.out.println("ConcreteColleague1----doDepMethod()");
        super.mediator.doSomething1();
        //super.mediator.doSomething2();
    }
}

ConcreteColleague2:


public class ConcreteColleague2 extends Colleague {

    public ConcreteColleague2(Mediator _mediator) {
        super(_mediator);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void doSelfMethod() {
        // TODO Auto-generated method stub
        System.out.println("ConcreteColleague2----doSelfMethod()");
    }

    @Override
    public void doDepMethod() {
        // TODO Auto-generated method stub
        System.out.println("ConcreteColleague2----doDepMethod()");
        //super.mediator.doSomething1();
        super.mediator.doSomething2();
    }
}

Client类:

public class Client {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Mediator mediator = new ConcreteMediator();     
        ConcreteColleague1 colleague1 = new ConcreteColleague1(mediator);
        ConcreteColleague2 colleague2 = new ConcreteColleague2(mediator);

        mediator.setC1(colleague1); 
        mediator.setC2(colleague2);

        System.out.println("-------------------------------------");    
        colleague1.doSelfMethod();
        System.out.println("-------------------------------------");
        colleague1.doDepMethod();
        System.out.println("-------------------------------------");
        colleague2.doSelfMethod();
        System.out.println("-------------------------------------");
        colleague2.doDepMethod();
        System.out.println("-------------------------------------");

    }

}

输出结果:

-------------------------------------
ConcreteColleague1----doSelfMethod()
-------------------------------------
ConcreteColleague1----doDepMethod()
ConcreteMediator----doSomething1()
ConcreteColleague1----doSelfMethod()
ConcreteColleague2----doSelfMethod()
-------------------------------------
ConcreteColleague2----doSelfMethod()
-------------------------------------
ConcreteColleague2----doDepMethod()
ConcreteMediator----doSomething2()
ConcreteColleague1----doSelfMethod()
ConcreteColleague2----doSelfMethod()
-------------------------------------

中介者模式—–飞机场调度飞机

UML图

这里写图片描述

具体例子

抽象类—飞机 Fly:


public abstract class Fly {
    //定义飞机是进站还是出站
    public static String typeIn = "IN";
    public static String typeOUT = "OUT";

    protected String name;
    protected int id;
    //定义中介者飞机场调度中心
    protected AirportMediator airportMediator;

    public Fly(String name, int id) {
        super();
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public AirportMediator getAirportMediator() {
        return airportMediator;
    }

    public void setAirportMediator(AirportMediator airportMediator) {
        this.airportMediator = airportMediator;
    }
    //定义飞机执行自己的操作
    public abstract void doSelfMethod(String type);
    //定义飞机执行与机场调度中心的操作
    public abstract void doDepMethod(String type);
    //定义飞机进站操作
    public abstract void in();
    //定义飞机出站操作
    public abstract void out();
    //定义飞机监听机场调度中心的通知
    public abstract void listerMediatorNotification(String notification);

}

具体飞机类—ConcreteFly :

public class ConcreteFly extends Fly {

    public ConcreteFly(String name, int id) {
        super(name, id);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void doSelfMethod(String type) {
        // TODO Auto-generated method stub
        System.out.println("ConcreteFly---doSelfMethod");
        System.out.println("Fly:"+ super.name +"---"+type.toLowerCase());       
    }

    @Override
    public void doDepMethod(String type) {
        // TODO Auto-generated method stub
        System.out.println("ConcreteFly---doDepMethod");
        System.out.println("Fly:"+ super.name +"-------doDepMethod()");
        super.airportMediator.doManager(this,type);

    }

    @Override
    public void in() {
        // TODO Auto-generated method stub
        System.out.println("ConcreteFly---in()---Fly:"+ super.name +"-------in");
        doSelfMethod(typeIn);
        doDepMethod(typeIn);
    }

    @Override
    public void out() {
        // TODO Auto-generated method stub
        System.out.println("ConcreteFly---out()---Fly:"+ super.name +"-------out");
        doSelfMethod(typeOUT);
        doDepMethod(typeOUT);
    }


    @Override
    public void listerMediatorNotification(String notification) {
        // TODO Auto-generated method stub
        System.out.println("ConcreteFly----listerMediatorNotification");
        System.out.println("Fly:"+super.name + "--lister Mediator Notification:"+notification);
    }

}

中介者角色—AirportMediator :

import java.util.ArrayList;


public class AirportMediator {
    //本机场所有的飞机
    private ArrayList<Fly> array = new ArrayList<Fly>();

    public void add(ConcreteFly fly){
        array.add(fly);
    }

    public void remove(ConcreteFly fly){
        array.remove(fly);
    }
    //执行调度命令
    public void doManager(Fly fly,String type){
        System.out.println("AirportMediator---doManager");

        if(type.equals(Fly.typeIn)){
            if(!array.contains(fly)){
                array.add(fly);
            }
        }else if (type.equals(Fly.typeOUT)) {
            if(array.contains(fly)){
                array.remove(fly);
            }
        }

        for(int i=0;i<array.size();i++){            
            ((ConcreteFly)array.get(i)).listerMediatorNotification("Mediator Notifycation:Fly:"+fly.getName()
                    +"---"+type.toLowerCase()+"---各个飞机按命令调度");
        }

    }

}

Client:


public class Client {


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        AirportMediator airportMediator = new AirportMediator();

        ConcreteFly f1 = new ConcreteFly("0001", 1);
        ConcreteFly f2 = new ConcreteFly("0002", 2);
        ConcreteFly f3 = new ConcreteFly("0003", 3);

        f1.setAirportMediator(airportMediator);
        f2.setAirportMediator(airportMediator);
        f3.setAirportMediator(airportMediator);

        f1.in();
        System.out.println("---------------------------------------");
        f2.in();
        System.out.println("---------------------------------------");
        f3.in();
        System.out.println("---------------------------------------");
        f1.out();
        System.out.println("---------------------------------------");
        f2.out();
        System.out.println("---------------------------------------");
        f3.out();
        System.out.println("---------------------------------------");  
    }
}

输出结果:

ConcreteFly---in()---Fly:0001-------in
ConcreteFly---doSelfMethod
Fly:0001---in
ConcreteFly---doDepMethod
Fly:0001-------doDepMethod()
AirportMediator---doManager
ConcreteFly----listerMediatorNotification
Fly:0001--lister Mediator Notification:Mediator Notifycation:Fly:0001---in---各个飞机按命令调度
---------------------------------------
ConcreteFly---in()---Fly:0002-------in
ConcreteFly---doSelfMethod
Fly:0002---in
ConcreteFly---doDepMethod
Fly:0002-------doDepMethod()
AirportMediator---doManager
ConcreteFly----listerMediatorNotification
Fly:0001--lister Mediator Notification:Mediator Notifycation:Fly:0002---in---各个飞机按命令调度
ConcreteFly----listerMediatorNotification
Fly:0002--lister Mediator Notification:Mediator Notifycation:Fly:0002---in---各个飞机按命令调度
---------------------------------------
ConcreteFly---in()---Fly:0003-------in
ConcreteFly---doSelfMethod
Fly:0003---in
ConcreteFly---doDepMethod
Fly:0003-------doDepMethod()
AirportMediator---doManager
ConcreteFly----listerMediatorNotification
Fly:0001--lister Mediator Notification:Mediator Notifycation:Fly:0003---in---各个飞机按命令调度
ConcreteFly----listerMediatorNotification
Fly:0002--lister Mediator Notification:Mediator Notifycation:Fly:0003---in---各个飞机按命令调度
ConcreteFly----listerMediatorNotification
Fly:0003--lister Mediator Notification:Mediator Notifycation:Fly:0003---in---各个飞机按命令调度
---------------------------------------
ConcreteFly---out()---Fly:0001-------out
ConcreteFly---doSelfMethod
Fly:0001---out
ConcreteFly---doDepMethod
Fly:0001-------doDepMethod()
AirportMediator---doManager
ConcreteFly----listerMediatorNotification
Fly:0002--lister Mediator Notification:Mediator Notifycation:Fly:0001---out---各个飞机按命令调度
ConcreteFly----listerMediatorNotification
Fly:0003--lister Mediator Notification:Mediator Notifycation:Fly:0001---out---各个飞机按命令调度
---------------------------------------
ConcreteFly---out()---Fly:0002-------out
ConcreteFly---doSelfMethod
Fly:0002---out
ConcreteFly---doDepMethod
Fly:0002-------doDepMethod()
AirportMediator---doManager
ConcreteFly----listerMediatorNotification
Fly:0003--lister Mediator Notification:Mediator Notifycation:Fly:0002---out---各个飞机按命令调度
---------------------------------------
ConcreteFly---out()---Fly:0003-------out
ConcreteFly---doSelfMethod
Fly:0003---out
ConcreteFly---doDepMethod
Fly:0003-------doDepMethod()
AirportMediator---doManager
---------------------------------------

优点

中介者模式的优点就是减少类之间的依赖,把原有的一对多的依赖变成了一对一的依赖,同事类只依赖中介者,减少了类的依赖,当然也降低了类间的耦合。

缺点

中介者会膨胀得很大,而且逻辑复杂,原本N个对象直接的相互依赖关系转换为中介者和同事类的依赖关系,同事类越多,中介者的逻辑就越复杂。

Android源码中的模式实现

android Mediator样例:—KeyguardViewMediator

uml图:
这里写图片描述

关键代码:

同事角色:
KeyguardService:

./base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardService.java
public class KeyguardService extends Service {
    //中介者角色
    private KeyguardViewMediator mKeyguardViewMediator;

    @Override
    public void onCreate() {
        ((SystemUIApplication) getApplication()).startServicesIfNeeded();
        //中介者角色初始化
        mKeyguardViewMediator =
                ((SystemUIApplication) getApplication()).getComponent(KeyguardViewMediator.class);
    }

    private final IKeyguardService.Stub mBinder = new IKeyguardService.Stub() {

        @Override // Binder interface
        public void addStateMonitorCallback(IKeyguardStateCallback callback) {
            checkPermission();
            //调用中介者角色
            mKeyguardViewMediator.addStateMonitorCallback(callback);
        }

        @Override // Binder interface
        public void verifyUnlock(IKeyguardExitCallback callback) {
            checkPermission();
            //调用中介者角色
            mKeyguardViewMediator.verifyUnlock(callback);
        }
        ......
        @Override
        public void onActivityDrawn() {
            checkPermission();
            //调用中介者角色
            mKeyguardViewMediator.onActivityDrawn();
        }
    };

}

同事角色:
FingerprintUnlockController:

./base/packages/SystemUI/src/com/android/systemui/statusbar/phone/FingerprintUnlockController.java
public class FingerprintUnlockController extends KeyguardUpdateMonitorCallback {
  //中介者角色
  private KeyguardViewMediator mKeyguardViewMediator;
    @Override
    public void onFingerprintAuthenticated(int userId) {
        ......       
        switch (mMode) {
            ...... 
            case MODE_WAKE_AND_UNLOCK:
                ...... 
                //调用角色者模式
                mKeyguardViewMediator.onWakeAndUnlocking();
                ...... 
    }
}

中介者:KeyguardViewMediator

framework/base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java

KeyguardViewMediator类充当了一个中介者角色,以对各个角色的通信进行协调。

参考资料

(1).设计模式之禅—第14章 中介者模式
(2)中介者模式
https://github.com/simple-android-framework/android_design_patterns_analysis/tree/master/mediator

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Java设计模式是一组经过实践验证的面向对象设计原则和模式,可以帮助开发人员解决常见的软件设计问题。下面是常见的23种设计模式: 1. 创建型模式(Creational Patterns): - 工厂方法模式(Factory Method Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 单例模式(Singleton Pattern) - 原型模式(Prototype Pattern) - 建造者模式(Builder Pattern) 2. 结构型模式(Structural Patterns): - 适配器模式(Adapter Pattern) - 桥接模式(Bridge Pattern) - 组合模式(Composite Pattern) - 装饰器模式(Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式(Flyweight Pattern) - 代理模式(Proxy Pattern) 3. 行为型模式(Behavioral Patterns): - 责任链模式(Chain of Responsibility Pattern) - 命令模式(Command Pattern) - 解释器模式(Interpreter Pattern) - 迭代器模式(Iterator Pattern) - 中介者模式(Mediator Pattern) - 备忘录模式(Memento Pattern) - 观察者模式(Observer Pattern) - 状态模式(State Pattern) - 策略模式(Strategy Pattern) - 模板方法模式(Template Method Pattern) - 访问者模式(Visitor Pattern) 4. 并发型模式(Concurrency Patterns): - 保护性暂停模式(Guarded Suspension Pattern) - 生产者-消费者模式(Producer-Consumer Pattern) - 读写锁模式(Read-Write Lock Pattern) - 信号量模式(Semaphore Pattern) - 线程池模式(Thread Pool Pattern) 这些设计模式可以根据问题的特点和需求来选择使用,它们提供了一些可复用的解决方案,有助于开发高质量、可维护且易于扩展的软件系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hfreeman2008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值