This is the 27th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Pour out

Hello, my friends, I am xiaohei, the house I rent is about to expire, today, taking advantage of the weekend, to find a new house.

If you work or study in Beijing, you know that it is very difficult to rent a house. It is very difficult to find a cheap price, a short commute and a nice room. It’s harder than finding the best programming language in the world (PHP is the best).

Because my colleague friends no extra rent the house, so, no conditions directly trade lease with the landlord and, of course, some friends would go to some village to find some elderly men, straight to find the owner, rent, me is afraid of trouble, and afraid of being cheated by pit, so I was looking for a “shell” directly, “a”, “a love a” such company, On their platform to find a house, these formulas are generally called intermediaries, ha ha, said so much nonsense, finally the topic is introduced.

That’s right, this installment focuses on the Design Patterns Series’ intermediary pattern.

An overview of the

The intermediary design pattern is one of the behavior design patterns used to reduce the coupling degree and complexity of communication between different objects in the system.

The Mediator design pattern is useful in applications where multiple components interact.

If objects interact directly with each other, the system components are tightly coupled, which makes maintainability more expensive and not difficult to scale.

The mediator style focuses on providing intermediaries between objects for communication and helps achieve loose coupling between objects.

Like today I go to rent this example, if I and the landlord direct docking, so for me, first of all, it is not easy to find the landlord, the landlord is not easy to find me, then I may have to find multiple landlord at the same time, the landlord may also want to communicate with other tenants to see about prices, because I will not necessarily rent, the interactive mode is probably like this.

The intermediary is designed to solve the problem of direct communication (communication) between the tenant and the landlord, so as to reduce the communication cost (coupling degree and complexity) between me and the landlord. The communication mode becomes as follows.

The structure of the mediator pattern

The structure of the finalist pattern is relatively simple. In the whole structure, it mainly contains the following parts:

  • Communication objects (multiple communication objects to communicate with each other)
  • Mediations (dealing with communication between objects)

We implement this structure in code.

First of all, no matter tenants or landlords, they must first be people. Intermediaries here can only deal with people’s rental needs.

So let’s define the abstract class Human that represents Human:

package com.heiz.design.mediator;

/ * * *@authorHei says Java *@ClassName Human
 * @DescriptionOn behalf of humanity@date2021/11/27 * * /
public abstract class Human {
    public abstract void xuqiu(a);
}
Copy the code

Define a SocialLivestock class that represents renters:

package com.heiz.design.mediator;

/ * * *@authorHei says Java *@ClassName SocialLivestock
 * @DescriptionTenant (class name is "Social animal") *@date2021/11/27 * * /
public class SocialLivestock extends Human {
    @Override
    public void xuqiu(a) {
        System.out.println("Tenant: I'd like to rent a small house."); }}Copy the code

Then define the Landlord class that represents the Landlord:

package com.heiz.design.mediator;

/ * * *@authorHei says Java *@ClassName Landlord
 * @DescriptionThe landlord *@date2021/11/27 * * /
public class Landlord extends Human {
    @Override
    public void xuqiu(a) {
        System.out.println("I have a room for rent."); }}Copy the code

Next define the RoomMediator class that represents the mediator:

package com.heiz.design.mediator;

/ * * *@author yuriy.lu
 * @ClassName RoomMediator
 * @DescriptionMediation *@date2021/11/27 * * /
public class RoomMediator {

    public void zulin(Human human) {
        human.xuqiu();
        System.out.println("Intermediary help you solve ~"); }}Copy the code

Let’s take a quick test:

package com.heiz.design.mediator;

/ * * *@authorHei says Java *@ClassName RoomMediatorTest
 * @Description
 * @date2021/11/27 * * /
public class RoomMediatorTest {

    public static void main(String[] args) {
        Human landlord = new Landlord();
        Human socialLivestock = new SocialLivestock();

        RoomMediator roomMediator = newRoomMediator(); roomMediator.zulin(landlord); roomMediator.zulin(socialLivestock); }}Copy the code

Execute the program and output the result:

The mediator pattern in the JDK

  • java.util.TimerOf the classschedule()methods
  • In the JUC packageExecutorOf the classexecute()methods
  • Reflection packagejava.lang.reflectIn theMethodOf the classinvoke()methods

summary

Intermediary mode is mainly to reduce the complexity of communication between objects, reduce the degree of coupling, decoupling between each class, in accordance with Demeter’s law.

The downside of the mediator pattern is that the mediation class becomes particularly complex when the communication logic is complex.

Read more: SOLID Principles in Java


I am xiao Hei, a programmer in the Internet “casual”.

Water does not compete, you are in the flow