Design Pattern-intermediary Pattern

Source: Internet
Author: User

In many cases, interaction between objects is very complex. At the beginning of the design, you do not know who the object to interact with and what type it is. We know that if object A and object B can interact with each other, A and B will form mutual dependencies. If there are more than one object, such dependencies will be very complex, in many cases, we do not know what objects need to interact with each other. Therefore, we cannot consider these situations during design. Since we cannot know the interaction between objects in advance during design, we can place this interaction on an "intermediate platform, this intermediate platform knows all objects (dependent on all objects, but whether objects must depend on the platform is not required ), in this way, the dependency between objects is simplified to the dependency between objects and platforms, which greatly reduces the complexity between objects. Of course, objects can interact on the platform and must comply with certain protocols, and the platform must know the details of all objects. (This is actually not a condition during development and implementation, such as Delphi and C #. All Java classes have a common base class, therefore, you do not need to define an abstract interface for these interactive objects ). This platform serves as an intermediary.

Design Patterns: a mediation object is used to encapsulate a series of objects (colleague objects) for interaction. The intermediary makes the objects do not need to be explicitly referenced to each other, so that the coupling is loose and the interaction between them can be changed independently.

The mediation mode has three roles: one is an abstract colleague. The role is defined purely to ensure that the intermediary can unify the form when defining the parameters of the interaction method, does not rely on specific colleagues (this abstract class is generally not required, because common development languages have a basic class type (such as Delphi, C # object class )), the second is the specific colleague class (the object to be interacted, which can be existing and defined), and the third is the intermediary. The following is a brief diagram of the mediation mode:

Example:

Public struct mediator_param
{
Public string name;
Public string value;
}
/// <Summary>
/// Summary of mediator.
/// </Summary>
Public abstract class mediator_mediator
{
// Define other common attributes of the intermediary
// The following attributes are generally processed by events, which is better
Public abstract void concreatecolleagueadatachange (mediator_colleague sender, mediator_param PARAM );
Public abstract void concreatecolleaguebdatachange (mediator_colleague sender, mediator_param PARAM );
}
Public class mediator_concreatemediator: mediator_mediator
{
Private mediator_concreatecolleaguea concreatecolleaguea;
Private mediator_concreatecolleagueb concreatecolleagueb;
Public override void concreatecolleagueadatachange (mediator_colleague sender, mediator_param PARAM)
{
If (Param. Name = "Tian ")
Concreatecolleagueb. showvalue (Param. value );
}
Public override void concreatecolleaguebdatachange (mediator_colleague sender, mediator_param PARAM)
{
Concreatecolleagueb. showvalue (Param. Name + "" + Param. value );
}
Public void introduceconcreatemediator (mediator_concreatecolleaguea concreatecolleaguea, mediator_concreatecolleagueb concreatecolleagueb)
{
This. concreatecolleaguea = concreatecolleaguea;
This. concreatecolleagueb = concreatecolleagueb;
}
Public mediator_concreatemediator ()
{
}
}
Public abstract class mediator_colleague
{

}
Public class mediator_concreatecolleaguea: mediator_collegou
{
Private mediator_mediator Mediator;
Public mediator_concreatecolleaguea (mediator_mediator mediator)
{
This. mediator = Mediator;
}
Public void datachanged ()
{
If (mediator! = NULL)
{
Mediator_param Param;
Param. Name = "Tian ";
Param. value = "121212 ";
Mediator. concreatecolleagueadatachange (this, Param );
}
}
}
Public class mediator_concreatecolleagueb: mediator_collegou
{
Private mediator_mediator Mediator;
Public mediator_concreatecolleagueb (mediator_mediator mediator)
{
This. mediator = Mediator;
}
Public void datachanged ()
{
If (mediator! = NULL)
{
Mediator_param Param;
Param. Name = "Zhang ";
Param. value = "888888 ";
Mediator. concreatecolleaguebdatachange (this, Param );
}
}
Public void showvalue (string value)
{
System. Windows. Forms. MessageBox. Show (value );
}
}
Public class mediator_client
{
Public static void test ()
{
Mediator_concreatemediator mediator = new mediator_concreatemediator ();
Mediator_concreatecolleaguea MCA = new mediator_concreatecolleaguea (mediator );
Mediator_concreatecolleagueb MCB = new mediator_concreatecolleagueb (mediator );
Mediator. introduceconcreatemediator (MCA, MCB );
MCA. datachanged ();
McB. datachanged ();
}
}

Advantages of the mediation mode:

Reduce subclass generation; decouple colleague classes; Simplify protocols between objects; Abstract How objects collaborate; and centralize control;

Improvement on intermediary mode:

1) generally, the development language has a basic parent class. Therefore, if you use this basic class as an abstract colleague, you do not need to display the definition during implementation.

2) When an event or elimination is used for interaction between objects, the colleague object can be independent of the intermediary and the relationship between the intermediary and the object can be further decoupled.

Typical application scenarios:

1) various interactive platforms in real life (such as exchanges, supermarkets, fairs, shopping malls, trade fairs, and online forums). Pay special attention to the following, in reality, the intermediary meanings of many intermediary companies (such as intermediary agencies for study abroad) are not the same as those in intermediary models. They not only reflect a kind of "intermediary, there is also the taste of "agent" in it. In the mediation mode, the intermediary only provides an interactive platform. The specific interaction is completed between objects.

2) UI of the development system.

Comparison:

Although the intermediary mode and the facade mode have similar functions, there is a big difference between the two. The facade mode provides a simplified interface for users to use a series of objects, the intermediary mode is mainly used to provide an interactive place for a series of objects, the intermediary object encapsulates a series of interactions between objects. In the intermediary mode, there is no user role concept and services are not provided externally.

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.