Hello, I am Xiao Huang, a Java development engineer of Unicorn Enterprise. The school recruited dozens of offers, the annual salary is 20W~40W. Thank you for meeting us in the vast sea of people. As the saying goes: When your talent and ability are not enough to support your dream, please calm down and study. I hope you can study with me and work hard together to realize your own dream. Welcome to pay attention to my public number: “Love to knock code yellow”

What is the intermediary model

As the name suggests, the broker model is similar to the broker we rent a house in, the middleman.

In Java, there is MVC architecture. C (Controller control) is the mediator between V (View View) and M (Model Model), playing the role of middleman in the interaction between the front end and the back end.

Mediator Pattern is used to reduce the complexity of communication between multiple objects and classes. This pattern provides a mediation class that typically handles communication between different classes and supports loose coupling, making code easy to maintain.

The structure of the broker pattern is shown below:

  • Mediator: An abstract class of Mediator
  • ConcreatMediator: Implementation class for mediators
  • Colleague: An abstract class of employees
  • ConcreateColleague: Implementation class for employees

Employees do not know each other and can only send messages through mediators, the so-called Mediator model

Second, why use the intermediary model

Let’s look at this example:

In the United Nations, every country can communicate through the United Nations to maintain world peace.

What happens when the United Nations does not exist?

Every country that wants to communicate with other countries has to re-establish a line of communication. The relationship is too complicated.

If we join other countries in the future, this relationship will be more difficult to maintain, so we can adopt the intermediary mode, with the structure as follows:Everyone can contact each other through our intermediary, and when other countries get involved, they just need to associate with the intermediary, and then everyone can be connected.

Implementation of the UN code

The overall architecture is as follows:

  • Country
public class Country {
    public UnitedNations unitedNations;
	// National access intermediaries
    public Country(UnitedNations unitedNations) {
        this.unitedNations = unitedNations; }}Copy the code
  • USA
public class USA extends Country {
    public USA(UnitedNations unitedNations) {
        super(unitedNations);
    }
	// Send information to a country
    public void declare(String message, Country country) {
        unitedNations.declare(message, this, country);
    }
	// Receive information about a country
    public void getMessage(String message, Country country) {
        System.out.println("Acquired by the United States" + country + "Message sent:" + message);
    }

    @Override
    public String toString(a) {
        return "The United States." "; }}Copy the code
  • Iraq
public class Iraq extends Country {

    public Iraq(UnitedNations unitedNations) {
        super(unitedNations);
    }

    public void declare(String message, Country country) {
        unitedNations.declare(message, this, country);
    }

    public void getMessage(String message, Country country) {
        System.out.println("Acquired by Iraq" + country.toString() + "Message sent:" + message);
    }

    @Override
    public String toString(a) {
        return "Iraq"; }}Copy the code
  • UnitedNations
public abstract class UnitedNations {
    // The sent message is sent to the received country
    public abstract void declare(String message, Country sendcountry, Country acceptCountry);
}

Copy the code
  • UnitedNationsSecurityCouncil
public class UnitedNationsSecurityCouncil extends UnitedNations {
    public USA usa;
    public Iraq iraq;

    public void setUsa(USA usa) {
        this.usa = usa;
    }


    public void setIraq(Iraq iraq) {
        this.iraq = iraq;
    }

    @Override
    public void declare(String message, Country sendcountry, Country acceptCountry) {
        // The recipient is Iraq
        if (sendcountry == usa && acceptCountry == iraq) {
            iraq.getMessage(message, sendcountry);
        } else {
        	// The recipient is the United Statesusa.getMessage(message, sendcountry); }}}Copy the code
  • The test class
public class Main {
    public static void main(String[] args) {
        // Create an intermediary
        UnitedNationsSecurityCouncil unitedNationsSecurityCouncil = new UnitedNationsSecurityCouncil();

        // Every country has access to intermediaries
        USA usa = new USA(unitedNationsSecurityCouncil);
        Iraq iraq = new Iraq(unitedNationsSecurityCouncil);

        // Intermediaries access each country
        unitedNationsSecurityCouncil.setIraq(iraq);
        unitedNationsSecurityCouncil.setUsa(usa);

        // The state sends the message
        usa.declare("The United States will not use nuclear weapons.", iraq);
        iraq.declare("Iraq will not use nuclear weapons.", usa);

		// Iraq received a message from the United States that the United States would never use nuclear weapons
		// The United States received a message from Iraq that It was determined not to use nuclear weapons}}Copy the code

Four,

We can see that the intermediary model can be used to well decouple the relationship between various countries.

The problem with the intermediary model, as the saying goes, is that all countries rely on intermediaries, and the logic of intermediaries is extremely complex.

We use the mediator pattern in the following scenarios:

  • Airport aircraft scheduling strategy, housing rental
  • MVC architecture
  • The UN /WTO acts as an intermediary to coordinate countries

So when there are complex references between objects in our system and we want to encapsulate the behavior of multiple classes with an intermediate class without generating too many subclasses, we can use the mediator pattern.

The content of this issue is over here, I am a Unicorn enterprise Java development engineer, you can leave a message or private message to add my wechat, we will see you next time!