Design Patterns Mediator Pattern Notes

illustrate

Record the writing method of learning design pattern-mediator pattern. The JDK version used is version 1.8.

Mediator

Intent : Use a mediating object to encapsulate a series of object interactions. Mediators remove the need for objects to explicitly reference each other, so they are loosely coupled and can independently change their interactions.
Structure :
Insert image description here

in:

  • Mediator defines an interface for communication between Colleague objects.
  • ConcreteMediator (concrete mediator) achieves collaborative behavior by coordinating each colleague object; understands and maintains its colleagues.
  • ConcreteSubject (specific target) stores the relevant status in each ConcreteObserver object; when its status changes, it sends a notification to each of its observers.
  • A Colleague class knows its mediator objects; each Colleague class object communicates with its mediator when it needs to communicate with other colleagues.

applicability:

  • A set of objects communicate in a well-defined but complex way, resulting in interdependencies that are confusingly structured and difficult to understand.
  • An object references many other objects and communicates directly with these objects, making it difficult to reuse the object.
  • I want to customize a behavior that is distributed among multiple classes without generating too many subclasses.

Table of contents

Insert image description here

Mediator pattern example class diagram

Insert image description here
Use this UML class diagram to implement the mediator pattern example.

abstract mediator class

package com.example.deesign_patterns.mediator;

//抽象中介者类
public abstract class Mediator {
    
    

    //联系方法
    public abstract void contact(String message,Person person);
}

abstract colleague class

package com.example.deesign_patterns.mediator;

//抽象同事类
public abstract class Person {
    
    

    protected String name;
    protected Mediator mediator;

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

Renters

package com.example.deesign_patterns.mediator;

//租房者类,具体的同事角色类
public class Tenant extends Person{
    
    

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

    //和中介联系(沟通)的方法
    public void contact(String message){
    
    
        mediator.contact(message,this);
    }

    //获取信息的方法
    public void getMessage(String message){
    
    
        System.out.println("租房者"+name+"获取到的信息是:"+message);
    }
}

Homeowner

package com.example.deesign_patterns.mediator;

//房主类,具体的同事角色类
public class HouseOwner extends Person{
    
    

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

    //和中介联系(沟通)的方法
    public void contact(String message){
    
    
        mediator.contact(message,this);
    }

    //获取信息的方法
    public void getMessage(String message){
    
    
        System.out.println("房主"+name+"获取到的信息是:"+message);
    }
}

Specific mediator role classes

package com.example.deesign_patterns.mediator;

//具体的中介者角色类
public class MediatorStructure extends Mediator{
    
    

    //聚合租房者和房主对象
    private Tenant tenant;
    private HouseOwner houseOwner;

    public Tenant getTenant() {
    
    
        return tenant;
    }

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

    public HouseOwner getHouseOwner() {
    
    
        return houseOwner;
    }

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

    @Override
    public void contact(String message, Person person) {
    
    
        if(person==houseOwner){
    
    
            tenant.getMessage(message);
        }else {
    
    
            houseOwner.getMessage(message);
        }
    }
}

Test class

package com.example.deesign_patterns.mediator;

//测试类
public class Client {
    
    

    public static void main(String[] args) {
    
    
        //创建中介者对象
        MediatorStructure mediator=new MediatorStructure();
        //创建租房者对象
        Tenant tenant=new Tenant("李四",mediator);
        //创建房主对象
        HouseOwner houseOwner=new HouseOwner("张三",mediator);
        //中介者要知道具体的房主和租房者
        mediator.setTenant(tenant);
        mediator.setHouseOwner(houseOwner);
        tenant.contact("我要租三室的房子!!!");
        houseOwner.contact("我这里有三室的房子,你要租吗?");
    }
}

Insert image description here

benefit:

  • Loosely coupled. The mediator pattern encapsulates the interactions between multiple colleague objects into the mediator object, thereby loosely coupling the colleague objects and basically achieving complementary dependence. In this way, colleague objects can be changed and reused independently, instead of "moving one place and affecting the whole body" as before.
  • Centrally control interactions. 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. Of course, if it is an already completed system, then extend the mediator Object, and each colleague class does not need to be modified.
  • A one-to-many association is converted into a one-to-one association. When the mediator pattern 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 a two-way one-to-one relationship, which makes the relationship between the objects Easier to understand and implement.

shortcoming:

  • When there are too many co-classes, the mediator's responsibilities will be huge, and it will become complex and large, making the system difficult to maintain.

Guess you like

Origin blog.csdn.net/weixin_48040732/article/details/131363754