前言:【模式總覽】——————————by xingoohtml
使用一箇中介的對象,封裝一組對象之間的交互,這樣這些對象就能夠不用彼此耦合。設計模式
這個中介者經常起着中間橋樑的做用,使其餘的對象能夠利用中介者完成某些行爲活動,所以它必須對全部的參與活動的對象瞭如指掌!ide
1 當一組對象要進行溝通或者業務上的交互,可是其關係卻又很複雜混亂時,能夠採用此模式。this
2 當一個對象與其餘的對象要進行緊密的交互,但又想服用該對象而不依賴其餘的對象時。spa
3 想創造一個運行於多個類之間的對象,又不想生成新的子類時。設計
Mediator 抽象的中介者,定義中介的規範code
interface Mediator{ public void colleagueChanged(Colleague c); }
ConcreteMediator 具體的中介者,一般內部依賴於多個業務對象htm
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 具體的業務角色blog
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 }
運行結果
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!哎呀我想要!