java design pattern-Mediator (mediator) pattern

Mediator Mediator model definition     Mediator, or arbitrator, is similar to a house intermediary, such as the anchor and guests in a studio. When the anchor is speaking, the guest cannot interrupt him. The guest can only speak when the anchor stops speaking, and the anchor You can talk at any time, and there is an intermediary here:       1. The anchor tells the intermediary that the show has started and I want to speak.       2. The intermediary checks to see if the guest is talking. If so, ask him to stop and let the anchor speak. , if the guest is not talking, let the anchor speak       3. The guest tells the intermediary that I want to speak, and the intermediary checks to see if the anchor is talking. If so, tell the guest that the anchor is speaking, and you can wait and talk; if the anchor is not there If you want to speak, let the guest say  the code example is as follows:   






 
Java code   favorite code
  1. package mediator;  
  2. /* 
  3.  * The intermediary class is associated with two participants. The two participants are registered with the intermediary. 
  4.  */  
  5. public class Mediator {  
  6.      private Anchor anchor;  
  7.      private Guest guest;  
  8.      public void regAnchor(Anchor anchor){  
  9.       this.anchor=anchor;  
  10.      }  
  11.      public void regGuest(Guest guest){  
  12.       this.guest=guest;  
  13.      }  
  14.      //The intermediary speaks on behalf of the host. If the guest is speaking, interrupt him, the host will say, and the host's status will change to busy.  
  15.     public void anchorSpeak(){  
  16.      if(!guest.isFree()){  
  17.       guest.stop();  
  18.      }  
  19.      System.out.println("Anchor is speaking....");  
  20.      anchor.setFree(false);  
  21.     }  
  22.     //The intermediary speaks on behalf of the guest. If the host is free, the guest can speak, and the guest status changes to busy.  
  23.     //Otherwise, the guest will not be allowed to speak and the host will continue speaking.  
  24.     public void guestSpeak(){  
  25.      if(anchor.isFree()){  
  26.       System.out.println("Guest is speaking....");  
  27.       guest.setFree(false);  
  28.      }  
  29.      else{  
  30.       System.out.println("Anchor is speaking. Do not interrupt...");  
  31.      }  
  32.        
  33.     }  
  34.     //The intermediary stops talking on behalf of the host and sets the status to idle  
  35.     public void anchorStop(){  
  36.      System.out.println("Anchor stop speaking now....");  
  37.      anchor.setFree(true);  
  38.     }  
  39.     //中介代替嘉宾停止说话,设置状态为空闲  
  40.     public void guestStop(){  
  41.      System.out.println("Guest stop speaking now...");  
  42.      guest.setFree(true);  
  43.        
  44.     }  
  45.     }  

     

Java代码   favorite code
  1. package mediator;  
  2. /* 
  3.  * 主持人类,有一个状态,是否处于空闲状态 
  4.  * 有一个终结者的私有属性,和中介者产生关系 
  5.  */  
  6. public class Anchor {  
  7.          private boolean free;  
  8.   
  9.          private Mediator med;  
  10.          public Anchor(Mediator md){  
  11.           med=md;  
  12.          }  
  13.   
  14.          public boolean isFree() {  
  15.           return free;  
  16.          }  
  17.   
  18.          public void setFree(boolean free) {  
  19.           this.free = free;  
  20.          }  
  21.   
  22.          //通过中介者来开始说话  
  23.          public void speak() {  
  24.           med.anchorSpeak();  
  25.          }  
  26.   
  27.          //通过中介者来停止说话  
  28.          public void stop() {  
  29.           med.anchorStop();  
  30.          }  
  31.       
  32. }  


Java代码   favorite code
  1. package mediator;  
  2. /* 
  3.  * 嘉宾类,私有属性标记是否空闲 
  4.  * 私有属性中介者,与中介者产生关系 
  5.  */  
  6. public class Guest {  
  7.      private boolean free;  
  8.   
  9.      private Mediator med;  
  10.   
  11.      public Guest(Mediator md) {  
  12.       this.med = md;  
  13.      }  
  14.   
  15.      public boolean isFree() {  
  16.       return free;  
  17.      }  
  18.   
  19.      public void setFree(boolean free) {  
  20.       this.free = free;  
  21.      }  
  22.   
  23.      //通过中介者开始说话  
  24.      public void speak() {  
  25.       med.guestSpeak();  
  26.   
  27.      }  
  28.      //通过中介者停止说话  
  29.      public void stop() {  
  30.       med.guestStop();  
  31.      }  
  32.     }  


Java代码   favorite code
  1. package mediator;  
  2.   
  3. public class Test {  
  4.      public static void main(String[] args) {  
  5.           // TODO Auto-generated method stub  
  6.          //新建一个中介者  
  7.           Mediator med = new Mediator();  
  8.             
  9.           //新建一个主持人,并和中介者关联,让主持人知道中介都有什么能力(什么方法)  
  10.           Anchor anchor = new Anchor(med);  
  11.             
  12.           //新建一个嘉宾,并和中介者关联,让嘉宾知道中介都能什么能力(什么方法)  
  13.           Guest guest=new Guest(med);  
  14.             
  15.           //在中介者里面,注册主持人,和嘉宾,让中介者知道嘉宾和主持人都需要什么  
  16.           med.regAnchor(anchor);  
  17.           med.regGuest(guest);  
  18.             
  19.           //主持人说  
  20.           anchor.speak();  
  21.             
  22.           //嘉宾说  
  23.           guest.speak();  
  24.             
  25.           //主持人停止  
  26.           anchor.stop();  
  27.             
  28.           //嘉宾说  
  29.           guest.speak();  
  30.             
  31.           //主持人说  
  32.           anchor.speak();  
  33.             
  34.          }  
  35. }  


 

    A mediator is a role that stands between many objects and properly handles the relationships between them.       There are only two participant classes in the above code, but we can appropriately expand these according to the purpose of the intermediary model, that is, add participant classes, and then the intermediary will have to take on heavier tasks. We see the details above. There are many methods in the mediator class Mediator and they are a bit messy.      Therefore, while decoupling the connection between participant classes, the intermediary itself is also overloaded with tasks, because almost all business logic is handed over to the intermediary, which can be said to be a "highly anticipated" role. This is where the intermediary model falls short.      In addition, the attributes and methods of the participants in the above code example are the same. We can extract an abstract class to reduce the code, but sometimes we cannot extract the commonalities between multiple "participants" to form an abstraction. class, which also greatly increases the difficulty of using the intermediary mode.   
     







Guess you like

Origin blog.csdn.net/linwei_hello/article/details/24321041