Writing in the front

Demeter’s rule

Only talk to your immediate friends, not “strangers.” The implication is that if two software entities do not need to communicate directly, then direct calls to each other should not occur and can be forwarded by a third party. The purpose is to reduce the coupling between classes and improve the relative independence of modules directly.

【 Behavioral 】 Mediator mode

The mediator pattern definition: Defines a mediation object that encapsulates the interaction of a set of objects, loosens the coupling between the original objects, and can change their interaction independently. The mediator mode is also called mediation mode

If the application, a typical example: message queuing. Yes, the famous message queue is the classic intermediary model

The model structure

  1. Mediator abstract
  2. Concrete Mediator
  3. An abstract Colleague is a Colleague
  4. Concrete Colleague

The structure of the mode mentioned above may be a little abstract. Specifically speaking, the contact or communication between each colleague class is not directly communicated, but forwarded and processed by the intermediary. The intermediary mode is also called the mediator mode, which can be understood as mediating the collaboration and communication between various classes.

The mediator pattern is a behavioral pattern, and here’s a review of the definition of a behavioral pattern: it describes how classes or objects work together to accomplish tasks that individual classes cannot accomplish, or defines how responsibilities are assigned.

Questions worth mentioning

Often we put Facade/Facade patterns together with mediators, and sometimes it even feels like they are confused, as if they are both using an intermediary to provide the service.

In fact, the difference between mediators is quite obvious. First of all, remember that Mediators treat all colleague classes equally and at the same level. It handles complex interactions between colleague classes, similar to star-shaped structures. Facade is different from Facade, which aggregates subsystems to provide unified services and exposes them to the outside. It is similar to umbrella structure. Students who do server-side development should be familiar with the layer-layer like Dao Service. A Service is a Facade that aggregates functionality to provide a unified Service to a Controller at a higher level. Ah, design patterns are all around us!

Implement case

So show me the Code phase

import org.apache.commons.lang3.StringUtils; import org.assertj.core.util.Lists; import java.util.List; /** * @program: dying-stranded * @description: wangzibin **/ public class MediatorDemo { static abstract class Mediator { public abstract void register(Client client);  public abstract void sendMessage(Client sender, String msg); } static class AllSendMediator extends Mediator { private static final List<Client> clientList = Lists.newArrayList(); @Override public void register(Client client) { clientList.add(client); client.mediator = this; If (stringutils.isblank (client.name)) {client.name = "client :" + system.currentTimemillis (); } } @Override public void sendMessage(Client sender, String msg) { for (Client client : clientList) { if (client ! = sender) { client.receive(sender, msg); } } } } static abstract class Client { protected Mediator mediator; protected String name; public abstract void send(String msg); public abstract void receive(Client sender, String msg); } static class SimpleClient extends Client { @Override public void send(String msg) { mediator.sendMessage(this, msg); } @override public void receive(sender, String MSG) {system.out. println(sender.name + "" + this.name +" : " + msg); } } public static void main(String[] args) { Mediator mediator = new AllSendMediator(); Client clientA=new SimpleClient(); Client clientB=new SimpleClient(); Client clientC=new SimpleClient(); clientA.name="clientA"; clientB.name="clientB"; clientC.name="clientC"; mediator.register(clientA); mediator.register(clientB); mediator.register(clientC); Clienta. send(" I posted a blog post [Behavioral] Mediator] click to view it "); Clientc. send(" I posted a blog [blabbla] check it out "); }}Copy the code

I wrote it in a class for the convenience of demonstration, so you can split it up and use it flexibly in the actual development process.

A brief explanation:

Click for details

Is it like a subscription? That’s what it is.

I am dying aground, hope this article can help you, we will see you next time ~