Definition: used to reduce the complexity of communication between multiple objects and classes. The mediator pattern belongs to the behavioral pattern.
Features:
1. Reduce the complexity of the class and convert one-to-many into one-to-one.
2. Decoupling between various classes.
3. Comply with the Dimit principle.
Applications in enterprise-level development and common frameworks: C in mvc mode
Specific examples:
public class Demo { public static void main(String[] args) { Mediator m = new Mediator(); Department d1 = new Department(m, "Development Department"); Department d2 = new Department(m, "Financial Department"); Department d3 = new Department(m, "Planning Department"); m.add(d1, "dep"); m.add(d2, "fin"); m.add(d3, "c"); d2.apply("There is a project in hand to be developed", "dep"); d1.apply("The development project requires funds", "fin"); d1.apply("The project has been developed and needs to be planned and published", "c"); d3.apply("The press conference requires funds", "fin"); }}class Department{ private String name; private Mediator mediator; public Department(Mediator mediator,String name) { this.name = name; this.mediator = mediator; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void apply(String thing,String coordinate){ System.out.println("We are "+this.getName()+","+thing+", need help"); mediator.dispatch(this, thing, coordinate); } public void handle(String name,String thing){ System.out.println("We are "+this.getName()+","+name+" Need our help, we will handle their affairs immediately"); System.out.println("************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************ *Intermediary object, holds all colleague objects, and is responsible for scheduling coordination work of various departments*/class Mediator{ private String name; private Map<String, Department> map = new HashMap<String, Department>(); public void add(Department d,String name){ this.map.put(name, d); } public void dispatch(Department dpt,String thing,String coordinate){ Department d = map.get(coordinate); d.handle(dpt.getName(),thing); }}The difference between the intermediary mode and the proxy mode: When I first heard about the modes of these two modes, I felt that these two modes seemed to be the same mode, but these two modes were completely different. The proxy mode was mainly used to hide actual objects and prevent others from knowing the specific operation details. The intermediary mode was mainly used to complex interactions of multiple objects with multiple objects. In order to simplify these interactions, the terminator mode was found.