Design Mode Study Notes-intermediary Mode

Source: Internet
Author: User

Overview:

Encapsulate a series of object interactions with intermediary objects. The intermediary allows objects to reference each other without being displayed, thus loose coupling and changing the interaction between them.

Applicable scenarios:

1. A group of objects communicate in a well-defined but complex manner. The resulting dependency structure is confusing and hard to understand.

2. An object references many other objects and communicates directly with them, making it difficult to reuse the object.

3. I want to customize the behavior of a branch in multiple classes without generating too many child classes.

Class diagram:

ExampleCode:

1. Define the abstract intermediary class

   ///     <Summary>  
/// Abstract intermediary class
/// </Summary>
Abstract Class Mediator
{
/// <Summary>
/// Obtain the colleague objects and abstract methods for sending messages
/// </Summary>
/// <Param name = "message"> </param>
/// <Param name = "colleague"> </param>
Public Abstract Void Send ( String Message, colleague );
}

2. define abstract colleague classes

   ///     <Summary>  
/// Abstract colleague class
/// </Summary>
Abstract Class Collegou
{
Protected Mediator;
/// <Summary>
/// Initialize the intermediary object
/// </Summary>
/// <Param name = "mediator"> </param>
Public Colleague (mediator)
{
This . Mediator = Mediator;
}
}

3. Specific intermediary

   ///     <Summary>  
/// Specific intermediary
/// </Summary>
Class Concretemediator: mediator
{
Private Concretecolleague1 colleague1;

Private Concretecolleague2 colleague2;

Public Concretecolleague1 colleague1
{
Set {Colleague1 = Value ;}
}
Public Concretecolleague2 colleague2
{
Set {Colleague2 = Value ;}
}
/// <Summary>
/// Rewrite the method of sending information, and send the message to the specified object based on the transmitted object
/// </Summary>
/// <Param name = "message"> </param>
/// <Param name = "colleague"> </param>
Public Override Void Send ( String Message, colleague)
{
If (Colleague = Colleague1)
{
// Colleague 1 sends messages to colleague 2
Colleague2.notify (Message );
}
Else
{
// Colleague 2: Send a message to colleague 1
Colleague1.notify (Message );
}
}
}

3. Two colleagues

   Class  Concretecolleague1: colleague
{
Public Concretecolleague1 (mediator ): Base (Mediator)
{}
Public Void Send ( String Message)
{
Mediator. Send (message, This );
}
Public Void Notify ( String Message)
{
Console. writeline ( " Colleague 1 got the message: " + Message );
}
}
Class Concretecolleague2: colleague
{
Public Concretecolleague2 (mediator ): Base (Mediator)
{}
Public Void Send ( String Message)
{
Mediator. Send (message, This );
}
Public Void Notify ( String Message)
{
Console. writeline ( " Colleague 2 received the message: " + Message );
}
}

4. Client call

   ///    <Summary>  
/// Test intermediary Mode
/// </Summary>
Static Void Testmediator ()
{
Concretemediator m = New Concretemediator ();

/// Let two colleagues know the intermediary
Concretecolleague1 C1 = New Concretecolleague1 (m );
Concretecolleague2 C2 = New Concretecolleague2 (m );

/// Let the intermediary know the specific object
M. colleague1 = C1;
M. colleague2 = C2;

Console. writeline ( " Colleague 1 sends a message to colleague 2 " );
C1.send ( " Have you eaten? " );
Console. writeline ( " Colleague 2 send a message to colleague 1 " );
C2.send ( " No. Are you going to treat me? " );

Console. Read ();
}

Summary:

The intermediary mode abstracts the interaction between classes and selects the intermediary mode based on the complexity of the scenario.

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.