Java intermediary model

Intermediary model (Mediator Pattern) is used to reduce the complexity of communications between a plurality of objects and classes. This mode provides an intermediate class, which generally handles the communication between the different classes and the loosely coupled, making the code easier to maintain. Mediator pattern belongs behavioral patterns.

Intent: with a mediation object that encapsulates a set of objects interact intermediary keeping objects explicitly referring to each other, so that it loosely coupled, and can be changed independently of the interaction between them.

Mainly to solve: there are a lot of objects and relationships between objects, this will inevitably lead to the structure of the system becomes very complicated, but if an object is changed, we also need to track objects associated with it, and make the appropriate treatment .

When to use: a plurality of classes coupled to each other to form a network structure.

How to solve: separating said network structure of the star structure.

The key code: the communication between the encapsulated object class Colleague a single process.

Application examples: 1, prior to China's accession to WTO countries trade with each other is a complex structure, it is by WTO countries to trade with each other. 2, airport scheduling system. 3, MVC frame, where C (controller) is M (models) and V (view) of the intermediary.

Advantages: 1, reduces the complexity of the class, converted into the one-to-many. 2, the decoupling between classes. 3, in line with the principles of Demeter.

Cons: Mediator will be huge, complicated by difficult to maintain.

Be used: 1, a reference to the presence of more complex relationships, dependencies between them leading to confusion and difficult to reuse the structure of the object between the objects in the system. 2, through a like plurality of intermediate class to encapsulate the behavior classes, and do not want to generate too many subclasses.

Note: it should not be used when duty chaos.

Memento.java

public class Memento { //创建Memento类
    private String state;
    
    public Memento(String state) {
        this.state = state;
    }
    
    public String getState() {
        return state;
    }
}

Originator.java

public class Originator { //创建Originator类
    private String state;
    
    public void setState(String state) {
        this.state = state;
    }
    
    public String getState() {
        return state;
    }
    
    public Memento saveStateToMemento() {
        return new Memento(state);
    }
    
    public void getStateFromMemento(Memento memento) {
        state = memento.getState();
    }
    
 }

CareTaker.java

import java.util.ArrayList;
import java.util.List;

public class CareTaker { //创建CareTaker类
    
    private List<Memento>mementoList = new ArrayList<Memento>();
    
    public void add(Memento state) {
        mementoList.add(state);
    }
    
    public Memento get(int index) {
        return mementoList.get(index);
    }
    
}

MementoPatternDemo.java

public class MementoPatternDemo {
    public static void main(String[] args) {
        Originator originator = new Originator();
        CareTaker careTaker   = new CareTaker();
        
        originator.setState("State #1");
        originator.setState("State #2");
        careTaker.add(originator.saveStateToMemento());
        
        originator.setState("State #3");
        careTaker.add(originator.saveStateToMemento());
        
        originator.setState("State #4");
        System.out.println("Current State: " + originator.getState());
        
        originator.getStateFromMemento(careTaker.get(0));
        System.out.println("First saved State: " + originator.getState());
        
        originator.getStateFromMemento(careTaker.get(1));
        System.out.println("Second saved State: " + originator.getState());
        
    }
}

 

Guess you like

Origin blog.csdn.net/walkerwqp/article/details/91788314