码迷,mamicode.com
首页 > 编程语言 > 详细

大话设计模式_中介者模式(Java代码)

时间:2014-06-10 00:55:22      阅读:390      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

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

简单描述:1个抽象Colleague,其派生多个具体ConcreteColleague,每个具体ConcreteColleague都认识一个具体中介者。他们发送消息的时候是通过中介者发送。1个中介者,具体中介者认识所有的ConcreteColleague(即持有所有具体ConcreteColleague的引用),它提供向其他具体ConcreteColleague发送消息的接口(这个接口只需要指明发送消息的ConcreteColleague而不要指明接收消息的ConcreteColleague)

大话设计模式中的截图:

bubuko.com,布布扣

代码例子:

AbstractMediator类:

bubuko.com,布布扣
1 package com.longsheng.mediator;
2 
3 public abstract class AbstractMediator {
4 
5     public abstract void sendMessage(String message, AbstractColleague mColleague);
6     
7 }
bubuko.com,布布扣

AbstractColleague类:

bubuko.com,布布扣
 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 }
bubuko.com,布布扣

ConcreteColleagueA类:

bubuko.com,布布扣
 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 }
bubuko.com,布布扣

ConcreteColleagueB类:

bubuko.com,布布扣
 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 }
bubuko.com,布布扣

Mediator类:

bubuko.com,布布扣
 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 }
bubuko.com,布布扣

客户端类:

bubuko.com,布布扣
 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 }
bubuko.com,布布扣

运行结果:

bubuko.com,布布扣
1 ConcreteColleagueB得到消息:A发的消息
2 ConcreteColleagueA得到消息:B发的消息
bubuko.com,布布扣

中介者模式在系统中很容易应用,也很容易在系统中误用。当系统出现了“多对多”交互复杂的对象群时,不要急于使用中介者模式,而要先反思你的系统在设计上是不是合理。

中介者模式一般应用于一组对象以定义良好但是复杂的方式进行通讯的场合,已经想定制一个分布在多个类中的行为,而又不想生成太多的子类的场合。

每个具体ConcreteColleague中的行为可以是不一样的

大话设计模式_中介者模式(Java代码),布布扣,bubuko.com

大话设计模式_中介者模式(Java代码)

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/creasylai19/p/3778707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!