24-day design model --------- intermediary model, design model ---------

Source: Internet
Author: User

24-day design model --------- intermediary model, design model ---------

Original works of Lin bingwen Evankaka. Reprinted please indicate the source http://blog.csdn.net/evankaka

I. intermediary Mode 1. Definition

An intermediary object is used to encapsulate a series of object interactions. The intermediary makes the objects do not need to be explicitly referenced to each other, so that the coupling is loose and the interaction between them can be changed independently.

2. Applicability

(1) communication between a group of objects in a well-defined but complex manner. The resulting dependency structure is confusing and hard to understand.
(2) It is difficult to reuse an object because one object references many other objects and communicates directly with these objects.
(3) To create a behavior that is distributed across multiple classes without generating too many child classes.

3. structure and role


(1) Abstract intermediary role: the abstract intermediary role defines a unified interface for communication between the roles of colleagues.

(2) Concrete Mediator role: the intermediary role coordinates the roles of colleagues to implement collaboration. Therefore, it needs to know and reference the roles of colleagues.

(3) colleagues' roles: each Colleague role knows the corresponding intermediary role and must communicate with other colleagues' roles through the intermediary role.

Ii. Example

Abstract class of a person

Package com. patten. mediator;/*** @ author: Lin bingwen * @ declare: abstract class of a Person * @ time: 2015.2.4 */public abstract class Person {protected Mediator m_Mediator; /*** @ function: constructor * @ param: housing intermediary */public Person (Mediator m_Mediator) {this. m_Mediator = m_Mediator;}/*** @ function: send a message to the Housing Agency * @ param: Message */public abstract void sendMessage (String m_String);/*** @ function: get the message sent by the Housing Agency * @ param: Message */public abstract void getMessage (String m_String );}
Specific category of buyers
Package com. patten. mediator;/*** @ author: Lin bingwen * @ declare: class of the buyer * @ time: 2015.2.4 */public class BuyHousePerson extends Person {public BuyHousePerson (Mediator m_Mediator) {super (m_Mediator) ;}@ Overridepublic void sendMessage (String m_String) {m_Mediator.sendMessage (this, m_String) ;}@ Overridepublic void getMessage (String m_String) {System. out. println ("the buyer received the message:" + m_String );}}
Specific category of house seller
Package com. patten. mediator;/*** @ author: Lin bingwen * @ declare: class of the seller * @ time: 2015.2.4 */public class SellHousePerson extends Person {public SellHousePerson (Mediator m_Mediator) {super (m_Mediator) ;}@ Overridepublic void sendMessage (String m_String) {m_Mediator.sendMessage (this, m_String) ;}@ Overridepublic void getMessage (String m_String) {System. out. println ("the Seller received the message:" + m_String );}}
Intermediary abstract class

Package com. patten. mediator;/*** @ author: Lin bingwen * @ declare: the intermediary's abstract class * @ time: 2015.2.4 */public abstract class Mediator {public abstract void sendMessage (Person m_Person, string m_String );}

Intermediary classes

Package com. patten. mediator;/*** @ author: Lin bingwen * @ declare: implementation of the housing agency class * @ time: 2015.2.4 */public class HouseMediator extends Mediator {private BuyHousePerson m_BuyHousePerson; // buyer private SellHousePerson m_SellHousePerson; // seller/*** @ function: sets buyer * @ param: buyer */public void setBuyHousePerson (BuyHousePerson m_Person) {registrant = m_Person ;} /*** @ function: Set the seller * @ param: Seller */public void setSellHousePerson (SellHousePerson m_Person) {m_SellHousePerson = m_Person;}/*** @ function: send a message to the seller based on the buyer's information, or in turn * @ param: m_Person is the buyer or seller, m_String is the message */@ Overridepublic void sendMessage (Person m_Person, String m_String) {if (m_Person = m_BuyHousePerson) {m_SellHousePerson.getMessage (m_String);} else if (m_Person = m_SellHousePerson) {m_BuyHousePerson.getMessage (m_String );}}}

Usage:

Package com. patten. mediator; public class Main {public static void main (String [] args) {HouseMediator m_HouseMediator = new HouseMediator (); // Housing Agency SellHousePerson m_SellHousePerson = new SellHousePerson (m_HouseMediator ); // The seller registers the information of the housing agent BuyHousePerson m_BuyHousePerson = new BuyHousePerson (m_HouseMediator); // The buyer, information of the house agent registration/information of the house agent registration buyer and seller m_HouseMediator.setSellHousePerson (m_SellHousePerson); m_HouseMediator.setBuyHousePerson (m_BuyHousePerson ); // the buyer and the seller sent a message m_BuyHousePerson.sendMessage ("the buyer Xiaohong says I want to buy a 200-ping-0.1 million house "); m_SellHousePerson.sendMessage ("the buyer said that I have a 300-foot 0.15 million house to sell ");}}

Result:

The seller received the message: the buyer Dede said that I would like to buy a 200-foot 0.1 million house.
The buyer received the message: bing, the seller of the house, said that I had a 300-foot 0.15 million house to sell.

Iii. Advantages and Disadvantages

(1) subclass is reduced. Mediator aggregates the actions of multiple objects. to change these actions, you only need to generate a subclass of Mediator, so that each collection class can be reused. (2) It decouples colleags. Mediator facilitates the loose coupling between different colleags. You can independently change and reuse different colleags and Mediator classes. It simplifies the object protocol. Use one-to-multiple interactions between Mediator and colleags to replace multiple-to-many interactions. One-to-multiple relationships are easier to understand, maintain, and expand. (4) It abstracts how objects collaborate. Using mediation as an independent concept and encapsulating it in an object enables you to focus on the interaction between the objects themselves. This helps to figure out how objects in a system interact.
(4) It centralizes control, and the intermediary mode changes the complexity of interaction into the complexity of the intermediary. Because the intermediary encapsulates the Protocol, it may become more complex than any other Colleague. This may make the intermediary itself a giant that is difficult to maintain



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.