Java design patterns described in (17): mediator mode

This article Source: GitHub · Click here || GitEE · Click here

A living scene

1, scene description

In the company's daily schedule usually divided into a number of departments, each department will be divided into different groups, a core job is to coordinate the work between the team manager of the department, such as the needs of the development team, product team, team unified summary to the manager, who unified arrangement and coordination.

2, illustrating scenes

3, code implementation

public class C01_InScene {
    public static void main(String[] args) {
        Manager manager = new Manager() ;
        EmployeeA employeeA = new EmployeeA("张三",manager) ;
        EmployeeB employeeB = new EmployeeB("李四",manager) ;
        employeeA.sendMsg(employeeA.name,"需要产品文档",employeeB);
    }
}
/**
 * 部门协调接口
 */
interface Department {
    void coordinate (String userName,String msg,Employee employee) ;
}
/**
 * 部门经理
 */
class Manager implements Department {
    @Override
    public void coordinate (String userName,String msg,Employee employee) {
        System.out.println("经理接收【"+userName+"】的协调任务:" + msg);
        System.out.println("经理转发【"+userName+"】协调任务,@【"+employee.name+"】");
        employee.getMsg(userName,msg);
    }
}
/**
 * 员工抽象类
 */
abstract class Employee {
    public String name ;
    private Department department ;
    public Employee (String name,Department department){
        this.name = name ;
        this.department = department ;
    }
    public void getMsg (String userName,String msg){
        System.out.println("【"+this.name+"】收到"+"【"+userName+"】协调任务:["+msg+"]");
    }
    public void sendMsg (String name,String msg,Employee employee){
        System.out.println("【"+name+"】发起协调任务:"+ msg);
        department.coordinate(name,msg,employee);
    }
}
/**
 * 具体员工
 */
class EmployeeA extends Employee {
    public EmployeeA(String name, Department department) {
        super(name, department);
    }
}
class EmployeeB extends Employee {
    public EmployeeB(String name, Department department) {
        super(name, department);
    }
}

Results of the

【张三】发起协调任务:需要产品文档
经理接收【张三】的协调任务:需要产品文档
经理转发【张三】协调任务,@【李四】
【李四】收到【张三】协调任务:[需要产品文档]

Second, the mediator model

1, the basic concept

Mediator pattern is a pattern of behavior of the object. Packaging way mediator pattern set of objects interacting with each other such that the objects do not have an explicit reference, referenced object is notified through an intermediary, such decoupling between objects. When the interaction between some of the objects of these objects change, will not immediately affect the interaction between other objects. Ensure that these interactions can be varied independently of each other.

2, illustrates a pattern

3, the core role

  • Abstract mediator role

Colleagues objects to define an interface mediator object, which is the main method of one or more events method.

  • Specific mediator role

Event method to achieve the abstract mediator declared. Specific mediator holds all of the specific class colleagues, exchange information and coordinate the various specific colleague object.

  • Abstract colleague Role

Define the interfaces to colleagues mediator object. Colleagues object holds only mediator not know the specific objects colleagues.

  • Colleagues role-specific

All classes are inherited specific colleague colleagues from the abstract class, you need to communicate with other colleagues when it held with the communication mediator, the mediator will be responsible for interacting with other objects colleagues.

4, source code implementation

/**
 * 抽象调停者类
 */
interface Mediator {
    void notify (Colleague colleague);
}
/**
 * 抽象同事类
 */
abstract class Colleague {
    /* 持有调停者对象 */
    private Mediator mediator;
    public Colleague(Mediator mediator){
        this.mediator = mediator;
    }
    public Mediator getMediator(){
        return mediator;
    }
}
/**
 * 具体调停者类
 */
class ConcreteMediator implements Mediator{
    private ConcreteColleagueA colleagueA ;
    private ConcreteColleagueB colleagueB ;
    public void setColleagueA(ConcreteColleagueA colleagueA) {
        this.colleagueA = colleagueA;
    }
    public void setColleagueB(ConcreteColleagueB colleagueB) {
        this.colleagueB = colleagueB;
    }
    @Override
    public void notify(Colleague colleague) {
        System.out.println("协调通知消息");
    }
}
/**
 * 具体同事类
 */
class ConcreteColleagueA extends Colleague{
    public ConcreteColleagueA(Mediator mediator) {
        super(mediator);
    }
    public void operate(){
        getMediator().notify(this);
    }
}
class ConcreteColleagueB extends Colleague{
    public ConcreteColleagueB(Mediator mediator) {
        super(mediator);
    }
    public void operation(){
        getMediator().notify(this);
    }
}

Third, the mediator pattern summary

  • Loose coupling

Mediator pattern by interaction between the plurality of packaged objects to colleagues mediator inside the object, so that the loose coupling between objects colleagues.

  • Centralized management interaction

Colleagues interactive multiple objects, the object is encapsulated inside the mediator centralized management, so that these interactions change when the mediator only need to modify objects on it.

  • Simplify the relationship between objects

After not using the mediator pattern when the relationship between colleagues is often the object of many to many, the introduction of a mediator objects, subjects and colleagues relations mediator object usually becomes a two-way-to-many.

  • Shortcoming mode

One potential drawback is the mediator pattern, excessive centralization, when many of my colleagues object very interactive, while complex, can lead mediator objects become very complex and difficult to manage and maintain.

Fourth, the source address

GitHub·地址
https://github.com/cicadasmile/model-arithmetic-parent
GitEE·地址
https://gitee.com/cicadasmile/model-arithmetic-parent

Guess you like

Origin www.cnblogs.com/cicada-smile/p/11838353.html