How to understand the intermediary mode, I would use real estate intermediary to understand it. Oh
Broker Mode:
1 Broker Mode2 Overview3 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 the interaction between them. 4 5 6 Applicability7 1. A group of objects communicates in a well-defined but complex way. The resulting interdependence structure is confusing and difficult to understand. 8 9 2. An object that references many other objects and communicates directly with those objects makes it difficult to reuse the object. Ten One 3. Want to customize a behavior that is distributed across multiple classes without having to generate too many subclasses. A - - participants the 1.Mediator - A mediator defines an interface for communicating with each colleague (colleague) object. - - 2.ConcreteMediator + the specific mediator realizes the collaborative behavior by coordinating the colleagues ' objects. - understand and maintain all of its colleagues. + A 3.Colleagueclass at each colleague class is aware of its mediator object. -Each colleague object communicates with its intermediary when it needs to communicate with other colleagues.
Test class:
1 Public classTest {2 3 Public Static voidMain (string[] args) {4Mediator Med =Newconcretemediator ();5 //the boss is here.6Med.notice ("Boss");7 8 //the client is here.9Med.notice ("Client");Ten } One}
1 Public Abstract class Mediator {23 Public Abstract void Notice (String content); 4 }
1 Public classConcretemediatorextendsMediator {2 3 PrivateColleaguea CA;4 5 PrivateColleagueb CB;6 7 PublicConcretemediator () {8CA =NewColleaguea ();9CB =NewColleagueb ();Ten } One A Public voidNotice (String content) { - if(Content.equals ("Boss")) { - //The boss is here, notify staff A. the ca.action (); - } - if(Content.equals ("Client")) { - //The customer has come, inform the front desk B + cb.action (); - } + } A}
1 Public Abstract class colleague {2 Public Abstract void action (); 3 }
1 public class Colleaguea extends colleague { 2 4 public void action () { 5 System.out.println ("Normal employee hard work" 6 7 }
1 Public class extends colleague {23 Public void action () {4 System.out.println ("The receptionist was very friendly." ); 5 }6 }
Do not write code to see the code, I am ...
Java design pattern--behavioral pattern--mediator pattern