The mediator pattern (mediator), which encapsulates a series of object interactions with a mediation object. The mediator makes the objects not need to explicitly reference each other, so that they are loosely coupled, and can independently change their interactions.
Look at the structure diagram of the broker pattern:
Colleague is called abstract colleague class, and Concretecolleague is a specific colleague class, each specific colleague only know their own behavior, but do not know the other same class situation, but they all know the intermediary object, mediator is an abstract intermediary, Defines the interface of the colleague object to the Mediator object, Concretemediator is the concrete mediator object, implements the abstract class method, it needs to know all the specific colleague class, and receives the information from the concrete colleague, sends the command to the concrete colleague object.
The following is a basic code structure for the broker pattern:
namespaceconsoleapplication1{//Mediator Abstract Mediator class Abstract classMediator { Public Abstract voidSend (stringmessage, colleague colleague); } //colleague Class abstract colleague class Abstract classcolleague {protectedMediator Mediator; Publiccolleague (mediator mediator) { This. Mediator =Mediator; } } //Concretemediator class classConcretemediator:mediator {PrivateConcreteColleague1 colleague1; PrivateConcreteColleague2 colleague2; PublicConcreteColleague1 Colleague1 {Set{colleague1 =value;} } PublicConcreteColleague2 Colleague2 {Set{colleague2 =value;} } Public Override voidSend (stringmessage, colleague colleague) { if(Colleague = =colleague1) {colleague2. Notify (message); } Else{colleague1. Notify (message); } } } classConcretecolleague1:colleague { PublicConcreteColleague1 (Mediator mediator):Base(mediator) {} Public voidSend (stringmessage) {Mediator. Send (Message, This); } Public voidNotify (stringmessage) {Console.WriteLine ("colleague 1 Get information:"+message); } } classConcretecolleague2:colleague { PublicConcreteColleague2 (Mediator mediator):Base(mediator) {} Public voidSend (stringmessage) {Mediator. Send (Message, This); } Public voidNotify (stringmessage) {Console.WriteLine ("colleague 2 Get information:"+message); } } classProgram {Static voidMain (string[] args) {Concretemediator m=NewConcretemediator (); ConcreteColleague1 C1=NewConcreteColleague1 (m); ConcreteColleague2 C2=NewConcreteColleague2 (m); M.colleague1=C1; M.colleague2=C2; C1. Send ("Have you had dinner yet? "); C2. Send ("not yet? Are you going to treat me? "); Console.readkey (); } } }
The results are as follows:
The broker pattern is easy to apply in the system and is easily misused in the system. When the system has a "many-to-many" interaction complex object group, do not rush to use the mediator mode, and to reflect on your system is not reasonable design.
The first thing about the mediator pattern is that the appearance of mediator reduces the coupling of each colleague, allowing the individual colleague classes and mediator to be changed and reused independently. Secondly, because the object how to work together abstraction, the intermediary as a separate concept and encapsulated in an object, so that the object of attention from the object of their own behavior transfer to the interaction between them, that is, standing in a more macroscopic angle to look at the system.
Because Concretemediator controls centralization, it turns the interaction complexity into the complexity of the mediator, which makes the intermediaries more complex than any one concretecolleague. The mediator pattern is typically used in situations where a set of objects communicates in a well-defined but complex way, and you want to customize a behavior that is distributed across multiple classes without having to generate too many subclasses.
Let's go back to the example of the United Nations in "Big Talk Design":
namespaceconsoleapplication1{//United Nations agencies Abstract classunitednations {//Statement Public Abstract voidDeclare (stringmessage, country colleague); } //Country Class Abstract classCountry {protectedunitednations Mediator; PublicCountry (unitednations mediator) { This. Mediator =Mediator; } } //American Class classUsa:country { PublicUSA (unitednations mediator):Base(mediator) {}//Statement Public voidDeclare (stringmessage) {Mediator. Declare (Message, This); } //Get information Public voidGetMessage (stringmessage) {Console.WriteLine ("The United States obtains information about each other:"+message); } } //Iraq category classIraq:country { PublicIraq (unitednations mediator):Base(mediator) {}//Statement Public voidDeclare (stringmessage) {Mediator. Declare (Message, This); } //Get information Public voidGetMessage (stringmessage) {Console.WriteLine ("Iraq obtains information on the other side:"+message); } } //The UN Security Council, equivalent to the Concretemediator category classunitednationssecuritycounci1:unitednations {PrivateUSA colleague1; PrivateIraq colleague2; //United States PublicUSA Colleague1 {Set{colleague1 =value;} } //Iraq PublicIraq Colleague2 {Set{colleague2 =value;} } //Statement Public Override voidDeclare (stringmessage, country colleague) { if(Colleague = =colleague1) {colleague2. GetMessage (message); } Else{colleague1. GetMessage (message); } } } classProgram {Static voidMain (string[] args) {UNITEDNATIONSSECURITYCOUNCI1 UNSC=NewUnitedNationsSecurityCounci1 (); USA C1=NewUSA (UNSC); Iraq C2=NewIraq (UNSC); UNSC. Colleague1=C1; UNSC. Colleague2=C2; C1. Declare ("no nuclear weapons to be developed, or war will be waged! "); C2. Declare ("We have no nuclear weapons and no fear of aggression. "); Console.readkey (); } } }
The results are as follows:
Mediator Mode-Design pattern learning