Intermediary model

Keywords: Java network

Definition: A mediator object is used to encapsulate a series of object interactions. The mediator makes the objects do not need to interact visually, so that the coupling is loose and the interaction between them can be changed independently.

Type: Behavior class pattern

Class diagram:

 

The structure of intermediary model

The mediator model, also known as the mediator model, is divided into three parts as shown in the class diagram:

Abstract mediator: Define the interface between colleague class object and mediator object, and use it to communicate among different colleague classes. Generally, one or more abstract event methods are included and implemented by subclasses.

The mediator implementation class: inherits from the abstract mediator and implements the event methods defined in the abstract mediator. Receive a message from a colleague class, and then influence other simultaneous classes through the message.

Colleague classes: If an object affects other objects and is also affected by other objects, then these two objects are called colleague classes. In class diagrams, there is only one colleague class, which is actually the omission of reality. In practical applications, colleague classes are generally composed of many components, which interact with each other and depend on each other. The more colleagues there are, the more complex the relationship is. Moreover, a colleague class can also be represented as a set of implementations that inherit the same abstract class. In the mediator mode, the communication between colleague classes must be done through the mediator.

 

The relationship between complex objects

 

Using the mediator model:

 

 

A mediation object is used to encapsulate the object interaction of some columns. The mediator makes the objects do not need to refer to each other explicitly, so that the coupling is loose and the interaction between them can be changed independently. The idea of the mediator pattern is very simple. By introducing a mediation object, other objects can only interact with the mediation object, and the mediation object knows how to interact with all other objects, so that the interaction relationship between objects is eliminated, and the decoupling between objects is realized. From this, we can also see a problem, that is, the intermediary object controls the logic of the whole system, it will be too complex, which is a shortcoming.

The essence of the mediator pattern is to encapsulate interactions:

(1) When the object changes its own state, it reports to the intermediary object.

(2) The intermediary object controls the logic of the whole system. It knows how to interact with all objects.

(3) The object needs to respond to the request made by the intermediary object.

 

Advantages of the intermediary model

Proper use of the mediator pattern can avoid over-coupling among colleague classes and allow them to be used relatively independently.

Using the mediator pattern, one-to-many associations between objects can be transformed into one-to-one associations, making the relationships between objects easy to understand and maintain.

Using the mediator pattern, we can abstract the behavior and cooperation of objects, and deal with the interaction between objects more flexibly.

 

Applicable scenario

The use of the mediator model is considered only for those relationships that are networked among colleague classes.

The network structure can be transformed into a star structure, so that the relationship between colleagues becomes clearer.

 

Example:

Colleague classes: 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. That is to say, when modifying the number of class A, multiply this number by 100 and assign it to class B. When modifying class B, divide the number by 100 and assign it to class A. Class A and class B interact with each other and are called colleague classes.

The code is as follows:

abstract class AbstractColleague {  
    protected int number;  
  
    public int getNumber() {  
        return number;  
    }  
  
    public void setNumber(int number){  
        this.number = number;  
    }  
    //An abstract method that modifies associative objects while modifying numbers.
    public abstract void setNumber(int number, AbstractColleague coll);  
}  
  
class ColleagueA extends AbstractColleague{  
    public void setNumber(int number, AbstractColleague coll) {  
        this.number = number;  
        coll.setNumber(number*100);  
    }  
}  
  
class ColleagueB extends AbstractColleague{  
      
    public void setNumber(int number, AbstractColleague coll) {  
        this.number = number;  
        coll.setNumber(number/100);  
    }  
}  
  
public class Client {  
    public static void main(String[] args){  
  
        AbstractColleague collA = new ColleagueA();  
        AbstractColleague collB = new ColleagueB();  
          
        System.out.println("==========Set up A Influence B==========");  
        collA.setNumber(1288, collB);  
        System.out.println("collA Of number Value:"+collA.getNumber());  
        System.out.println("collB Of number Value:"+collB.getNumber());  
  
        System.out.println("==========Set up B Influence A==========");  
        collB.setNumber(87635, collA);  
        System.out.println("collB Of number Value:"+collB.getNumber());  
        System.out.println("collA Of number Value:"+collA.getNumber());  
    }  
} 

 

In the above code, Class A and Class B are related by direct association. If we want to use the mediator pattern, Class A and Class B can not be directly related. They must be related by a mediator.

 

abstract class AbstractColleague {  
    protected int number;  
  
    public int getNumber() {  
        return number;  
    }  
  
    public void setNumber(int number){  
        this.number = number;  
    }  
    //Note that the parameters here are no longer a colleague class, but an intermediary.
    public abstract void setNumber(int number, AbstractMediator am);  
}  
  
class ColleagueA extends AbstractColleague{  
  
    public void setNumber(int number, AbstractMediator am) {  
        this.number = number;  
        am.AaffectB();  
    }  
}  
  
class ColleagueB extends AbstractColleague{  
  
    @Override  
    public void setNumber(int number, AbstractMediator am) {  
        this.number = number;  
        am.BaffectA();  
    }  
}  
  
abstract class AbstractMediator {  
    protected AbstractColleague A;  
    protected AbstractColleague B;  
      
    public AbstractMediator(AbstractColleague a, AbstractColleague b) {  
        A = a;  
        B = b;  
    }  
  
    public abstract void AaffectB();  
      
    public abstract void BaffectA();  
  
}  
class Mediator extends AbstractMediator {  
  
    public Mediator(AbstractColleague a, AbstractColleague b) {  
        super(a, b);  
    }  
  
    //Dealing with the effect of A on B
    public void AaffectB() {  
        int number = A.getNumber();  
        B.setNumber(number*100);  
    }  
  
    //The effect of treatment B on A
    public void BaffectA() {  
        int number = B.getNumber();  
        A.setNumber(number/100);  
    }  
}  
  
public class Client {  
    public static void main(String[] args){  
        AbstractColleague collA = new ColleagueA();  
        AbstractColleague collB = new ColleagueB();  
          
        AbstractMediator am = new Mediator(collA, collB);  
          
        System.out.println("==========By setting A Influence B==========");  
        collA.setNumber(1000, am);  
        System.out.println("collA Of number The value is:"+collA.getNumber());  
        System.out.println("collB Of number The value is A 10 times:"+collB.getNumber());  
  
        System.out.println("==========By setting B Influence A==========");  
        collB.setNumber(1000, am);  
        System.out.println("collB Of number The value is:"+collB.getNumber());  
        System.out.println("collA Of number The value is B 0.1 Fold:"+collA.getNumber());  
          
    }  
}

 

Although the code is relatively long, it is still relatively easy to understand. In fact, it is to re-encapsulate the original code dealing with object relations into a mediation class, through which to deal with the relationship between objects.

Posted by saviiour on Tue, 09 Apr 2019 22:45:31 -0700