Jane said design pattern - intermediary model

First, what is the intermediary model

  Mediation is no stranger to the word, that is, the housing agency "intermediary" is meant middleman. For example MVC pattern, C (Controller Controller) is M (Model model) and V (View view) of the intermediary, acts as an intermediary when the rear end of the front interactions.

  Intermediary model (Mediator) , with an intermediary object that encapsulates set of objects interact. Mediator the respective objects do not explicitly referring to each other, so that it loosely coupled, and can be changed independently of the interaction between them. UML structure is as follows:

  Wherein, Mediator abstract intermediary defined colleagues object to a broker object's interface; the Colleague abstract colleagues class; ConcreteMediator specific intermediary objects, methods abstract class, it needs to know all of the specific colleague classes, and the specific colleague receive messages, issue commands to specific objects colleagues; ConcreteColleague specific class colleagues, each specific colleagues only know their own behavior, rather than to understand the situation like other colleagues, but they know they are nothing but an intermediary object.

  1. Mediator abstract

  Abstract intermediary role defined unified interface for communication between co-workers role.

. 1  public  abstract  class Mediator {
 2      
. 3      // abstract method for transmitting a message 
. 4      public  abstract  void Send (String Message, the Colleague Colleague);
 . 5      
. 6 }

  2. abstract class colleagues

  Each character knows a colleague mediator role, and we must collaborate with other colleagues when the role of communication through an intermediary role. Colleagues behavior of each class two ways: one is a colleague own actions, such as changing the state of the object itself, its handling behavior, this behavior is called spontaneous behavior, with no dependencies other colleagues or intermediary class; second species must rely on intermediaries to complete the act, called the dependent method.

1 public abstract class Colleague {
2     
3     protected Mediator mediator;
4     
5     public Colleague(Mediator mediator) {
6         this.mediator = mediator;
7     }
8 
9 }

  3. Specific intermediaries class

  In particular intermediary role by coordinating role for collaboration colleague behavior, so it must rely on each co-worker roles.

 1 public class ConcreteMediator extends Mediator {
 2 
 3     private ConcreteColleague1 colleague1;
 4     private ConcreteColleague2 colleague2;
 5     
 6     public void setColleague1(ConcreteColleague1 colleague1) {
 7         this.colleague1 = colleague1;
 8     }
 9 
10     public void setColleague2(ConcreteColleague2 colleague2) {
11         this.colleague2 = colleague2;
12     }
13 
14     @Override
15     public void send(String message, Colleague colleague) {
16         if(colleague == colleague1) {
17             colleague2.notify(message);
18         } else {
19             colleague1.notify(message);
20         }
21     }
22 
23 }

  4. DETAILED class colleagues

  Here to ConcreteColleague1 for example, ConcreteColleague2 not repeat them.

 1 public class ConcreteColleague1 extends Colleague {
 2 
 3     public ConcreteColleague1(Mediator mediator) {
 4         super(mediator);
 5     }
 6     
 7     public void send(String message) {
 8         mediator.send(message, this);
 9     }
10     
11     public void notify(String message) {
12         System.out.println("同事1得到消息:" + message);
13     }
14 
15 }

  5. Client Client

  First create a specific intermediary object, and then instantiate the class and two colleagues bind specifically with the Mediator, colleague1 and colleague2 send information through intermediaries.

 1 public class Client {
 2     
 3     public static void main(String[] args) {
 4         ConcreteMediator mediator = new ConcreteMediator();
 5         
 6         ConcreteColleague1 colleague1 = new ConcreteColleague1(mediator);
 7         ConcreteColleague2 colleague2 = new ConcreteColleague2(mediator);
 8         
 9         mediator.setColleague1(colleague1);
10         mediator.setColleague2(colleague2);
11         
12         colleague1.send("Nice to meet u.");
13         colleague2.send("Nice to meet u too.");
14     }
15 
16 }

  Results are as follows:
  

Second, the application of the intermediary model

  1. When to Use

  • Coupled to each other when a plurality of classes, form a network structure

  2. Methods

  • The mesh structure is separated into a star structure

  3. advantage

  • Reducing inter-dependent class, reducing coupling
  • In line with the principles of Demeter

  4. shortcomings

  • Mediator will expand the large and complex logic

  5. usage scenarios

  • The presence of more complex relationships between objects in a reference system
  • A middle class want to package a plurality of classes of behavior, but do not want to generate too much subclass

  6. Application Examples

  • UN / WTO as an intermediary to coordinate the various national
  • Housing agency / airport scheduling system
  • MVC framework, where C (Contorller controller) is M (Model model) and V (View view) of the intermediary

  7. Notes

  • Should not be used when chaos duties

Third, to achieve the intermediary model

  We have mentioned above, as a United Nations by national representatives, for the maintenance of international peace and security, to solve the problem of the role of economic, social, cultural or humanitarian character of international, it is the role of a mediator, the mediator, countries can be completely direct a relationship through the "United Nations" is a mediator, rather than direct communication.

  Here's to the UN Security Council as a mediator, look at the intermediary model. UML is as follows:

  1. The United Nations agencies

  Abstract intermediaries.

1 public abstract class UnitedNations {
2     
3     public abstract void declare(String message, Country country);
4 
5 }

  2. National class

  Abstract State class, it should be declared an intermediary role.

1 public class Country {
2     
3     protected UnitedNations unitedNations;
4     
5     public Country(UnitedNations unitedNations) {
6         this.unitedNations = unitedNations;
7     }
8 
9 }

  3. Country-specific class

  Country-specific class, the country inherited abstract class. Here at USA class, for example, Iraq category not repeat them.

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

  4. The United Nations Security Council

  In particular intermediary role, inherited abstract intermediaries, rewrite it declare () method to send different information to different countries.

 1 public class UnitedNationsSecurityCouncil extends UnitedNations {
 2 
 3     private USA usa;
 4     private Iraq iraq;
 5     
 6     public void setUsa(USA usa) {
 7         this.usa = usa;
 8     }
 9 
10     public void setIraq(Iraq iraq) {
11         this.iraq = iraq;
12     }
13 
14     @Override
15     public void declare(String message, Country country) {
16         if(country == usa) {
17             iraq.getMessage(message);
18         } else {
19             usa.getMessage(message);
20         }
21     }
22 
23 }

  5. Client Client

  Examples of a UN Security Council objects, dialogue between the two countries through the United Nations Security Council.

. 1  public  class Client {
 2  
. 3      public  static  void main (String [] args) {
 . 4          
. 5          UnitedNationsSecurityCouncil the UNSC = new new UnitedNationsSecurityCouncil ();
 . 6          
. 7          USA USA = new new USA (the UNSC);
 . 8          Iraq Iraq = new new Iraq (the UNSC);
 . 9          
10          UNSC.setUsa (usa);
 11          UNSC.setIraq (iraq);
 12          
13          usa.declare ( "allowed to develop nuclear weapons" );
 14          iraq.declare ( "we do not have nuclear weapons" );
 15     }
16 
17 }

  Results are as follows:

  

 

  Source Address: https://gitee.com/adamjiangwh/GoF 

Guess you like

Origin www.cnblogs.com/adamjwh/p/10959987.html