Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Mediator Pattern

Mediator Pattern

The Mediator Pattern (Mediator Pattern) is used to reduce the communication complexity between multiple objects and classes; it belongs to behavioral patterns.

The Mediator Pattern defines a mediator object to encapsulate the interactions between a series of objects. The mediator allows objects to not explicitly reference each other, thereby loosely coupling them, and their interactions can be changed independently.

Introduction

Intent

By introducing a mediator object to encapsulate and coordinate interactions between multiple objects, the coupling between objects is reduced.

Main problem solved

Use Cases

Implementation

Key code

Application Example

  1. WTO: After China joined the WTO, countries traded through the WTO, simplifying bilateral relations.
  2. Airport scheduling system: Coordinates aircraft takeoffs and landings, runway usage, etc.
  3. MVC framework: The controller acts as the mediator between the model and the view.

Advantages

  1. Reduce complexity: Converts one-to-many relationships among multiple objects into one-to-one relationships.
  2. Decoupling: Objects no longer reference each other directly and interact through the mediator.
  3. Complies with the Law of Demeter: An object only needs to know the mediator, not other objects.

Disadvantages

Usage recommendation

Notes

structures

The Mediator Pattern contains the following main roles:

Create a class that implements

We demonstrate the Mediator Pattern through a chat room example. In the example, multiple users can send messages to the 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, and our demo class uses the User object to display the communication between them.

Step 1

Create the mediator 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 the 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 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!

"

)

;

}

}

Step 4

Run the program, output result:



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

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