Design Pattern _ intermediary Pattern
DefinitionDefine an object that encapsulates how a set of objects interact. mediator promptes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. (encapsulate a series of object interactions with an intermediary object. The intermediary makes the interaction between objects do not need to be displayed, so that the coupling is loose and the interaction between them can be changed independently)
Unfamiliar WordsEncapsulates:Summary;Summary;IncludeA set of: a series of interact:Interaction;Mutual influence;InteractionLoose:Loose and wide;Fuzzy;Casual;FreeCoupling: coupled referring:Mentioned (current Word Segmentation of refer );Target;Relationship;ConsultExplicitly: vary:Changes;Different and different;[Generate] mutation
ExampleFor example
Procurement management, sales management, inventory management... there is a communication between multiple modules. If you need to maintain and modify something, who would like to become the programmer?
Based on the network topology, there are three types: Bus, ring, and star.
According to this design, the following
Class Diagram
Analysis class diagram: The three submodules inherit AbstractColleague, Mediator inherits AbstractMediator, its parent class has three submodule instances, and a unified processing execute method () to complete specific tasks, independent methods in the three sub-modules are required.
Public abstract class AbstractColleague {protected AbstractMediator mediator; // Why is it protected: Only public AbstractColleague (AbstractMediator mediator) {this. mediator = mediator ;}}
public abstract class AbstractMediator { protected Purchase purchase; protected Sale sale; protected Stock stock; public AbstractMediator(){ purchase=new Purchase(this); sale=new Sale(this); stock=new Stock(this); } public abstract void execute(String str,Object...objects);}
Three colleagues, each doing their own thing
Public class Purchase extends actcolleague {public Purchase (AbstractMediator mediator) {super (mediator);} public void buyIBMcomputer (int number) {super.mediator.exe cute (purchase. buy, number);} public void refuseBuyIBM () {System. out. println (no longer purchasing IBM computers );}}
Public class Sale extends actcolleague {public Sale (AbstractMediator mediator) {super (mediator);} public void sellIBMComputer (int number) {super.mediator.exe cute (sale. failed, number); System. out. println (sales computers: + number + servers);} // feedback, 0 ~ 100 public int getSaleStatus () {Random rand = new Random (System. currentTimeMillis (); int saleStatus = rand. nextInt (100); System. out. println (sales of IBM computers: + saleStatus); return saleStatus;} public void offSale () {super.mediator.exe cute (sale. offbeat );}}
Public class Stock extends AbstractColleague {public Stock (AbstractMediator mediator) {super (mediator);} private static int COMPUTER_NUMBER = 100; public void increase (int number) {COMPUTER_NUMBER + = number; system. out. println (inventory quantity: + COMPUTER_NUMBER);} public void decrease (int number) {COMPUTER_NUMBER-= number; System. out. println (inventory quantity: + COMPUTER_NUMBER);} public int getStockNumber () {return COMPUTER_NUMBER;} public void clearStock () {System. out. println (inventory cleanup quantity: + COMPUTER_NUMBER); super.mediator.exe cute (stock. clear );}}
An intermediary that processes complex businesses and calls one or more methods from three colleagues.
Public class Mediator extends actmediator {@ Override public void execute (String str, Object... objects) {if (str. equals (purchase. buy) {// purchase a computer this. buyComputer (Integer) objects [0]);} else if (str. equals (sale. sales) {// sales computer this. sellComputer (Integer) objects [0]);} else if (str. equals (sale. offline) {// sell this. offbeat ();} else if (str. equals (stock. clear) {// clearance processing this. clearStock () ;}} private void buyComputer (Integer number) {int saleStatus = super. sale. getSaleStatus (); if (saleStatus> 80) {// The sales status is good System. out. println (purchasing IBM computers: + number + servers); super. stock. increase (number);} else {// poor sales status int buyNumber = number/2; System. out. println (purchasing IBM computers: + buyNumber +) ;}} private void sellComputer (Integer number) {if (super. stock. getStockNumber ()
Test
Public static void main (String [] args) {AbstractMediator mediator = new Mediator (); // a mediator System. out. println (------------ Purchase computers by purchasers --------------); purchase Purchase = new purchase (mediator); // view the constructor of its abstract class to know Purchase. buyIBMcomputer (100); System. out. println (------------- sales personnel sales computers -----------); Sale sale = new Sale (mediator); sale. sellIBMComputer (1); System. out. println (------------ Warehouse Manager cleaning --------------); Stock stock = new Stock (mediator); stock. clearStock ();}
Description public abstract void execute (String str, Object... objects); variable parameter array, specific accessible http://blog.csdn.net/testcs_dn/article/details/38920323
Wu: multiple modules are coupled. When communication is required, business writing is messy. At this time, an intermediary is required to manage troubles.
General writing
Public abstract class Mediator {// define the colleague class protected ConcreteColleague1 c1; protected ConcreteColleague2 c2; // inject the colleague class into public ConcreteColleague1 getC1 () {return c1 ;} public void setC1 (ConcreteColleague1 c1) {this. c1 = c1;} public ConcreteColleague2 getC2 () {return c2;} public void setC2 (ConcreteColleague2 c2) {this. c2 = c2;} // the business logic of the intermediary mode public abstract void doSomething1 (); public abstract void doSomething2 ();}
public abstract class Colleague {protected Mediator mediator;public Colleague(Mediator _mediator){this.mediator = _mediator;}}
Public class ConcreteMediator extends Mediator {@ Overridepublic void doSomething1 () {// call the methods of the colleagues' classes, as long as they are public methods, super can be called. c1.selfMethod1 (); super. c2.selfMethod2 ();} public void doSomething2 () {super. c1.selfMethod1 (); super. c2.selfMethod2 ();}}
Public class ConcreteColleague1 extends Colleague {// The ConcreteColleague1 (Mediator _ mediator) {super (_ mediator);} // self-owned method self-methodpublic void selfMethod1 () is passed through the constructor () {// process your own business logic} // dependency method dep-methodpublic void depMethod1 () {// process your own business logic // the business logic that you cannot process, delegate to the intermediary for super. mediator. doSomething1 ();}}
Each colleague has a single task and each task, and the Project Manager ConcreteMediator of the intermediary needs to mobilize each part to complete one task.
Reduces coupling and dependency.
DisadvantagesThe more types of colleagues, the more complicated they are, and suitable for small systems
Use CasesThe spider mesh structure appears in the class diagram.
ExampleAirport scheduling center MVC Framework: C (Controller) is an intermediary. The front-end Controller isolates M (Model, business logic) and V (Viw, view) from open intermediary services.
Colleague: abstract brother: interface is a bit funny.