标签:c style class blog code java
中介者模式:用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显示的相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
简单描述:1个抽象Colleague,其派生多个具体ConcreteColleague,每个具体ConcreteColleague都认识一个具体中介者。他们发送消息的时候是通过中介者发送。1个中介者,具体中介者认识所有的ConcreteColleague(即持有所有具体ConcreteColleague的引用),它提供向其他具体ConcreteColleague发送消息的接口(这个接口只需要指明发送消息的ConcreteColleague而不要指明接收消息的ConcreteColleague)
大话设计模式中的截图:
代码例子:
AbstractMediator类:
1 package com.longsheng.mediator; 2 3 public abstract class AbstractMediator { 4 5 public abstract void sendMessage(String message, AbstractColleague mColleague); 6 7 }
AbstractColleague类:
1 package com.longsheng.mediator; 2 3 public abstract class AbstractColleague { 4 5 protected AbstractMediator mMediator; 6 7 public AbstractColleague(AbstractMediator mMediator) { 8 this.mMediator = mMediator; 9 } 10 11 public AbstractMediator getmMediator() { 12 return mMediator; 13 } 14 15 public void setmMediator(AbstractMediator mMediator) { 16 this.mMediator = mMediator; 17 } 18 19 }
ConcreteColleagueA类:
1 package com.longsheng.mediator; 2 3 public class ConcreteColleagueA extends AbstractColleague { 4 5 public ConcreteColleagueA( AbstractMediator mMediator ) { 6 super(mMediator); 7 } 8 9 public void sendMessage( String message ) { 10 mMediator.sendMessage(message, this); 11 } 12 13 public void getMessage(String message) { 14 System.out.println("ConcreteColleagueA得到消息:" + message); 15 } 16 17 }
ConcreteColleagueB类:
1 package com.longsheng.mediator; 2 3 public class ConcreteColleagueB extends AbstractColleague { 4 5 public ConcreteColleagueB( AbstractMediator mMediator ) { 6 super(mMediator); 7 } 8 9 public void sendMessage( String message ) { 10 mMediator.sendMessage(message, this); 11 } 12 13 public void getMessage(String message) { 14 System.out.println("ConcreteColleagueB得到消息:" + message); 15 } 16 17 }
Mediator类:
1 package com.longsheng.mediator; 2 3 public class Mediator extends AbstractMediator { 4 5 private ConcreteColleagueA mColleagueA; 6 private ConcreteColleagueB mColleagueB; 7 8 public ConcreteColleagueA getmColleagueA() { 9 return mColleagueA; 10 } 11 12 public void setmColleagueA(ConcreteColleagueA mColleagueA) { 13 this.mColleagueA = mColleagueA; 14 } 15 16 public ConcreteColleagueB getmColleagueB() { 17 return mColleagueB; 18 } 19 20 public void setmColleagueB(ConcreteColleagueB mColleagueB) { 21 this.mColleagueB = mColleagueB; 22 } 23 24 @Override 25 public void sendMessage(String message, AbstractColleague mColleague) { 26 if(mColleague == null) { 27 System.out.println("同事不能为空!"); 28 return; 29 } 30 if( mColleague == mColleagueA ) { 31 mColleagueB.getMessage(message); 32 } else if( mColleague == mColleagueB ) { 33 mColleagueA.getMessage(message); 34 } 35 } 36 37 }
客户端类:
1 package com.longsheng.mediator; 2 3 public class Client { 4 5 public static void main( String args[] ) { 6 Mediator mMediator = new Mediator(); 7 ConcreteColleagueA mColleagueA = new ConcreteColleagueA(mMediator); 8 ConcreteColleagueB mColleagueB = new ConcreteColleagueB(mMediator); 9 10 mMediator.setmColleagueA(mColleagueA); 11 mMediator.setmColleagueB(mColleagueB); 12 13 mColleagueA.sendMessage("A发的消息"); 14 mColleagueB.sendMessage("B发的消息"); 15 } 16 17 }
运行结果:
1 ConcreteColleagueB得到消息:A发的消息 2 ConcreteColleagueA得到消息:B发的消息
中介者模式在系统中很容易应用,也很容易在系统中误用。当系统出现了“多对多”交互复杂的对象群时,不要急于使用中介者模式,而要先反思你的系统在设计上是不是合理。
中介者模式一般应用于一组对象以定义良好但是复杂的方式进行通讯的场合,已经想定制一个分布在多个类中的行为,而又不想生成太多的子类的场合。
每个具体ConcreteColleague中的行为可以是不一样的
大话设计模式_中介者模式(Java代码),布布扣,bubuko.com
标签:c style class blog code java
原文地址:http://www.cnblogs.com/creasylai19/p/3778707.html