This is the 21st day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Author’s other platforms:
| CSDN:blog.csdn.net/qq\_4115394…
| the nuggets: juejin. Cn/user / 651387…
| zhihu: www.zhihu.com/people/1024…
| GitHub:github.com/JiangXia-10…
| public no. : 1024 notes
This article is about 2,124 words and will take about 8 minutes to read
define
Mediator Pattern is also called Mediator Pattern. It defines a mediation object that encapsulates the interaction between a series of objects. Mediators reduce coupling by eliminating the need for objects to refer explicitly to each other, and can independently change their interaction behavior. It is an object behavior pattern.
The most common example in life is that the rental agency is a kind of intermediary. If we want to rent a house, we can communicate with the landlord through the intermediary. At this time, we do not need to know who the other party is, nor do we need to face to face.
Part of the
The broker pattern has three components:
1. Mediator: Abstract Mediator is an abstract class of Mediator. It provides abstract methods for registering and forwarding information of colleague objects, and is used for communication between various colleague classes. Usually includes one or more abstract event methods and is implemented by subclasses.
2. Concrete Mediator: Concrete Mediator. Inherits the abstract mediator and implements the event methods defined in the abstract mediator. Receives a message from a colleague class, and then affects the other concurrent classes through the message. It relies on the colleague role because it manages the colleague object and coordinates the interactions between the various colleague roles.
An object that affects and is also affected by other objects is called a Colleague class. Colleague classes are also composed of two parts: Abstract Colleague and Concrete Colleague. The abstract colleague class defines the interface of the colleague class, holds the intermediary object, provides the abstract method of the interaction of the colleague object, and implements all the common functions of the interacting colleague class. The concrete colleague class is the implementer of the abstract colleague class, and the intermediary object is responsible for the subsequent interaction when it needs to interact with other colleague objects.
chestnuts
Abstract mediator
public abstract class Mediator {
public abstract void look(Colleague cl);
public abstract void regist(Colleague cl);
}
Copy the code
Abstract colleague class:
public abstract class Colleague {
protected Mediator mediator;
public void setMedium(Mediator mediator) {
this.mediator = mediator;
}
public abstract void get();
public abstract void send();
}
Copy the code
The concrete mediation class inherits the abstract mediation class:
Public class ConcreteMediator extends Mediator {//list <Colleague> colleagues = new ArrayList<Colleague>(); @Override public void look(Colleague cl) { if (! colleagues.contains(cl)) { colleagues.add(cl); cl.setMedium(this); } } @Override public void regist(Colleague cl) { for (Colleague ob : colleagues) { if (! ob.equals(cl)) { ((Colleague) ob).get(); }}}}Copy the code
The concrete colleague class inherits the abstract colleague class:
Public class ConcreteColleague1 extends Colleague{@override public void get() {system.out.println (" ConcreteColleague1 extends Colleague () {@override public void get() {system.out.println (). ); } @override public void send() {system.out.println (); ); mediator.look(this); }}Copy the code
Public class ConcreteColleague2 extends Colleague{@override public void get() {system.out.println (" ConcreteColleague2 extends Colleague () {@override public void get() {system.out.println (). ); } @override public void send() {system.out.println (); ); mediator.look(this); }}Copy the code
Test method:
public class MediatorPatternTest { public static void main(String[] args) { Mediator md = new ConcreteMediator(); Colleague c1, c2; c1 = new ConcreteColleague1(); c2 = new ConcreteColleague2(); md.look(c1); md.look(c2); c1.send(); c2.send(); }}Copy the code
The running results are as follows:
Through the above code can be found that the mediator pattern is the most complicated the relationship between the colleague class, several colleagues related association between classes, influence each other, if it is multiple classes (> 2) the relationship between them is a complex network structure, so it is a kind of excessive coupling architecture, such a system is definitely not good. In this way, the network of over-coupling becomes a diffuse star structure by introducing mediators so that peer objects only interact with the mediator (imagine that peer objects spread out with the mediator as the core), which is a decoupled process. The intermediary can be understood as the neighborhood committee aunt, no matter what the community has, go to the neighborhood committee aunt, she will solve for you.
Advantages of the mediator pattern
1. Using the mediator pattern can avoid excessive coupling between colleague classes, so that each colleague class can be used relatively independently.
2. Using the mediator pattern, the one-to-many association between objects can be transformed into one-to-one association, making the relationship between objects easy to understand and maintain.
3. The intermediary pattern can abstract the behavior and collaboration of objects, and can deal with the interaction between objects flexibly.
Disadvantages of the mediator pattern
The disadvantages of the intermediary model are obvious and few, including:
1. The specific intermediary class contains the interaction details between colleagues, which may make the specific intermediary class very complex;
2. The mediator pattern transforms the direct interdependence of multiple objects into a dependency between the mediator and multiple peer classes. The system is difficult to maintain. As the number of colleague classes increases, the mediator becomes bloated, complex and difficult to maintain.
Application scenarios
The application scenario of the mediator pattern is its definition. If a class depends on multiple classes at the same time, the appropriate use of the mediator pattern can clarify and decouple an otherwise chaotic object relationship.
A common application is in the MVC pattern, where the Controller Controller is a mediator object. Both Model and View use it to communicate.
conclusion
The mediator pattern is a common pattern and its essence is to decouple the interaction between multiple colleague objects. Each object holds a reference to the mediator object and deals only with the mediator object. These interactions are managed uniformly through intermediary objects. So if the relationship between objects does not reach a complex chaotic relationship, there is no need to use the intermediary pattern, just through dependency management, if the use of the intermediary pattern is not appropriate, it will make the program more chaotic.
This article and all of the previous design pattern examples will be synchronized to Github, if you need to download star!
Github address: github.com/JiangXia-10…
Related recommendations:
Learning Design Patterns from Scratch part 1: What are Design Patterns
Learning design Patterns from scratch (PART 2) : Singleton patterns
Learning design Patterns from Scratch (3)
Learning design Patterns from Scratch (4) : Factory Pattern
Learning Design Pattern from Scratch (5) : Builder Pattern
Learning design Patterns from Scratch (6) : Adapter Pattern
Learn design Pattern from scratch (6) : Proxy Pattern
Decorator Pattern ()
Learning design Patterns from Scratch (9) : Facade Pattern
Learning design Patterns from Scratch (10) : Bridge Pattern
Learning Design Patterns from Scratch (11) : Composite Pattern
Learn design Pattern from scratch (12) : Flyweight Pattern
Learning Design Patterns from Scratch (13) : Visitor Pattern