【設計模式】—— 中介者模式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!哎呀我想要!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM