Step by step. NET Design Mode Study Notes 20, Mediator (intermediary Mode)

Source: Internet
Author: User

Overview

During software building, multiple objects are often associated and interacted with each other,
Objects often maintain a complex reference relationship. If you need to change it, this direct reference relationship will face constant changes.
In this case, we can use an "Intermediary object" to manage the associations between objects and avoid tightly coupled reference relationships between interactive objects to better resist changes.

Intention
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.

Structure chart

Role description:

Mediator: Abstract The Mediator role, which defines the interface from a colleague object to the Mediator object. This role is generally implemented by the Java Abstract class.
ConcreteMediator: The Paster mode inherits from the abstract Paster, and implements the event Methods declared by the superclass. It receives messages from a specific colleague object and sends commands to a specific colleague object.
Colleague: Abstract Colleague role. It defines the interface of the contact object of the contact. It only knows the contact but does not know the objects of other colleagues.
Colleague1 and colleague2: the roles of a specific colleague class inherited from the abstract colleague class. Each specific colleague class knows its behavior in a small scope, but does not know its purpose in a large scope.

 

Examples in life

There are many marital agencies on the street. Generally, single men and women store their respective data in the matchmaking service. single men and women choose to compare their own images. If a person is interested in mutual interests, then arrange for them to meet and talk. If they feel good, they will become faster. If they are not good, they will continue matching.

 

Example

When I first came to work in this area, I was faced with renting a house. At that time, I was not familiar with my life and found an intermediary. I found a single room about 300 metres away from the company. According to this, we have designed the use case diagram of the intermediary mode. The use case diagram is as follows:

 

Code Design:

Create mongoingmediator. cs First:

Html # viewSource "commandName =" viewSource "highlighterId =" highlighter_789982 "> view sourceprint?
1 public abstractclass RentingMediator
2 {
3     public abstract string Notice(string message,House house);
4 }

 

Create House. cs again:

View sourceprint?
01 public abstract class House
02 {
03     protected RentingMediator mediator;
04  
05     public House(RentingMediator rentingMediator)
06     {
07         this.mediator = rentingMediator;
08     }
09  
10     public abstract string
Related Article

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.