中介者模式定义如下:
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框架已经非常流行,这也是中中介者模式的优点的一个体现
中介服务
现在中介服务十分的多,比如租房中介,出国中介,这些都是中介模式的具体体现。比如,如果你要租房子,如果没有房屋中介,你就必须一个一个小区去找,还是和房东签约,检查家具,水电煤等等,如果有了中介,那你只需要知会中介你的要求,看完房子就直接签约,而找房子,与房东谈价格,检查家具,水电煤,网络电视等等,可以全部由中介帮你处理了,是不是十分的方便。
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() -------------------------------------
抽象类—飞机 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个对象直接的相互依赖关系转换为中介者和同事类的依赖关系,同事类越多,中介者的逻辑就越复杂。
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