preface
The intermediary mode, which is easy to use, essentially encapsulates the details of the interaction between different objects, rather than directly interacting, and takes on a lot of responsibility, but the communication efficiency actually gets worse.
directory
A, definitions,
Encapsulating a series of object interactions with a mediation object, the mediator makes them loosely coupled and can change their interactions independently by making them interact without needing to appear.
Two, mode principle analysis
Public Abstract class Mediator{// define the public ConcreteComponent1 c1; protected ConcreteComponent2 c2; public ConcreteComponent1 getC1(){ return c1; } public ConcreteComponent2 getC2(){ return c2; } public void setC1(ConcreteComponent1 c1){ this.c1 = c1; } public void setC2(ConcreteComponent2 c2){ this.c2 = c2; } public abstract void doSomething1(); public abstract void doSomething2(); } Public class ConcreteMediator extends Mediator{@override public void doSomething1(){public void doSomething1() super.c1.selfMethod1(); super.c2.selfMethod2(); } public void doSomething2(){ super.c1.selfMethod1(); super.c2.selfMethod2(); }} public abstract class Component{public abstract class Component; piublic Component(Mediator _mediator){ this.mediator = _mediator; }} public class ConcreteComponent1 extends Component{public ConcreteComponent1(ConcreteComponent1){public ConcreteComponent1(ConcreteComponent1){ super(_mediator); } public void selfMethod1 () {/ / your business logic} public void depMethod1 () {/ / own business logic super. The mediator. DoSomething1 (); }}Copy the code
As you can see from the template code, the intermediary object is designed to handle the direct interaction between objects, encapsulating the details of the interaction between multiple objects.
Take, for example, the ability to send messages in chat rooms
Public interface ChatRoom {void sendMessage(String MSG, String userId); void addUser(User user); Public class ChatRoomImpl implements ChatRoom {private Map<String, User> usersMap = new HashMap<>(); @Override public void sendMessage(String msg, String userId) { User u = usersMap.get(userId); u.receive(msg); } @Override public void addUser(User user) { this.usersMap.put(user.getId(), user); Public abstract class User {private ChatRoom mediator; private String id; private String name; public User(ChatRoom room, String id, String name){ this.mediator = room; this.name = name; this.id = id; } public abstract void send(String msg, String userId); public abstract void receive(String msg); public ChatRoom getMediator() { return mediator; } public String getId() { return id; } public String getName() { return name; Public class ChatUser extends User {public ChatUser(ChatRoom room, String ID, String name) {super(room, String id, String name) id, name); } @Override public void send(String msg, String userId) { System.out.println(this.getName() + " :: Sending Message : " + msg); getMediator().sendMessage(msg, userId); } @Override public void receive(String msg) { System.out.println(this.getName() + " :: Received Message : " + msg); }}Copy the code
Iii. Usage Scenarios
-
When a spider web is present in a class diagram, consider using the mediator pattern to tease out a star structure
-
Complex reference relationships exist between objects in the system
-
When common behavior in multiple classes is encapsulated by an intermediate object
Four advantages,
-
Reduce the direct interaction between objects and indirectly decouple excessive dependencies
-
Reduce the number of subclasses created and simplify the design and implementation of the system
-
Through the middle layer, you can realize fast expansion and improve the code extensibility
Five, the disadvantages
-
The interaction logic of the middle tier, the broker class, can be complex and difficult to maintain later
-
The intermediaries evolved with new, heavily dependent objects
-
Intermediaries need to know the logic of all object interactions