Mediator Pattern)

Source: Internet
Author: User

Mediator Pattern)

Intermediary mode definition:

The intermediary mode is also called the mediation mode. It is defined as: an intermediary object encapsulates the interaction of a series of objects. The intermediary makes the interaction of objects do not need to be displayed, so that the coupling is loose, in addition, the interaction between them can be changed independently.


Advantages of intermediary mode:

The advantage of the intermediary mode is that it reduces the dependency between classes and changes the original one-to-many dependency into one-to-one dependency. The colleague class only depends on the intermediary, reducing the coupling between classes.

 

Disadvantages of intermediary mode:

The disadvantage of the intermediary mode is that the intermediary will expand greatly, and the logic is complicated. The more colleagues there are, the more complicated the intermediary logic is.

 

Scenarios of intermediary mode:

The intermediary mode is suitable for tightly coupled objects (A Spider network structure occurs in a class chart ).

 

Generic class diagram of the intermediary mode:

Mediator (Abstract intermediary role): defines the unified interface of the intermediary for direct communication between different colleagues' roles.

Concretemediator (a specific intermediary role): coordinates the roles of colleagues to implement collaboration. It depends on the roles of colleagues. 

Colleague (colleague role): Every colleague role knows the existence of the intermediary role and communicates with other colleagues' roles through the intermediary role. The behavior of each colleague can be divided into two types: the first is the behavior of the colleague role itself, and the method of not interacting with other colleagues' roles or intermediaries, this method is called self-method. The second is the behavior that must be completed by the intermediary, called dep-method ).

 

Common mediation ModelsCode:

Abstract intermediary role:

 Public   Abstract  Class Mediator
{
/* Intermediary dependent on various colleagues */
Private Concretecolleague1 colleague1 = Null ;
Private Concretecolleague2 colleague2 = Null ;

Public Mediator ()
{

}

Public Concretecolleague1 getcolleague1 ()
{
Return Colleague1;
}

Public Void Setcolleague1 (concretecolleague1 colleague1)
{
This . Colleague1 = colleague1;
}

Public Concretecolleague2 getcolleague2 ()
{
Return Colleague2;
}

Public Void Setcolleague2 (concretecolleague2 colleague2)
{
This . Colleague2 = colleague2;
}

/* Defines the unified interface of the intermediary to coordinate communications between the roles of colleagues. */
Abstract Public Void Dosomething1 ();
Abstract Public Void Dosomething2 ();
}

Specific intermediary role:

 Public   Class Concretemediator Extends Mediator
{
/* The intermediary object encapsulates the interaction of a series of objects and can independently change the interaction between them. */
@ Override
Public Void Dosomething1 ()
{
Super . Getcolleague1 (). selfmethod1 ();
Super . Getcolleague2 (). selfmethod1 ();
}

/* The intermediary object encapsulates the interaction of a series of objects and can independently change the interaction between them. */
@ Override
Public Void Dosomething2 ()
{
Super . Getcolleague1 (). selfmethod2 ();
Super . Getcolleague2 (). selfmethod2 ();
}
}

Colleagues:

 Public   Class Concretecolleague1
{
Private Mediator mediator = Null ;

Public Concretecolleague1 (mediator)
{
This . Mediator = Mediator;
}

/* Spontaneous behavior 1 */
Public Void Selfmethod1 ()
{
System. Out. println ("colleague1 selfmethod1 invoke! ");
}

/* Spontaneous behavior 2 */
Public Void Selfmethod2 ()
{
System. Out. println ("colleague1 selfmethod2 invoke! ");
}

/* Dependent Behavior */
Public Void Depmethod ()
{
This . Mediator. dosomething1 ();
}
}
  Public   class  concretecolleague2 
{< br> private mediator = null ;
Public concretecolleague2 (mediator)
{< br> This . mediator = mediator;
}< br>
Public void selfmethod1 ()
{< br> system. out. println ("colleague2 selfmethod1 invoke! ");
}< br> Public void selfmethod2 ()
{< br> system. out. println ("colleague2 selfmethod2 invoke! ");
}< br> Public void depmethod ()
{< br> This . mediator. dosomething2 ();
}< BR >}

Scenario:

 
Public ClassClient
{
Public Static VoidMain (string [] ARGs)
{
Mediator mediator =NewConcretemediator ();

Concretecolleague1 colleague1 =NewConcretecolleague1 (mediator );
Concretecolleague2 colleague2 =NewConcretecolleague2 (mediator );

Mediator. setcolleague1 (colleague1 );
Mediator. setcolleague2 (colleague2 );

Colleague1.depmethod ();
System. Out. println ("---------------------------------");
Colleague2.depmethod ();
}
}


Understanding "Intermediary mode"


Using the intermediary mode will simplify the complex one-to-many dependencies between objects to one-to-one dependencies:

 

Before the intermediary mode is used, objects are directly dependent, and objects are directly interacted. After the intermediary mode is used, every object depends on the intermediary object. When you need to interact with other objects, it is an indirect interaction between the intermediary and other objects. We can change the interaction of the colleague class without modifying any colleague class (by modifying the intermediary ).

Intermediary mode in practice:

1. Each airport has a "XXX airport scheduling center ". This is a real-world middleware model. Without this scheduling center, when each plane (colleague class) arrives at the airport, you need to first interact with other planes to see if there is any conflict between the plane and my arrival, then interact with the airport (colleagues) to see if there is no idle run, and finally interact with the Logistics Department (colleagues) it is hard to imagine whether the stopped resources are ready for interaction.

2. MVC framework. The struts2 controller is a typical intermediary.

3. Various intermediary companies in reality.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.