设计模式之中介者模式(Mediator)
2017-03-17 20:25
573 查看
意图:
用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互.
适用性:
1.一组对象以定义良好但是复杂的方式进行通信.产生的相互依赖关系结构混乱且难以理解.
2.一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象
3.想定制一个分布在多个类中的行为,而又不想生成太多的子类.
效果:
1.减少了子类生成
2.它将各Colleague解耦
3.它简化了对象协议
4.它对对象如何协作进行了抽象
5.它使控制中心话
代码实现:
package com.git.books.b_design_patterns.q_medlator;
/**
* @Description:协议中介者
* @author: songqinghu
* @date: 2017年3月17日 下午7:25:01
* Version:1.0
*/
public abstract class AgreementMedlator {
/**
* @描述:国家同事改变时触发具体处理
* @param colleague
* @return void
* @createTime:2017年3月17日
* @author: songqinghu
*/
public abstract void countryColleagueChanged(Colleague colleague,Action action);
}
结构图:
重点:
理解中介者模式,合理使用此模式
参考:
<<设计模式>>
<<Java与模式>>
用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互.
适用性:
1.一组对象以定义良好但是复杂的方式进行通信.产生的相互依赖关系结构混乱且难以理解.
2.一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象
3.想定制一个分布在多个类中的行为,而又不想生成太多的子类.
效果:
1.减少了子类生成
2.它将各Colleague解耦
3.它简化了对象协议
4.它对对象如何协作进行了抽象
5.它使控制中心话
代码实现:
package com.git.books.b_design_patterns.q_medlator;
/**
* @Description:协议中介者
* @author: songqinghu
* @date: 2017年3月17日 下午7:25:01
* Version:1.0
*/
public abstract class AgreementMedlator {
/**
* @描述:国家同事改变时触发具体处理
* @param colleague
* @return void
* @createTime:2017年3月17日
* @author: songqinghu
*/
public abstract void countryColleagueChanged(Colleague colleague,Action action);
}
package com.git.books.b_design_patterns.q_medlator; import java.util.HashSet; import java.util.Set; import java.util.TreeSet; /** * @Description: WTO国际贸易协议 * @author: songqinghu * @date: 2017年3月17日 下午7:58:12 * Version:1.0 */ public class WTOAgreementMedlator extends AgreementMedlator { private Set<Colleague> colleagues = new HashSet<Colleague>(); @Override public void countryColleagueChanged(Colleague colleague, Action action) { if(Action.Expand.equals(action)){ colleagues.add(colleague); }else if(Action.Fade.equals(action)){ colleagues.remove(colleague); } for (Colleague c : colleagues) { c.action(action); } } }
package com.git.books.b_design_patterns.q_medlator; public abstract class Colleague { private AgreementMedlator medlator; public Colleague(AgreementMedlator medlator) { this.medlator = medlator; } /** * @描述:子类行动方法 * @return void * @exception * @createTime:2017年3月17日 * @author: songqinghu */ public abstract void action(Action action); /** * @描述:子类改变方法 * @return void * @createTime:2017年3月17日 * @author: songqinghu */ public abstract void change(Change change); /** * @描述:返回当前协议对象 * @return AgreementMedlator * @createTime:2017年3月17日 * @author: songqinghu */ public AgreementMedlator getMedlator(){ return this.medlator; } } /** * @Description: 改变操作 * @author: songqinghu * @date: 2017年3月17日 下午7:40:24 * Version:1.0 */ enum Change{ Join,SignOut,Activity } /** * @Description: 具体行动指令 * @author: songqinghu * @date: 2017年3月17日 下午7:43:22 * Version:1.0 */ enum Action{ Expand,Fade,Exchange }
package com.git.books.b_design_patterns.q_medlator; /** * @Description: 中国伙伴 * @author: songqinghu * @date: 2017年3月17日 下午7:37:58 * Version:1.0 */ public class ChinaColleague extends Colleague { private String name ="china"; public ChinaColleague(AgreementMedlator medlator) { super(medlator); } @Override public void action(Action action) { //can create factroy process this action if(Action.Expand.equals(action)){ System.out.println("the " + name + " expanding !"); }else if(Action.Fade.equals(action)){ System.out.println("the " + name + " backward !"); }else{ System.out.println("the " + name + " get some thing!"); } } @Override public void change(Change change) { if(change ==null) throw new RuntimeException("the change can't null "); Action action ; if(Change.Join.equals(change)){ action = Action.Expand; }else if(Change.SignOut.equals(change)){ action = Action.Fade; }else{ action = Action.Exchange; } this.getMedlator().countryColleagueChanged(this,action); } }
package com.git.books.b_design_patterns.q_medlator; /** * @Description: 中国伙伴 * @author: songqinghu * @date: 2017年3月17日 下午7:37:58 * Version:1.0 */ public class AmericaColleague extends Colleague { private String name ="america"; public AmericaColleague(AgreementMedlator medlator) { super(medlator); } @Override public void action(Action action) { //can create factroy process this action if(Action.Expand.equals(action)){ System.out.println("the " + name + " expanding !"); }else if(Action.Fade.equals(action)){ System.out.println("the " + name + " backward !"); }else{ System.out.println("the " + name + " get some thing!"); } } @Override public void change(Change change) { if(change ==null) throw new RuntimeException("the change can't null "); Action action ; if(Change.Join.equals(change)){ action = Action.Expand; }else if(Change.SignOut.equals(change)){ action = Action.Fade; }else{ action = Action.Exchange; } this.getMedlator().countryColleagueChanged(this,action); } }
package com.git.books.b_design_patterns.q_medlator; /** * @Description: 中介者模式测试类 * @author: songqinghu * @date: 2017年3月17日 下午8:03:19 * Version:1.0 */ public class MedlatorClient { public static void main(String[] args) { AgreementMedlator medlator = new WTOAgreementMedlator(); Colleague china = new ChinaColleague(medlator); Colleague america = new AmericaColleague(medlator); china.change(Change.Join); america.change(Change.Join); china.change(Change.Activity); america.change(Change.SignOut); } }
结构图:
重点:
理解中介者模式,合理使用此模式
参考:
<<设计模式>>
<<Java与模式>>
相关文章推荐
- [导入]C#面向对象设计模式纵横谈(17):(行为型模式) Mediator 中介者模式.zip(8.75 MB)
- 设计模式之Mediator(中介者)
- 设计模式之(十七)中介者模式(mediator)
- 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)
- 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)
- 设计模式学习—Mediator(中介者)
- 设计模式--中介者模式(Mediator)
- 设计模式之Mediator中介者模式
- 设计模式学习—Mediator(中介者)
- 设计模式——中介者模式(Mediator)
- 设计模式之Mediator(中介者)
- 设计模式之Mediator(中介者)
- 设计模式--中介者模式(Mediator)
- 步步为营 .NET 设计模式学习笔记 二十、Mediator(中介者模式)
- 设计模式--中介者模式(Mediator)
- Net设计模式实例之中介者模式(Mediator Pattern)(1) 推荐
- Net设计模式实例之中介者模式(Mediator Pattern)(2)
- 设计模式----Mediator(中介者)模式
- 设计模式之Mediator(中介者)
- 设计模式(21)-中介者模式(Mediator)