Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Intermediary mode


May 27, 2021 Design mode


Table of contents


Intermediary mode

Mediator Pattern is used to reduce the complexity of communication between multiple objects and classes. T his pattern provides a mediation class that typically handles communication between different classes and supports loose coupling, making the code easy to maintain. The intermediary pattern is a behavioral pattern.

Introduced

Intent: Encapsulates a series of object interactions with a mediation object, so that the objects do not need to be explicitly referenced to each other, making them loosely coupled and changing their interaction independently.

The main solution: There are a lot of associations between objects and objects, which inevitably lead to the structure of the system becomes very complex, and if an object changes, we also need to track the objects associated with it, and do the appropriate processing.

When to use: Multiple classes are coupled to each other, forming a mesh structure.

How to solve: Separate the above mesh structure into star structure.

Key code: Communication between objects Is encapsulated into a class that is handled separately.

Application example: 1, China's accession to the WTO before the countries trade with each other, the structure is complex, now each country through the WTO to trade with each other. 2 , airport dispatch system. 3, MVC framework, where C (controller) is M (model) and V (view) intermediary.

Pros: 1, reduces the complexity of the class and transforms one-to-many into one-to-one. 2 . Decoupling between classes. 3 , in line with the Dimit principle.

Cons: Intermediaries can be large and complex and difficult to maintain.

Use scenario: 1, there are more complex reference relationships between objects in the system, resulting in a confusing dependency structure between them and difficult to reus the object. 2 , want to encapsulate the behavior of multiple classes through an intermediate class, but do not want to generate too many sub-classes.

Note: Should not be used when responsibilities are confusing.

Realize

We demonstrate the intermediary pattern with chat room instances. I n an instance, multiple users can send messages to a chat room that displays messages to all users. W e'll create two classes, ChatRoom and User. User objects use the ChatRoom method to share their messages.

MediatorPatternDemo, our demo class uses User objects to display communication between them.

Intermediary mode

Step 1

Create a mediation class.

ChatRoom.java

import java.util.Date;

public class ChatRoom {
   public static void showMessage(User user, String message){
      System.out.println(new Date().toString()
         + " [" + user.getName() +"] : " + message);
   }
}

Step 2

Create a user class.

User.java

public class User {
   private String name;

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public User(String name){
      this.name  = name;
   }

   public void sendMessage(String message){
      ChatRoom.showMessage(this,message);
   }
}

Step 3

Use User objects to display communication between them.

MediatorPatternDemo.java

public class MediatorPatternDemo {
   public static void main(String[] args) {
      User robert = new User("Robert");
      User john = new User("John");

      robert.sendMessage("Hi! John!");
      john.sendMessage("Hello! Robert!");
   }
}

Step 4

Verify the output.

Thu Jan 31 16:05:46 IST 2013 [Robert] : Hi! John!
Thu Jan 31 16:05:46 IST 2013 [John] : Hello! Robert!