Today, let’s take a look at the intermediary pattern, hoping that through the explanation of this article, you can have a deeper understanding of this pattern.

Basic introduction

The mediator pattern is one of the behavioral design patterns.

The core idea of this pattern is to encapsulate a series of object interactions through a mediation class, so that there is no need to display the mutual reference between objects, so as to reduce the degree of coupling between objects.

Take a chestnut

Xiao Ming wants to rent a house, but he uses the most primitive method to find the most suitable house.

It took a lot of turning around to find a suitable place to rent (without using the broker model).

Rookie also want to rent a house, but rookie smart ah certainly will not be their blind turn, directly found an intermediary.

Through the intermediary, easy to solve the rental problem. This is the embodiment of the mediator pattern, where a mediation class encapsulates and interacts with other classes.

The mediator pattern UML class diagram

UML class diagram explanation

Mediator: An abstract Mediator that defines methods for registering and interacting with colleague objects. ConcreteMediator: Concrete intermediaries that implement methods defined in abstract intermediaries. Defines a collection to manage colleague objects. Colleague: An abstract Colleague class that aggregates abstract intermediary objects. Defines the public methods required by all concrete colleague classes. ConcreteColleagueA/B: Concrete colleague class that implements methods defined in the abstract class.Copy the code

Case on

Case study: Explain the intermediary model through the example of finding an intermediary to rent a house.

Owner => Corresponds to abstract colleague class

public abstract class Homeowner {

  // Aggregate broker
  private Mediator mediator;

  public Homeowner(Mediator mediator) {
    this.mediator = mediator;
  }

  public abstract void signContract(a);

  public Mediator getMediator(a) {
    returnmediator; }}Copy the code

Specific Colleagues

/** * basement */
public class BasementHomeowner extends Homeowner {

  // constructor
  public BasementHomeowner(Mediator mediator) {
    super(mediator);
    this.getMediator().Register("Basement".this);
  }

  @Override
  public void signContract(a) {
    System.out.println("Welcome to the basement, only 500 yuan per month!"); }}/** ** ** */
public class VillaHomeowner extends Homeowner {

  public VillaHomeowner(Mediator mediator) {
    super(mediator);
    this.getMediator().Register("Villa".this);
  }

  @Override
  public void signContract(a) {
    System.out.println("Welcome to luxury villa, only 5000 yuan per month!"); }}Copy the code

Abstract mediator

public interface Mediator {

  // Register a colleague object
  void Register(String name, Homeowner homeowner);

  // Receive the message
  void receiveMessage(int price);
}
Copy the code

Specific intermediaries

public class ConcreteMediator implements Mediator {

  private HashMap<String, Homeowner> homeownerMap;

  public ConcreteMediator(a) {
    this.homeownerMap = new HashMap<>();
  }

  @Override
  public void Register(String name, Homeowner homeowner) {
    homeownerMap.put(name, homeowner);
  }

  @Override
  public void receiveMessage(int price) {
    switch (price) {
      case 500:
        homeownerMap.get("Basement").signContract();
        break;
      case 5000:
        homeownerMap.get("Villa").signContract();
        break;
      default:
        System.out.println("You don't have what you want, go somewhere else!");
        break; }}}Copy the code

Client test classes

public class Client {

  public static void main(String[] args) {
    // Create an intermediary
    ConcreteMediator concreteMediator = new ConcreteMediator();

    // Create a specific owner
    BasementHomeowner basementHomeowner = new BasementHomeowner(concreteMediator);
    VillaHomeowner villaHomeowner = new VillaHomeowner(concreteMediator);

    concreteMediator.receiveMessage(5000); }}Copy the code

The execution result

conclusion

1. The mediator pattern can be used to transform the many-to-many relationship between classes into a one-to-many relationship, reducing the complexity of classes.

2. The intermediary mode can reduce the dependency between classes and reduce the degree of coupling.

3. The intermediary class undertakes more responsibilities in this mode, and the whole system will be affected once an error occurs.

4. The intermediary class will grow larger as the number of colleague classes increases and the complexity of the business increases, thus increasing the difficulty of maintenance.

This is the end of today’s sharing, if you feel that the article written by “rookie” is still good, remember to like and pay attention to yo! Your support is what keeps me going. Article where to write problems also hope that you can point out, I will be modestly taught.