Writing in the front

  • Take notes on learning design patterns
  • Improve the flexible use of design patterns

Learning to address

www.bilibili.com/video/BV1G4…

www.bilibili.com/video/BV1Np…

Refer to the article

C.biancheng.net/view/1317.h…

Program source code

Gitee.com/zhuang-kang…

22. The Mediator model

Generally speaking, the relationship between colleague classes is quite complex. When multiple colleague classes are associated with each other, the relationship between them will present a complex network structure, which is an over-coupled architecture, which is not conducive to the reuse of classes and unstable. For example, in the lower left figure, there are six colleague class objects, and if object 1 changes, four of them will be affected. If object 2 changes, five objects will be affected. That is, the design of direct associations between colleague classes is bad.

If the intermediary pattern is introduced, the relationship between colleague classes will become a star structure. As can be seen from the figure on the lower right, the change of any class will only affect the class itself and the intermediary, thus reducing the coupling of the system. A good design does not encapsulate all object-relational logic in this class, but uses a special class to manage behavior that does not belong to it.

22.1 Definition and characteristics of the intermediary model

Definition of Mediator mode: Define a Mediator object to encapsulate the interaction between a series of objects, so that the original object coupling between the loose, and can independently change their interaction. Mediator mode is also called mediation mode, which is a typical application of Demeter’s rule.

The mediator pattern is an object behavior pattern with the following major advantages.

  1. Each class has its own job, which is Demeter’s law.
  2. The coupling between objects is reduced, making objects easy to be reused independently.
  3. The one-to-many association between objects is changed into one-to-one association, which improves the flexibility of the system and makes the system easy to maintain and expand.

The main disadvantage is that the mediator pattern turns the direct interdependence of multiple objects into a dependency between the mediator and multiple peer classes. The more colleague classes there are, the more bloated the mediator becomes, complex and difficult to maintain.

22.2 Structure and implementation of intermediary pattern

22.2.1 Structure of the mediator pattern

  • Mediator role: It is the Mediator’s interface and provides an abstract method for colleague objects to register and forward their information.
  • The ConcreteMediator role: Implements the mediator interface that defines a List to manage colleague objects and coordinate the interactions between the various colleague roles, so it relies on the colleague role.
  • Colleague roles: Define interfaces to Colleague classes, hold mediator objects, provide abstract methods for Colleague object interaction, and implement common functions of all interacting Colleague classes.
  • A Concrete Colleague role: is an implementer of an abstract Colleague class. When it is necessary to interact with other Colleague objects, the intermediary objects are responsible for subsequent interactions.

22.2.2 Code implementation

At present, most people rent their houses through real estate agents. Homeowners entrust their houses to real estate agents, and renters get housing information from real estate agents. A real estate agent acts as an intermediary between renters and homeowners

Relationship between the class diagram

Mediator

package com.zhuang.mediator;

/ * * *@Classname Mediator
 * @DescriptionAbstract mediator *@Date2021/3/28 at *@Created by dell
 */

public abstract class Mediator {
    // Declare a contact method
    public abstract void constact(String message, Person person);
}
Copy the code

Person

package com.zhuang.mediator;

/ * * *@Classname Person
 * @DescriptionAbstract colleague class *@Date2021/3/28 at *@Created by dell
 */

public abstract class Person {
    protected String name;
    protected Mediator mediator;

    public Person(String name, Mediator mediator) {
        this.name = name;
        this.mediator = mediator; }}Copy the code

HouseOwner

package com.zhuang.mediator;

/ * * *@Classname HouseOwner
 * @DescriptionSpecific colleague class homeowner *@Date2021/3/28 at *@Created by dell
 */

public class HouseOwner extends Person{

    public HouseOwner(String name, Mediator mediator) {
        super(name, mediator);
    }

    // Contact a broker
    public void constact(String message){
        mediator.constact(message,this);
    }

    // Get information
    public void getMessage(String message){
        System.out.println("Owner"+name+"Information obtained :"+message); }}Copy the code

Tenant

package com.zhuang.mediator;

/ * * *@Classname Tenant
 * @DescriptionConcrete colleague class tenant *@Date2021/3/28 who fell *@Created by dell
 */

public class Tenant extends Person{
    public Tenant(String name, Mediator mediator) {
        super(name, mediator);
    }

    // Contact a broker
    public void constact(String message){
        mediator.constact(message,this);
    }

    // Get information
    // Get information
    public void getMessage(String message){
        System.out.println("Renter"+name+"Information obtained :"+message); }}Copy the code

MediatorStructure

package com.zhuang.mediator;

/ * * *@Classname MediatorStructure
 * @DescriptionIntermediaries *@Date2021/3/28 who fell *@Created by dell
 */

public class MediatorStructure extends Mediator {

    // The agent structure must have information about all the homeowners and renters
    private HouseOwner houseOwner;
    private Tenant tenant;

    public HouseOwner getHouseOwner(a) {
        return houseOwner;
    }

    public void setHouseOwner(HouseOwner houseOwner) {
        this.houseOwner = houseOwner;
    }

    public Tenant getTenant(a) {
        return tenant;
    }

    public void setTenant(Tenant tenant) {
        this.tenant = tenant;
    }

    @Override
    public void constact(String message, Person person) {
        if (person == houseOwner) {
            // If it is the owner, the renter gets the information
            tenant.getMessage(message);
        } else {
            // Instead, the homeowner gets the informationhouseOwner.getMessage(message); }}}Copy the code

Client

package com.zhuang.mediator;

/ * * *@Classname Client
 * @DescriptionMediator pattern test classes *@Date2021/3/28 but *@Created by dell
 */

public class Client {
    public static void main(String[] args) {
        // Landlord renter agency
        MediatorStructure mediator = new MediatorStructure();

        // The owner and renter only need to know the agency
        HouseOwner houseOwner = new HouseOwner("Zhang", mediator);
        Tenant tenant = new Tenant("Bill", mediator);

        // The agency needs to know the owner and the renter
        mediator.setHouseOwner(houseOwner);
        mediator.setTenant(tenant);

        tenant.constact("Need to rent a house");
        houseOwner.constact("I have a house. Do you want to rent it??"); }}Copy the code

22.3 Application Scenarios of intermediary mode

  • When there is a complex network structure between objects, the dependency relationship is chaotic and difficult to reuse.
  • When you want to create an object that runs between classes without generating new subclasses.

22.4 Notes and details of the Intermediary model

  • Multiple classes are coupled to each other to form a network structure, which is separated into a star structure by the intermediary pattern for decoupling
  • Reduce the dependence between classes, reduce the coupling, in accordance with Demeter’s law
  • Intermediaries take on more responsibility, and if things go wrong, the system will be affected
  • Improper design, practical use, special attention

Write in the last

  • If my article is useful to you, please give me a click 👍, thank you 😊!
  • If you have any questions, please point them out in the comments section! 💪