Java Design Pattern modeling and implementation of cainiao series (22) intermediary Pattern

Source: Internet
Author: User

Java Design Pattern modeling and implementation of cainiao series (22) intermediary Pattern

 

 

Mediator: it is mainly used to reduce the coupling between classes, because if there is a dependency between classes, it is not conducive to function expansion and maintenance, because only one object needs to be modified, other associated objects must be modified.

I. uml modeling:

 

Ii. Code Implementation

 

/*** Mediator: it is mainly used to reduce coupling between classes, because if there is a dependency between classes, ** it is not conducive to function expansion and maintenance, because only one object needs to be modified, and other associated objects must be modified. ** Example: the following uses a house (rental) intermediary as an example */interface Mediator {void createMediator (); void recommendHouse () ;}/ *** (User) let the intermediary recommend the house for us ** so we need to hold an intermediary 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 (John is choosing a house ...);}} class LiSi extends User {public LiSi (Mediator mediator) {super (mediator); // TODO Auto-generated constructor stub} @ Overridepublic void selectHouse () {System. out. println (Li Si is choosing a house ...);}} /*** Housing Agency: the intermediary recommends a house to those who want to find the house and selects the house they want. ** Therefore, the intermediary must hold those instances 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);}/*** the intermediary recommends the house to those who want to find the house, select the desired house */@ Overridepublic void recommendHouse () {zhangsan. selectHouse (); lisi. selectHouse () ;}}/*** client Test class ** @ author Leo */public class Test {public static void main (String [] args) {Mediator mediator = new MyMediator (); mediator. createMediator (); mediator. recommendHouse ();}}

Iii. Summary

 

The intermediary mode only needs to care about the relationship with the Mediator class. The relationship between the specific class and the call can be handed over to the Mediator class.

 

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.