13. Mediator (mediator) mode

An intermediary object is used to encapsulate a series of object interactions. The intermediary enables objects to interact without explicit interaction, thereby loosening the coupling, and independently changing the interaction between them.

Class Diagram

Code example

We use an example to illustrate what a colleague class is: There are two classes A and B, each with a number, and to ensure that the number in class B is always 100 times the number in class A. In other words, when modifying the number of class A, multiply this number by 100 and assign it to class B, and when modifying class B, divide the number by 100 and assign it to class A. Class A, Class B influence each other, it is called colleague class. code show as below:

1.	abstract class AbstractColleague {  
2.	    protected int number;  
3.	  
4.	    public int getNumber() {  
5.	        return number;  
6.	    }  
7.	  
8.	    public void setNumber(int number){  
9.	        this.number = number;  
10.	    }  
11.	    //抽象方法,修改数字时同时修改关联对象  
12.	    public abstract void setNumber(int number, AbstractColleague coll);  
13.	}  
14.	  
15.	class ColleagueA extends AbstractColleague{  
16.	    public void setNumber(int number, AbstractColleague coll) {  
17.	        this.number = number;  
18.	        coll.setNumber(number*100);  
19.	    }  
20.	}  
21.	  
22.	class ColleagueB extends AbstractColleague{  
23.	      
24.	    public void setNumber(int number, AbstractColleague coll) {  
25.	        this.number = number;  
26.	        coll.setNumber(number/100);  
27.	    }  
28.	}  
29.	  
30.	public class Client {  
31.	    public static void main(String[] args){  
32.	  
33.	        AbstractColleague collA = new ColleagueA();  
34.	        AbstractColleague collB = new ColleagueB();  
35.	          
36.	        System.out.println("==========设置A影响B==========");  
37.	        collA.setNumber(1288, collB);  
38.	        System.out.println("collA的number值:"+collA.getNumber());  
39.	        System.out.println("collB的number值:"+collB.getNumber());  
40.	  
41.	        System.out.println("==========设置B影响A==========");  
42.	        collB.setNumber(87635, collA);  
43.	        System.out.println("collB的number值:"+collB.getNumber());  
44.	        System.out.println("collA的number值:"+collA.getNumber());  
45.	    }  
46.	}  

In the above code, class A and class B are related through direct association. If we want to use the intermediary model, the class A and class B cannot be directly associated. They must use an intermediary to achieve the purpose of association. .

1.	abstract class AbstractColleague {  
2.	    protected int number;  
3.	  
4.	    public int getNumber() {  
5.	        return number;  
6.	    }  
7.	  
8.	    public void setNumber(int number){  
9.	        this.number = number;  
10.	    }  
11.	    //注意这里的参数不再是同事类,而是一个中介者  
12.	    public abstract void setNumber(int number, AbstractMediator am);  
13.	}  
14.	  
15.	class ColleagueA extends AbstractColleague{  
16.	  
17.	    public void setNumber(int number, AbstractMediator am) {  
18.	        this.number = number;  
19.	        am.AaffectB();  
20.	    }  
21.	}  
22.	  
23.	class ColleagueB extends AbstractColleague{  
24.	  
25.	    @Override  
26.	    public void setNumber(int number, AbstractMediator am) {  
27.	        this.number = number;  
28.	        am.BaffectA();  
29.	    }  
30.	}  
31.	  
32.	abstract class AbstractMediator {  
33.	    protected AbstractColleague A;  
34.	    protected AbstractColleague B;  
35.	      
36.	    public AbstractMediator(AbstractColleague a, AbstractColleague b) {  
37.	        A = a;  
38.	        B = b;  
39.	    }  
40.	  
41.	    public abstract void AaffectB();  
42.	      
43.	    public abstract void BaffectA();  
44.	  
45.	}  
46.	class Mediator extends AbstractMediator {  
47.	  
48.	    public Mediator(AbstractColleague a, AbstractColleague b) {  
49.	        super(a, b);  
50.	    }  
51.	  
52.	    //处理A对B的影响  
53.	    public void AaffectB() {  
54.	        int number = A.getNumber();  
55.	        B.setNumber(number*100);  
56.	    }  
57.	  
58.	    //处理B对A的影响  
59.	    public void BaffectA() {  
60.	        int number = B.getNumber();  
61.	        A.setNumber(number/100);  
62.	    }  
63.	}  
64.	  
65.	public class Client {  
66.	    public static void main(String[] args){  
67.	        AbstractColleague collA = new ColleagueA();  
68.	        AbstractColleague collB = new ColleagueB();  
69.	          
70.	        AbstractMediator am = new Mediator(collA, collB);  
71.	          
72.	        System.out.println("==========通过设置A影响B==========");  
73.	        collA.setNumber(1000, am);  
74.	        System.out.println("collA的number值为:"+collA.getNumber());  
75.	        System.out.println("collB的number值为A的10倍:"+collB.getNumber());  
76.	  
77.	        System.out.println("==========通过设置B影响A==========");  
78.	        collB.setNumber(1000, am);  
79.	        System.out.println("collB的number值为:"+collB.getNumber());  
80.	        System.out.println("collA的number值为B的0.1倍:"+collA.getNumber());  
81.	          
82.	    }  
83.	}  

