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 ");