The mediator pattern

Definition 1.

Using a mediation object to encapsulate a series of object interactions, the mediator uses objects without explicit interactions, thereby effectively reducing coupling and independently changing the interactions between encapsulated columns of objects.

The mediator mode is also called the mediator mode: the dependency between objects is like war, it is very chaotic, join a center, all objects communicate with the center, accept the mediation of the center.

The mediator pattern typically has four roles

AbstractColleague class: Abstract the mediator role. Specify a mediator for communication with other colleagues.

public abstract class AbstractColleague {

    protected AbstractMediator mediator;

    public AbstractColleague(AbstractMediator mediator) {
        this.mediator = mediator; }}Copy the code

AbstractMediator Abstract class: Abstract the mediator role. Define a unified interface for communication between colleague roles.

public abstract class AbstractMediator {

    protected ColleagueOne colleagueOne;

    protected ColleagueTwo colleagueTwo;

    public ColleagueOne getColleagueOne(a) {
        return colleagueOne;
    }

    public ColleagueTwo getColleagueTwo(a) {
        return colleagueTwo;
    }

    public void setColleagueOne(ColleagueOne colleagueOne) {
        this.colleagueOne = colleagueOne;
    }

    public void setColleagueTwo(ColleagueTwo colleagueTwo) {
        this.colleagueTwo = colleagueTwo;
    }

    public abstract void executeOne(a);

    public abstract void executeTwo(a);
}
Copy the code

Colleague: Colleague roles. Each colleague role knows the role of mediator, and the behavior of each colleague role can be divided into two types: one is its own behavior, which is used to change its own state or deal with its own business logic, and has no dependence on other colleagues and intermediaries, which is called spontaneous behavior. The other is the behavior that must depend on the intermediary to complete, called the dependency method.

public class ColleagueOne extends AbstractColleague {

    public ColleagueOne(AbstractMediator mediator) {
        super(mediator);
    }

    /** * spontaneous behavior */
    public void selfMethodOne(a) {
        // Handle your own business logic
        System.out.println("ColleagueOne's own method.");
    }

    /** * dependent behavior */
    public void depMethodOne(a) {
        // Handle your own business logic
        System.out.println("ColleagueOne's dependency approach.");

        // Delegate processing to an intermediary
        super.mediator.executeOne(); }}public class ColleagueTwo extends AbstractColleague {

    public ColleagueTwo(AbstractMediator mediator) {
        super(mediator);
    }

    /** * spontaneous behavior */
    public void selfMethodTwo(a) {
        // Handle your own business logic
        System.out.println("ColleagueTwo's own way.");
    }

    /** * dependent behavior */
    public void depMethodTwo(a) {
        // Handle your own business logic
        System.out.println("ColleagueTwo's dependency approach");

        // Delegate processing to an intermediary
        super.mediator.executeTwo(); }}Copy the code

Mediator Class: The general Mediator role. Coordinate colleague roles to achieve collaborative behavior, you must rely on individual colleague roles.

public class Mediator extends AbstractMediator {

    @Override
    public void executeOne(a) {
        super.colleagueOne.selfMethodOne();
        super.colleagueTwo.selfMethodTwo();
    }

    @Override
    public void executeTwo(a) {
        super.colleagueTwo.selfMethodTwo();
        super.colleagueOne.selfMethodOne(); }}Copy the code

Scene: the class

public class Client {

    public static void main(String[] args) {
        // Define the broker
        AbstractMediator mediator = new Mediator();

        // Define the colleague class
        ColleagueOne colleagueOne = new ColleagueOne(mediator);
        ColleagueTwo colleagueTwo = new ColleagueTwo(mediator);

        // The mediator injects the colleague classmediator.setColleagueOne(colleagueOne); mediator.setColleagueTwo(colleagueTwo); colleagueOne.depMethodOne(); colleagueTwo.depMethodTwo(); }}Copy the code

Application 2.

2.1 the advantages

  • Reduce dependencies between classes and reduce coupling between classes

2.2 disadvantages

  • Intermediaries can be bloated and logically complex

2.3 Application Scenarios

In object-oriented programming, there will inevitably be dependencies between objects and objects, the existence is reasonable, not necessarily dependency, use the mediator mode. The mediator pattern applies to situations where there is tight coupling between multiple objects, and spider webs appear in class diagrams. Using the intermediary pattern helps to tease out the spider web into a star structure.

  • Dispatch center
  • Media gateway
  • Intermediary service