Github source address

An overview of 23 design patterns

  • Java Language Design – An overview of 23 design patterns

Creation pattern

  • Factory Method Pattern
  • Abstract Factory Pattern
  • Builder Mode
  • Prototype mode
  • Singleton

Structural mode

  • Facade Pattern
  • Adapter mode (Adapter)
  • Proxy mode
  • Composite mode
  • Flyweight Mode
  • Decorator pattern
  • Bridge mode (Bridge)

Behavioral pattern

  • Mediator Mode
  • Observer Model
  • Command mode
  • Iterator pattern (Iterator)
  • Template Method
  • Strategy Pattern
  • State mode
  • Memento Mode
  • Interpreter mode
  • Chain of Responsibility model
  • Visitor Pattern

define

By encapsulating a set of object interactions with a mediation object, the mediator loosens the coupling of objects without explicitly referring to each other, and can change their interactions independently.

The advantages and disadvantages

Advantages: 1, reduce the class complexity, one-to-many into one-to-one. 2. Decoupling between classes. 3, in accordance with the Demeter principle.

Cons: Intermediaries can be large and complex to maintain.

Usage Scenarios:

1. Complex reference relationships exist among objects in the system, resulting in disorganized structure of the dependency relationship between them and difficulty in reusing the object. 2. You want an intermediate class to encapsulate behavior in multiple classes without generating too many subclasses.

implementation

We demonstrate the mediator pattern with a chat room example. In this example, multiple users can send messages to a chat room, and the chat room displays messages to all users. We will create two classes ChatRoom and User. User objects use the ChatRoom method to share their messages.

MediatorPatternDemo, our demo class uses the User object to show the communication between them.

Step 1

Create the mediation class.

ChatRoom.java

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

Step 2

Create the User class.

User.java

public class User {
    private String name;

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

    public String getName(a) {
        return name;
    }

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

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

Step 3

Use the User object 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!"); }}Copy the code

Step 4

Execute the program and output the result:

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