Design patterns to achieve the two languages (C ++ and Java) (eight: intermediary model)

Defined Mediator (Mediator) mode: a mediation object to encapsulate the definition of the interaction between a series of objects, the coupling between the loose objects original, and may be varied independently of the interaction between them. Intermediary model called the mediation model, which is a typical application of the Law of Demeter.


Mediator object behavioral pattern is a pattern, the main advantages are as follows.

  1. Reducing the coupling between objects, objects that are easily multiplexed independently.
  2. The many associations between objects into one association to improve the flexibility of the system, making the system easy to maintain and extend.

Its main drawback is: when colleagues like too much responsibility will be a great mediator, it becomes complicated and large that the system difficult to maintain.

UML:

Mediator to achieve mutual transmission of messages among colleagues as an example.

C ++ implementation

 1 #include <string>
 2 #include <iostream>
 3 
 4 class Colleague;
 5 
 6 class Mediator
 7 {
 8 public:
 9     virtual void send(std::string  msg, Colleague * p) = 0;
10 };
11 
12 class Colleague
13 {
14 protected:
15     Mediator * m_mediator;
16 public:
17     Colleague(Mediator * p)
18     {
19         m_mediator = p;
20     }
21     virtual void send(std::string msg) = 0;
22     virtual void notify(std::string msg) = 0;
23 };
24 
25 class ConcreteColleague_0 : public Colleague
26 {
27 public:
28     ConcreteColleague_0(Mediator * p) : Colleague(p) {}
29     void send(std::string msg)
30     {
31         m_mediator->send(msg, this);
32     }
33     void notify(std::string msg)
34     {
35         std::cout << "Colleague_0 收到了消息:" << msg << std::endl;
36     }
37 };
38 
39 class ConcreteColleague_1 : public Colleague
40 {
41 public:
42     ConcreteColleague_1(Mediator * p) : Colleague(p) {}
43     void send(std::string msg)
44     {
45         m_mediator->send(msg, this);
46     }
47     void notify(std::string msg)
48     {
49         std::cout << "Colleague_1 收到了消息:" << msg << std::endl;
50     }
51 };
52 
53 class ConcreteMediator : public Mediator
54 {
55 private:
56     // here may be a list of 
57 is      the Colleague * m_p1;
 58      the Colleague * m_p2;
 59  public :
 60      void addColleague (the Colleague * P1, the Colleague * P2)
 61 is      {
 62 is          m_p1 = P1;
 63 is          m_p2 = P2;
 64      }
 65      void Send (std :: String msg, colleague * the p-)
 66      {
 67          // here to accept the message from a colleague sent me, should give anyone who according to specific requirements to
 68          // here is a very simple application knowledge. For example there may be two such always queues.
69          //Is a client queue, a queue is a customer 
70          IF (P == m_p1)
 71 is              m_p2-> Notify (MSG);
 72          the else 
73 is              m_p1-> Notify (MSG);    
 74      }
 75  };
 76  
77  int main ()
 78  {
 79      the using  namespace STD;
 80      // intermediary model 
81      ConcreteMediator * P = new new ConcreteMediator ();
 82      the Colleague * = pCol1 new new ConcreteColleague_0 (P);
 83      the Colleague * = pCol2 new newConcreteColleague_1 (the p-);
 84      p-> addColleague (pCol1, pCol2);
 85      pCol1-> the send ( " you from work yet? " );
 86      pCol2-> the send ( " Not yet, do you? " );
 87      the Delete pCol1 ;
 88      Delete pCol2;
 89      Delete P;
 90      return  0 ;
 91 is }

Java implementation:

 1 public interface Mediator {
 2     void send(String message, Colleague colleague);
 3 }
 4 
 5 public abstract class Colleague {
 6 
 7     protected Mediator mediator;
 8 
 9     public Colleague(Mediator mediator){
10         this.mediator = mediator;
11     }
12 
13     public abstract void send(String message);
14 
15     abstract public void notify(String message);
16 
17 }
18 
19 public class Colleague1 extends Colleague {
20 
21     public Colleague1(Mediator mediator){
22         super(mediator);
23     }
24 
25     public void send(String message){
26         mediator.send(message, this);
27     }
28 
29     public void notify(String message){
30         System.out.println("Colleague1 received " + message);
31     }
32 }
33 
34 public class Colleague2 extends Colleague {
35 
36     public Colleague2(Mediator mediator){
37         super(mediator);
38     }
39 
40     public void send(String message){
41         mediator.send(message, this);
42     }
43 
44     public void notify(String message){
45         System.out.println("Colleague2 received " + message);
46     }
47 }
48 
49 public class ConcreteMediator implements Mediator {
50 
51     private Colleague colleague1;
52 
53     private Colleague colleague2;
54 
55     public void addColleague(Colleague colleague1, Colleague colleague2){
56         this.colleague1 = colleague1;
57         this.colleague2 = colleague2;
58     }
59 
60     @Override
61     public void send(String message, Colleague colleague) {
62         if (colleague == colleague1){
63             colleague2.notify(message);
64         }else if(colleague == colleague2){
65             colleague1.notify(message);
66         }
67     }
68 }
69 
70 public class Main {
71 
72     public static void main(String[] args) {
73         ConcreteMediator concreteMediator = new ConcreteMediator();
74         Colleague1 colleague1 = new Colleague1(concreteMediator);
75         Colleague2 colleague2 = new Colleague2(concreteMediator);
76         concreteMediator.addColleague(colleague1, colleague2);
77         colleague1.send("下班了吗");
78         colleague2.send("没呢");
79     }
80 }

 

Guess you like

Origin www.cnblogs.com/Asp1rant/p/11354957.html
Recommended