Intermediary model structure

The intermediary model is also called the mediator model. From the class diagram, it is divided into three parts:

  1. Abstract intermediary: Define the interface from colleague objects to intermediary objects for communication between colleague classes. Generally include one or several abstract event methods, and are implemented by subclasses.
  2. Mediator implementation class: inherited from the abstract mediator, and implement the event methods defined in the abstract mediator. Receive messages from a colleague class, and then influence other simultaneous classes through the message.
  3. Colleague class: If an object affects other objects and is also affected by other objects, then these two objects are called colleague classes. In the class diagram, there is only one colleague class, which is actually a practical omission. In practical applications, colleague classes are generally composed of multiple, which influence and depend on each other. The more colleagues, the more complicated the relationship. Moreover, the colleague class can also be represented as a group of implementations that inherit the same abstract class. In the intermediary model, colleague classes must pass through the intermediary to pass messages.

Why use the intermediary model

Generally speaking, the relationship between colleague classes is relatively complex. When multiple colleague classes are related to each other, the relationship between them will appear as a complex network structure. This is an over-coupled architecture, that is, no Conducive to the reuse of classes, but also unstable. For example, in the figure below, there are six colleague objects. If object 1 changes, then 4 objects will be affected. If object 2 changes, then 5 objects will be affected. In other words, the design of direct association between colleague classes is not good.

If the intermediary model is introduced, the relationship between the colleague classes will become a star structure. From the figure, we can see that any change in a class will only affect the class itself and the intermediary, thus reducing the system Coupling. A good design must not encapsulate all the object relationship processing logic in this class, but use a special class to manage behaviors that do not belong to itself.

Pros and cons of the intermediary model

  1. Appropriate use of the intermediary model can avoid excessive coupling between colleague classes, so that each colleague class can be used relatively independently.
  2. Using the intermediary model can transform one-to-many associations between objects into one-to-one associations, making the relationships between objects easy to understand and maintain.
  3. Using the intermediary model can abstract the behavior and collaboration of objects, and can handle the interaction between objects more flexibly.

Mediator mode usage scenarios

In object-oriented programming, a class is bound to have a dependency relationship with other classes, and a completely independent class is meaningless. It is also quite common that a class depends on multiple classes at the same time. Since there is such a situation, it shows that the one-to-many dependency relationship has its rationality, and the appropriate use of the intermediary model can make the original messy object relationship clear, but if Abuse, it may bring counterproductive effects. Generally speaking, only for the kind of network structure between colleagues, will consider using the intermediary model. The network structure can be changed to a star structure to make the relationship between colleagues clearer.

The intermediary model is a more commonly used model, and it is also a model that is easier to abuse. In most cases, the relationship between colleague classes will not be complicated to a messy network structure. Therefore, in most cases, it is sufficient to encapsulate the dependencies between objects within the colleague class, and there is no need to introduce an intermediary者 mode. Abusing the intermediary model will only make things more complicated.

 

 

 

Guess you like

Origin blog.csdn.net/sinat_37138973/article/details/88636247