1700462773
super.mediator.doSomething1();
1700462774
1700462775
}
1700462776
1700462777
}
1700462778
1700462779
public class ConcreteColleague2 extends Colleague{
1700462780
1700462781
//通过构造函数传递中介者
1700462782
1700462783
public ConcreteColleague2(Mediator_mediator){
1700462784
1700462785
super(_mediator);
1700462786
1700462787
}
1700462788
1700462789
//自有方法self-method
1700462790
1700462791
public void selfMethod2(){
1700462792
1700462793
//处理自己的业务逻辑
1700462794
1700462795
}
1700462796
1700462797
//依赖方法dep-method
1700462798
1700462799
public void depMethod2(){
1700462800
1700462801
//处理自己的业务逻辑
1700462802
1700462803
//自己不能处理的业务逻辑,委托给中介者处理
1700462804
1700462805
super.mediator.doSomething2();
1700462806
1700462807
}
1700462808
1700462809
}
1700462810
1700462811
为什么同事类要使用构造函数注入中介者,而中介者使用getter/setter方式注入同事类呢?这是因为同事类必须有中介者,而中介者却可以只有部分同事类。
1700462812
1700462813
1700462814
1700462815
1700462817
设计模式之禅 14.3 中介者模式的应用
1700462818
1700462819
14.3.1 中介者模式的优点
1700462820
1700462821
中介者模式的优点就是减少类间的依赖,把原有的一对多的依赖变成了一对一的依赖,同事类只依赖中介者,减少了依赖,当然同时也降低了类间的耦合。
1700462822
[
上一页 ]
[ :1.700462773e+09 ]
[
下一页 ]