1. Definition
Using a mediator object to encapsulate the interaction of a Series object, the mediator allows the individual objects to not need explicit references, and can independently change the interaction between objects.
The intermediary pattern is very simple, in the mediator pattern through the introduction of a mediator object, let other objects interact with the Mediator object, and the mediator object knows how to interact with all objects, so that the interaction between objects is not, so that the decoupling between objects.
Each colleague object has changed, and you do not need to know what will cause other objects to change, only need to notify the mediator, and the intermediary to interact with other objects. The benefit of this loose coupling is that the interaction between colleague objects is reduced and facilitates the modification and extension of functions.
2. Class Diagram
Mediator: The Mediator interface, which defines the method of interaction between colleague objects, can be a more general approach, such as change, or a small-scale interactive approach.
Concretemediator: A mediator entity that implements the methods defined in the interface, holds references to colleague objects, and coordinates interactions between colleague objects.
Colleague: Colleague class, usually abstract as abstract class, the main function is to constrain the type of colleague object, implement some common methods.
Colleaguea, Colleagueb: colleague entities, which implement their own business and need to interact with other colleague objects, communicate with the mediator and let the mediator handle the interaction.
3. Example
PackageCom.jerry.designpattern;/*** Intermediary *@authorJerry * @date January 20, 2015 morning 11:05:00*/ Public InterfaceMediator {/** * * @paramMediator*/ voidchanged (colleague colleague);} PackageCom.jerry.designpattern;/** * * @authorJerry * @date January 20, 2015 morning 11:26:05*/ Public classMainboardImplementsmediator{PrivateCddriver Cddriver; PrivateCPU CPU; PrivateVideocard Videocard; Privatesoundcard soundcard; Public voidsetcddriver (Cddriver cddriver) { This. Cddriver =Cddriver; } Public voidSetCpu (CPU CPU) { This. CPU =CPU; } Public voidSetvideocard (Videocard videocard) { This. Videocard =Videocard; } Public voidSetsoundcard (soundcard soundcard) { This. soundcard =soundcard; } @Override Public voidchanged (colleague colleague) {//TODO auto-generated Method Stub if(Colleagueinstanceofcddriver) {String Data=Cddriver.getdata (); Cpu.executedata (data); } Else if(ColleagueinstanceofCPU) {Videocard.showvideo (Cpu.getvideodata ()); Soundcard.sounddata (Cpu.getsounddata ()); } } } PackageCom.jerry.designpattern;/** * * @authorJerry * @date January 20, 2015 morning 11:04:43*/ Public Abstract classcolleague {PrivateMediator Mediator; Publiccolleague (mediator mediator) { This. Mediator =Mediator; } PublicMediator Getmediator () {returnMediator; } Public voidSetmediator (Mediator mediator) { This. Mediator =Mediator; } } PackageCom.jerry.designpattern;/** * * @authorJerry * @date January 20, 2015 morning 11:14:26*/ Public classCpuextendscolleague{ PublicCPU (Mediator mediator) {Super(mediator); } PrivateString Videodata; PrivateString Sounddata; PublicString Getvideodata () {returnVideodata; } PublicString Getsounddata () {returnSounddata; } Public voidexecutedata (String data) {string[] SS= Data.split (","); This. Videodata = Ss[0]; This. Sounddata = ss[1]; Super. Getmediator (). Changed ( This); }} PackageCom.jerry.designpattern;/** * * @authorJerry * @date January 20, 2015 morning 11:06:49*/ Public classCddriverextendscolleague{PrivateString data; PublicCddriver (Mediator mediator) {Super(mediator); } Public voidREADCD () { This. data = "design pattern, worth a good study"; Super. Getmediator (). Changed ( This); } PublicString GetData () {return This. Data; }} PackageCom.jerry.designpattern;/** * * @authorJerry * @date January 20, 2015 morning 11:23:19*/ Public classSoundcardextendscolleague{ Publicsoundcard (Mediator mediator) {Super(mediator); } Public voidsounddata (String data) {System.out.println ("VoiceOver:" +data); }} PackageCom.jerry.designpattern;/** * * @authorJerry * @date January 20, 2015 morning 11:20:31*/ Public classVideocardextendscolleague{ PublicVideocard (Mediator mediator) {Super(mediator); } Public voidShowvideo (String data) {System.out.println ("Watching:" +data); }} PackageCom.jerry.designpattern;/** * * @authorJerry * @date January 20, 2015 morning 11:31:44*/ Public classClient { Public Static voidMain (string[] args) {mainboard mainboard=Newmainboard (); Cddriver Cddriver=NewCddriver (mainboard); CPU CPU=NewCPU (mainboard); Videocard Videocard=NewVideocard (mainboard); Soundcard soundcard=Newsoundcard (mainboard); Mainboard.setcddriver (Cddriver); MAINBOARD.SETCPU (CPU); Mainboard.setvideocard (Videocard); Mainboard.setsoundcard (soundcard); CDDRIVER.READCD (); }}
4. Understanding the Broker Model
The mediator mode function is very simple, is to encapsulate the interaction between objects, if an object's operation will cause other related objects to change, or an operation needs to cause the object's subsequent and continuous operation, and this object does not want to handle these relationships, it can be handed to the intermediary to deal with all the trouble to the intermediary, Only notify the intermediary when necessary, the other let the intermediary to deal with it.
5. Pros and cons of intermediaries
Advantages: Loose coupling, set control interaction, many to many become one-to-many
Disadvantage: Excessive centralization, if the interaction between colleagues is very much, and more complex, all concentrated in the intermediary, will make the intermediaries become very complex, difficult to manage and maintain.
[design mode] Broker mode