【设计模式】—— 中介者模式Mediator

  前言:【模式总览】——————————by xingoo

  模式意图

  使用一个中介的对象,封装一组对象之间的交互,这样这些对象就可以不用彼此耦合。

  这个中介者常常起着中间桥梁的作用,使其他的对象可以利用中介者完成某些行为活动,因此它必须对所有的参与活动的对象了如指掌!

  应用场景

  1 当一组对象要进行沟通或者业务上的交互,但是其关系却又很复杂混乱时,可以采用此模式。

  2 当一个对象与其他的对象要进行紧密的交互,但又想服用该对象而不依赖其他的对象时。

  3 想创造一个运行于多个类之间的对象,又不想生成新的子类时。

  模式结构

  

  Mediator 抽象的中介者,定义中介的规范

interface Mediator{
    public void colleagueChanged(Colleague c);
}

  ConcreteMediator 具体的中介者,通常内部依赖于多个业务对象

class ConcreteMediator implements Mediator{
    private Colleague1 col1;
    private Colleague2 col2;
    public void colleagueChanged(Colleague c) {
        col1.action();
        col2.action();
    }
    public void createConcreteMediator() {
        col1 = new Colleague1(this);
        col2 = new Colleague2(this);
    }
    private Colleague1 getCol1() {
        return col1;
    }
    public Colleague2 getCol2() {
        return col2;
    }
}

  Colleague 抽象的业务角色

abstract class Colleague{
    private Mediator mediator;
    public Colleague(Mediator mediator){
        this.mediator = mediator;
    }
    public Mediator getMediator() {
        return mediator;
    }
    public abstract void action();
    public void change(){
        mediator.colleagueChanged(this);
    }
}

  Colleague1 Colleague2 具体的业务角色

class Colleague1 extends Colleague{
    public Colleague1(Mediator m){
        super(m);
    }
    public void action(){
        System.out.println("this is an action from Colleague1");
    }
}
class Colleague2 extends Colleague{
    public Colleague2(Mediator m){
        super(m);
    }
    public void action(){
        System.out.println("this is an action from Colleague2");
    }
}

  全部代码

 1 package com.xingoo.test.design.mediator;
 2 abstract class Colleague{
 3     private Mediator mediator;
 4     
 5     public Colleague(Mediator mediator){
 6         this.mediator = mediator;
 7     }
 8     
 9     public Mediator getMediator() {
10         return mediator;
11     }
12     
13     public abstract void action();
14     
15     public void change(){
16         mediator.colleagueChanged(this);
17     }
18 }
19 class Colleague1 extends Colleague{
20     public Colleague1(Mediator m){
21         super(m);
22     }
23     public void action(){
24         System.out.println("this is an action from Colleague1");
25     }
26 }
27 class Colleague2 extends Colleague{
28     public Colleague2(Mediator m){
29         super(m);
30     }
31     public void action(){
32         System.out.println("this is an action from Colleague2");
33     }
34 }
35 interface Mediator{
36     public void colleagueChanged(Colleague c);
37 }
38 class ConcreteMediator implements Mediator{
39     private Colleague1 col1;
40     private Colleague2 col2;
41     
42     public void colleagueChanged(Colleague c) {
43         col1.action();
44         col2.action();
45     }
46     
47     public void createConcreteMediator() {
48         col1 = new Colleague1(this);
49         col2 = new Colleague2(this);
50     }
51     
52     private Colleague1 getCol1() {
53         return col1;
54     }
55     
56     public Colleague2 getCol2() {
57         return col2;
58     }
59     
60 }
61 
62 public class Client {
63     public static void main(String[] args) {
64         ConcreteMediator mediator = new ConcreteMediator();
65         mediator.createConcreteMediator();
66         Colleague1 col1 = new Colleague1(mediator);
67 //        Colleague2 col2 = new Colleague2(mediator);
68         mediator.colleagueChanged(col1);
69     }
70 }
View Code

  运行结果

this is an action from Colleague1
this is an action from Colleague2

 

  生活中的设计模式

  

 

  毕业的同学们,第一个要解决的问题就是租房子,当白富美高富帅出没社会后,穷屌丝没了生存之地。但是只要勤劳,一样有饭吃有房住!

  这里房屋中介好比是一个中介者,它知道每个租客的身份信息,当有房屋出租后,它会发送给每一个租客消息。

  这样,租客们中有一个变化活动时,都会利用房屋中介,发送消息到其他的租客。下面就是模仿的一个过程。

  房屋中介代码如下:

 1 interface StateMediator{
 2     public void sell(Tenant tenant);
 3 }
 4 class RealEstateAgents implements StateMediator{
 5     private TenantA teA;
 6     private TenantB teB;
 7     private TenantC teC;
 8     
 9     public void sell(Tenant tenant) {
10         System.out.println("海景洋房 已经租出去了!");
11         if(tenant instanceof TenantA){
12             teB.crying();
13             teC.crying();
14         }else if(tenant instanceof TenantB){
15             teA.crying();
16             teC.crying();
17         }else if(tenant instanceof TenantC){
18             teB.crying();
19             teA.crying();
20         }
21     }
22     
23     public void createAgents(){
24         teA = new TenantA(this);
25         teB = new TenantB(this);
26         teC = new TenantC(this);
27     }
28 }

  租客的代码如下:

 1 abstract class Tenant{
 2     private RealEstateAgents agent;
 3     public Tenant(RealEstateAgents agent) {
 4         this.agent = agent;
 5     }
 6     public abstract void crying();
 7     public void renting(){
 8         agent.sell(this);
 9     }
10 }
11 class TenantA extends Tenant{
12     public TenantA(RealEstateAgents agent) {
13         super(agent);
14     }
15     public void crying() {
16         System.out.println("我是高富帅 TenantA!哎呀我想要!");
17     }
18 }
19 class TenantB extends Tenant{
20     public TenantB(RealEstateAgents agent) {
21         super(agent);
22     }
23     public void crying() {
24         System.out.println("我是白富美 TenantB!哎呀我想要!");
25     }
26 }
27 class TenantC extends Tenant{
28     public TenantC(RealEstateAgents agent) {
29         super(agent);
30     }
31     public void crying() {
32         System.out.println("我是穷屌丝 TenantC!哎呀我想要!");
33     }
34 }

  产生的业务活动如下:

 1 public class ClientTest {
 2     public static void main(String[] args) {
 3         RealEstateAgents agent = new RealEstateAgents();
 4         agent.createAgents();
 5         
 6         System.out.println("TeA 抢到了房子了!");
 7         agent.sell(new TenantA(agent));
 8         
 9         System.out.println("过了两个月 TeB 抢到了房子了!");
10         agent.sell(new TenantB(agent));
11     }
12 }

  运行结果

TeA 抢到了房子了!
海景洋房 已经租出去了!
我是白富美 TenantB!哎呀我想要!
我是穷屌丝 TenantC!哎呀我想要!
过了两个月 TeB 抢到了房子了!
海景洋房 已经租出去了!
我是高富帅 TenantA!哎呀我想要!
我是穷屌丝 TenantC!哎呀我想要!
posted @ 2014-11-07 22:09 xingoo 阅读(...) 评论(...) 编辑 收藏