The intermediary mode of design pattern

Source: Internet
Author: User

The mediator pattern definition: Encapsulates a series of object interactions with a mediation object, so that the objects do not need to be shown to interact with each other, so that they are loosely coupled and can independently change the interaction between them.

Advantages:

The advantage of the broker model is to reduce the dependency between classes, turn the original one-to-many dependencies into a single-to-a-dependency, the colleague class relies only on intermediaries, reduces dependence, and, of course, reduces the coupling between classes.

Disadvantages:

The disadvantage of the intermediary model is that the intermediary will swell up a lot, and the logic is complex, the original N objects directly to the interdependence of the relationship between the intermediary and the colleague class, the more colleagues, the more complex the logic of the intermediary.

The class diagram is as follows:


The implementation code is as follows:

Abstract class of intermediaries:

Package com.designpatterns.mediator;/** * @author WSYW126 * @version created: May 8, 2016 3:33:09 * class Description: Alljava */public abstr Act class Imediator {protected ConcreteColleague1 c1;protected ConcreteColleague2 c2;public ConcreteColleague1 getC1 () { return C1;} public void SetC1 (ConcreteColleague1 c1) {C1 = C1;} Public ConcreteColleague2 getC2 () {return C2;} public void setC2 (ConcreteColleague2 c2) {C2 = C2;} public abstract void DoSomething1 ();p ublic abstract void DoSomething2 ();}

The broker's implementation class:

Package com.designpatterns.mediator;/** * @author WSYW126 * @version created: May 8, 2016 3:48:04 class Description: Alljava */public class C Oncretemediator extends Imediator {public concretemediator () {} @Overridepublic void DoSomething1 () {super. C1.selfmethod1 (); super. C2.SELFMETHOD2 ();} @Overridepublic void DoSomething2 () {super. C1.selfmethod1 (); super. C2.SELFMETHOD2 ();}}

Abstract colleague Class:

Package com.designpatterns.mediator;/** * @author WSYW126 * @version created: May 8, 2016 3:40:28 class Description: Alljava */public Abstra CT class Colleague {protected Imediator Mediator;public colleague (Imediator mediator) {this.mediator = mediator;}}

Implementation of colleague Class 1:

Package com.designpatterns.mediator;/** * @author WSYW126 * @version created: May 8, 2016 3:42:44 * class Description: Alljava */public class ConcreteColleague1 extends colleague {public ConcreteColleague1 (Imediator mediator) {super (mediator);} public void SelfMethod1 () {System.out.println ("This is selfmethod1!");} public void DepMethod1 () {super.mediator.doSomething1 ();}}

Implementation of colleague Class 2:

Package com.designpatterns.mediator;/** * @author WSYW126 * @version created: May 8, 2016 3:45:41 class Description: Alljava */public class C OncreteColleague2 extends colleague {public ConcreteColleague2 (Imediator mediator) {super (mediator);} public void SelfMethod2 () {System.out.println ("This is selfmethod2!");} public void DepMethod2 () {super.mediator.doSomething2 ();}}
Test class:

Package com.designpatterns.mediator;/** * @author WSYW126 * @version created: May 8, 2016 3:51:22 class Description: Alljava */public class C lient {public static void main (string[] args) {concretemediator cm = new Concretemediator (); ConcreteColleague1 C1 = new ConcreteColleague1 (cm); ConcreteColleague2 C2 = new ConcreteColleague2 (cm); CM.SETC1 (C1); cm.setc2 (C2); C2.depmethod2 ();}}

This is the mediator pattern.

Here's one thing to explain: The colleague class uses the constructor to inject the mediator, and the mediator uses getter and setter methods to inject the colleague class. Because the colleague class must have the intermediary, but the intermediary can have only some colleague class.


References :
The Zen of design pattern

Remark :
Reprint Please specify source: http://blog.csdn.net/wsyw126/article/details/51345997
WSYW126


The intermediary mode of design pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.