Latest web development tutorials

Intermediary model

Intermediary model (Mediator Pattern) is used to reduce the complexity of communication between a plurality of objects and classes. This model provides an intermediate class, which typically handles communications between different classes, and support for loosely coupled, making the code easier to maintain. Mediator patterns belong behavioral patterns.

Introduction

Intent: The use of an intermediary object that encapsulates a set of objects interact intermediary keeping objects from referring to each other explicitly, making it loose coupling, and can independently change the interaction between them.

Mainly to solve: there are a lot of objects and relationships between objects, this will inevitably lead to the structure of the system becomes very complicated, but if an object is changed, we also need to keep track of objects associated with it, and make the appropriate treatment .

When to use: multiple classes coupled with each other, forming a network structure.

How to fix: the above-mentioned network structure is separated into the star structure.

The key code: communication between Colleague objects encapsulate a class separately.

Application examples: 1, prior to China's accession to WTO is the various countries trade with each other, complex structure, it is by WTO countries to trade with each other.2, airport scheduling system. 3, MVC framework, where C (the controller) is M (model) and V (view) mediator.

Advantages: 1, reducing the complexity of the class will be converted into one-to-many.2, each decoupling between classes. 3, in line with the principles of Demeter.

Disadvantages: large broker will become complex and difficult to maintain.

Usage scenarios: 1 exists between objects in the system more complex reference relationship, leading to dependence among them confusing and difficult to reuse the structure of the object.2, want a middle class that encapsulates the behavior of multiple classes, and do not want to generate too many subclasses.

Note: it should not be used when the responsibility confusion.

achieve

Let's walk through the intermediary model chat rooms instance. Example, multiple users can send messages to chat rooms, chat rooms, message is displayed to all users. We will create two classesChatRoomandUser.User objectsChatRoomways to share their news.

MediatorPatternDemo,we demonstrate the use ofUserclass object to display the communication between them.

Mediator pattern UML diagram

step 1

Create intermediary 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 theUserobject to display the 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 output.

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