[Design Pattern]Mediator Pattern

[Design Pattern]Intermediary Pattern

1 Overview

background

Generally speaking, the relationship between colleague classes is relatively complex. When multiple colleague classes are related to each other, the relationship between them will appear as a complex network structure, which is an over-coupled architecture, that is, no It is conducive to the reuse of classes, and it is also unstable. For example, in the left figure below, there are six colleague class objects. If object 1 changes, then four objects will be affected. If object 2 changes, then 5 objects will be affected. That said, the design of direct associations between colleague classes is bad.

If the mediator mode is introduced, the relationship between the colleague classes will become a star structure. As can be seen from the figure below on the right, the change of any class will only affect the class itself and the mediator, thus reducing the system coupling. A good design must not encapsulate all the object relationship processing logic in this class, but use a special class to manage those behaviors that do not belong to itself.

definition

Also called mediation mode, it defines an intermediary role to encapsulate the interaction between a series of objects, so that the coupling between the original objects is loose, and the interaction between them can be changed independently.

structure

The mediator pattern includes the following main roles:

  • Abstract mediator (Mediator) role: it is the interface of the mediator, providing abstract methods for colleague object registration and forwarding colleague object information.

  • Concrete Mediator role: Implement the mediator interface, define a List to manage colleague objects, and coordinate the interaction between each colleague role, so it depends on the colleague role.

  • Abstract colleague class (Colleague) role: define the interface of the colleague class, save the mediator object, provide abstract methods for colleague object interaction, and implement the public functions of all interacting colleague classes.

  • The role of Concrete Colleague: It is the implementer of the abstract colleague class. When it needs to interact with other colleague objects, the mediator object is responsible for the subsequent interaction.

2. Realization ideas

Scenes

Now renting is basically done through a real estate agent, the owner entrusts the house to the real estate agent, and the renter obtains the house information from the real estate agent. Real estate agents act as intermediaries between renters and home owners.

accomplish
//abstract mediator
public abstract class Mediator {
    //declare a contact method
    public abstract void Constact(string message,Person person);
}

//abstract colleague class
public abstract class Person {
    protected string name;
    protected Mediator mediator;

    public Person(string name,Mediator mediator){
        this.name = name;
        this.mediator = mediator;
    }
}

//Specific Colleagues Home Owner
public class HouseOwner : Person {

    public HouseOwner(string name, Mediator mediator) :base(name, mediator){
    }

    //Contact an intermediary
    public void Constact(String message){
        mediator.Constact(message, this);
    }

    //getting information
    public void GetMessage(String message){
        Console.WriteLine("homeowner" + name +"Information obtained:" + message);
    }
}

//Specific colleague type Lessee
public class Tenant : Person {
    public Tenant(string name, Mediator mediator):base(name, mediator) {
    }

    //Contact an intermediary
    public void Constact(string message){
        mediator.Constact(message, this);
    }

    //getting information
    public void GetMessage(string message){
        Console.WriteLine("Renters" + name +"Information obtained:" + message);
    }
}

//Agency
public class MediatorStructure : Mediator {
    //First, the agency must know the information of all homeowners and renters
    private HouseOwner houseOwner;
    private Tenant tenant;

    public HouseOwner GetHouseOwner() {
        return houseOwner;
    }

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

    public Tenant GetTenant() {
        return tenant;
    }

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

    public void Constact(string message, Person person) {
        if (person == houseOwner) {          //If homeowner, renter gets information
            tenant.GetMessage(message);
        } else {       //It's the homeowner who gets the information anyway
            houseOwner.GetMessage(message);
        }
    }
}


   class Program
    {
        static void Main(string[] args)
        {
            //A homeowner, a renter, an agency
        	MediatorStructure mediator = new MediatorStructure();

        	//Homeowners and renters just need to know the agency
        	HouseOwner houseOwner = new HouseOwner("Zhang San", mediator);
        	Tenant tenant = new Tenant("Li Si", mediator);

        	//Intermediary structures need to know homeowners and renters
        	mediator.SetHouseOwner(houseOwner);
        	mediator.SetTenant(tenant);

        	tenant.Constact("Need to rent a three bedroom house");
        	houseOwner.Constact("I have a three-bedroom house, do you need to rent it?");
        }
    }

It can be seen from the above code that the Program class directly accesses the ProxyStation class object, that is to say, the ProxyStation acts as an intermediary between the access object and the target object. At the same time, the sell method has also been enhanced (the proxy point charges some service fees, outputs logs, etc.).

3. Advantages and disadvantages

advantage
  • loosely coupled

    The mediator pattern encapsulates the interaction between multiple colleague objects into the mediator object, so that the colleague objects are loosely coupled, and basically complementary dependencies can be achieved. In this way, colleague objects can be changed and reused independently, instead of "holding one place and moving the whole body" as before.

  • Centralized control interaction

    The interaction of multiple colleague objects is encapsulated in the mediator object for centralized management, so that when these interactive behaviors change, you only need to modify the mediator object. object, and each colleague class does not need to be modified.

  • Convert one-to-many associations to one-to-one associations

    When the mediator mode is not used, the relationship between colleague objects is usually one-to-many. After the mediator object is introduced, the relationship between the mediator object and the colleague object usually becomes two-way one-to-one, which makes the relationship between the objects Easier to understand and implement.

shortcoming
  • When there are too many colleague classes, the responsibility of the mediator will be large, and it will become complex and large, so that the system is difficult to maintain.

4. Usage scenarios

  • There are complex reference relationships between objects in the system, and the system structure is chaotic and difficult to understand.
    One-to-many, after the introduction of the mediator object, the relationship between the mediator object and the colleague object usually becomes two-way one-to-one, which makes the relationship between the objects easier to understand and implement.
shortcoming
  • When there are too many colleague classes, the responsibility of the mediator will be large, and it will become complex and large, so that the system is difficult to maintain.

4. Usage scenarios

  • There are complex reference relationships between objects in the system, and the system structure is chaotic and difficult to understand.
  • When you want to create an object that runs between multiple classes, but don't want to generate new subclasses.

Tags: C# Design Pattern

Posted by videxx on Tue, 20 Sep 2022 21:16:55 +0300