Design Pattern 17-mediator

Source: Internet
Author: User

Using system;
Using system. collections;

Namespace Pattern
{
/// <Summary>
/// Summary description for class1.
/// </Summary>
/* The definition of this mode is very simple to use an intermediary object to encapsulate a series of object interactions. Intermediary

So that each object does not need to be explicitly referenced to each other, so that the coupling is loose and can be changed independently.

Interaction between them. What are the benefits? Obviously, to reduce the complexity

Yes. Now it is a one-to-many relationship, because all objects are connected only to a single line of the Mediator intermediary.

System. The intermediary hermit contains the relationship between objects. The following is a simple example. For example: Xiao Wang

I want to hand over the manuscript to Xiao Li. Xiao Li wants to hand over the book to Xiao Zhang. Xiao Zhang wants to hand over the pen to Xiao Wang.

Give the walkman to Xiao Li so that interaction between objects is complex.

It is to deliver the item to an intermediary. In this way, the intermediary is responsible for distributing the item.

Establish contact. The intermediary maintains the relationship between objects .*/

// Start with the interface
Public interface iperson
{
String name {Get ;}
Arraylist Article {Get; set;} // each person may have multiple items
}

Public class Wang: iperson
{
Private string name = "Wang ";
Private arraylist article;

Public string name
{
Get {return name ;}
}

Public arraylist article
{
Get {return article ;}
Set {Article = value ;}
}
// ...... Do other things
}

Public class Zhang: iperson
{
Private string name = "Zhang ";
Private arraylist article;

Public string name
{
Get {return name ;}
}

Public arraylist article
{
Get {return article ;}
Set {Article = value ;}
}
// ...... Do other things
}

Public class Lee: iperson
{
Private string name = "Lee ";
Private arraylist article;

Public string name
{
Get {return name ;}
}

Public arraylist article
{
Get {return article ;}
Set {Article = value ;}
}
// ...... Do other things
}

// Intermediary
Public interface imediator
{
// Exchange items
Void sweaparticle (iperson P1, iperson P2, object );
}

Public class concretemediator: imediator
{

Public void sweaparticle (iperson P1, iperson P2, object)
{
P1.article. Add ();
P2.article. Remove ();
}
}

 
}

// Intermediary Mode

// Imediator M = new concretemediator ();
// Iperson Wang = new Wang ();
// Iperson Zhang = new Zhang ();
// Iperson Lee = new Lee ();
//
// Wang. Article. Add ("manuscript ");
// Zhang. Article. Add ("books ");
// Lee. Article. Add ("pen ");
// Lee. Article. Add ("Walkman ");
//
//// Switch start
// M. sweaparticle (Wang, Zhang, "");
// M. sweaparticle (Zhang, Lee, "books ");
// M. sweaparticle (Lee, Zhang, "pen ");
// M. sweaparticle (Lee, Wang, "Walkman ");

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.