Java Design Patterns Rookie series (22) The model and implementation of the mediator pattern

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/40027109


Mediator Mode (mediator): Mainly used to reduce the coupling between classes and classes, because if the class has a dependency on the class, it is not conducive to the extension and maintenance of the function, because as long as you modify an object, the other associated objects will have to be modified.

First, UML modeling:



Second, the Code implementation

/** * Mediator Mode (mediator): Mainly used to reduce the coupling between classes and classes, because if the class has dependencies between classes, * * is not conducive to the expansion and maintenance of functions, because as long as you modify an object, the other associated objects will have to be modified. * Example: The following housing (rental) intermediary as an example */interface mediator {void createmediator (); void Recommendhouse ();}  /** (User) Let the intermediary help us recommend the house * * so we need to hold a mediation instance */abstract class User {private Mediator Mediator;public user (mediator mediator) {this.mediator = mediator;} Public Mediator Getmediator () {return mediator;} public abstract void Selecthouse ();} Class Zhangsan extends User {public Zhangsan (mediator mediator) {super (mediator);//TODO auto-generated constructor stub} @Overridepublic void Selecthouse () {System.out.println ("Zhang San in house selection ...");} Class LiSi extends User {public LiSi (mediator mediator) {super (mediator);//TODO auto-generated constructor stub} @Overrid epublic void Selecthouse () {System.out.println ("John Doe in house selection ...");} /** * Housing Agency: Intermediaries recommend houses for those who want to find a house, and they choose the house they want. * So the intermediary has to hold those instances before it is possible to rent out the house. */class Mymediator implements Mediator {private User zhangsan;private user Lisi;public user Getzhangsan () {return Zhangsan ;} Public useR Getlisi () {return Lisi;} @Overridepublic void Createmediator () {Zhangsan = new Zhangsan (this), Lisi = new Lisi (this);} /** * Intermediaries recommend houses to those who are looking for a house, they choose the house they want */@Overridepublic void Recommendhouse () {zhangsan.selecthouse (); Lisi.selecthouse ( );}} /** * Client Test class * * @author Leo */public class Test {public static void main (string[] args) {Mediator mediator = new Mymedia Tor (); Mediator.createmediator (); Mediator.recommendhouse ();}}

Iii. Summary

The mediator pattern only cares about the relationship with the Mediator class, the relationship between the specific class and the class, and the call to mediator on the line.


Java Design Patterns Rookie series (22) The model and implementation of the mediator 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.