Design pattern: Intermediary pattern

Keywords: Java

Intermediary is a very common word in real life. We buy houses and rent houses to find an intermediary. Through the intermediary, we complete the purchase and sale of houses. The buyer and the seller directly communicate with the intermediary to complete the transaction contract.

Real estate agents, third party platforms, Alipay and so on are intermediaries. They act as an intermediary in the middle and undertake the business processes of both parties.

Core: intermediary object, which simplifies some complex business processing and makes each business processing object directly deal with the intermediary.

Example: simulate company business processing. The general manager acts as an intermediary and is responsible for coordinating all departments.

/**
 * Mediator abstract interface
 */
public interface Mediator {
    //Management object
    void register(String name, Department departMent);
    //Calling object
    void command(String something);
}

  

/**
 * Department internal object interface
 */
public interface Department {
    //Complete department tasks
    void doSomeThing();
    //Report to the intermediary and inform the intermediary to call
    void apply();
}

  

public class FinacialDepartment implements Department {

    //Holding intermediary object
    private Mediator mediator;

    public FinacialDepartment(Mediator mediator) {
        this.mediator = mediator;
        mediator.register("finacial", this);
    }

    @Override
    public void doSomeThing() {
        System.out.println("Work of financial department");
    }

    @Override
    public void apply() {
        System.out.println("Financial reporting");
    }
}

  

/**
 * R & D department
 */
public class Development implements Department {

    //Holding intermediary object
    private Mediator mediator;

    public Development(Mediator mediator) {
        this.mediator = mediator;
        mediator.register("development", this);
    }

    @Override
    public void doSomeThing() {
        System.out.println("R & D department work");
    }

    @Override
    public void apply() {
        System.out.println("The R & D department's reporting needs the support of the financial department.");
        //Notify mediators, call other object methods
        mediator.command("finacial");

    }
}

  

/**
 * Specific intermediary
 */
public class Manager implements Mediator {

    private Map<String, Department> map = new HashMap<>();

    @Override
    public void register(String name, Department departMent) {
        map.put(name, departMent);
    }

    @Override
    public void command(String something) {
        map.get(something).doSomeThing();
    }
}

  

public class Client {
    public static void main(String[] args) {
        //tertium quid
        Mediator manager = new Manager();
        //All department objects and intermediaries need to be connected to specific objects
        Department devop = new Development(manager);
        Department finaical = new FinacialDepartment(manager);
    
        devop.doSomeThing();
        //Through the application to the intermediary, the specific object can be called indirectly
        devop.apply();
    }
}

In this simple example, the mediator Manager decouples the interaction between departments. Each object holds the reference of the mediator object and only deals with the mediator object.

It simplifies the relationship between objects, encapsulates the relationship among objects of the system, decouples the colleague classes, and makes the system become loose coupling.

This pattern can be considered when there is a need to coordinate the relationship of multiple objects in business processing without adding subclass method processing.

Posted by ThisIsMyName on Wed, 04 Dec 2019 12:06:54 -0800