中介者模式

《大话设计模式》第二十五章

[java]  view plain copy
  1. package ch25;  
  2.   
  3. public abstract class Country {  
  4.     protected UnitedNations mediator;  
  5.   
  6.     public Country(UnitedNations mediator) {  
  7.         this.mediator = mediator;  
  8.     }  
  9. }  


 

[java]  view plain copy
  1. package ch25;  
  2.   
  3. public abstract class UnitedNations {  
  4.     public abstract void declare(String message, Country colleague);  
  5. }  


 

[java]  view plain copy
  1. package ch25;  
  2.   
  3. public class Irag extends Country {  
  4.   
  5.     public Irag(UnitedNations mediator) {  
  6.         super(mediator);  
  7.     }  
  8.     public void declare(String message) {  
  9.         mediator.declare(message, this);  
  10.     }  
  11.       
  12.     public void getMessage(String message) {  
  13.         System.out.println("伊拉克获得对方信息:" + message);  
  14.     }  
  15. }  


 

[java]  view plain copy
  1. package ch25;  
  2.   
  3. public class USA extends Country {  
  4.   
  5.     public USA(UnitedNations mediator) {  
  6.         super(mediator);  
  7.     }  
  8.       
  9.     public void declare(String message) {  
  10.         mediator.declare(message, this);  
  11.     }  
  12.       
  13.     public void getMessage(String message) {  
  14.         System.out.println("美国获得对方信息:" + message);  
  15.     }  
  16.   
  17. }  


 

[java]  view plain copy
  1. package ch25;  
  2.   
  3. public class UnitedNationsSecurityCouncil extends UnitedNations {  
  4.     private USA colleague1;  
  5.     private Irag colleague2;  
  6.       
  7.     public void setColleague1(USA colleague1) {  
  8.         this.colleague1 = colleague1;  
  9.     }  
  10.     public void setColleague2(Irag colleague2) {  
  11.         this.colleague2 = colleague2;  
  12.     }  
  13.     @Override  
  14.     public void declare(String message, Country colleague) {  
  15.         if (colleague == colleague1) {  
  16.             colleague2.getMessage(message);  
  17.         } else {  
  18.             colleague1.getMessage(message);  
  19.         }  
  20.     }  
  21.   
  22. }  


 

[java]  view plain copy
  1. package ch25;  
  2.   
  3. /** 
  4.  * 中介者模式 
  5.  * @author Administrator 
  6.  * 
  7.  */  
  8. public class Client {  
  9.   
  10.     /** 
  11.      * @param args 
  12.      */  
  13.     public static void main(String[] args) {  
  14.         UnitedNationsSecurityCouncil unsc = new UnitedNationsSecurityCouncil();  
  15.           
  16.         USA c1 = new USA(unsc);  
  17.         Irag c2 = new Irag(unsc);  
  18.           
  19.         unsc.setColleague1(c1);  
  20.         unsc.setColleague2(c2);  
  21.           
  22.         c1.declare("不准研制核武器,否则要发动战争");  
  23.         c2.declare("我们没有核武器,也不怕侵略");  
  24.     }  
  25.   
  26. }  


运行:

[plain]  view plain copy
  1. 伊拉克获得对方信息:不准研制核武器,否则要发动战争  
  2. 美国获得对方信息:我们没有核武器,也
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值