C # design Pattern-Mediator Pattern)

Source: Internet
Author: User

I. Overview

In software development, we sometimes encounter many objects interacting with each other. There is a complex reference relationship between objects. It is very difficult to modify the system when the requirement is changed. To decouple System Objects, an indirect layer can be introduced to manage relationships between objects. This is the intermediary mode.

Ii. intermediary Mode

An intermediary object is used to encapsulate a series of object interactions. The intermediary allows each object to reference each other without being displayed, so that the coupling is loose and the interaction between them can be changed independently. The structure is as follows:

 

As an intermediary, Mediator defines an interface for interacting with Colleague objects.

ConcreteMediator implements the Mediator interface. As a specific intermediary, ConcreteMediator coordinates various colleags to implement collaborative behavior.

A Mediator object is referenced in each Colleague to implement interaction with other colleags.

The Mediator mode uses Mediator to implement loose coupling between colleags. However, because Mediator converts the complex reference relationships between colleags into the complexity of Mediator, with the complexity of the logic, the Mediator object may become quite complex. Therefore, use it with caution in actual use, so that the complexity of the Mediator will not offset the benefits brought by the intermediary mode.

Iii. Example

Let's take a car remote control as an example to introduce the intermediary mode. The example is relatively simple and there is no need to use the intermediary mode, but you can take a look at the usage of this mode.

First, define colleags.


1 public abstract class PlayerControllerButton
2 {
3 protected PlayerController _ controller;
4 public bool Enable {get; set ;}
5
6 public PlayerControllerButton (PlayerController)
7 {
8 _ controller = controller;
9}
10
11 public virtual void Click ()
12 {
13 _ controller. ClickButton (this );
14}
15}
16
17 public class StartButton: PlayerControllerButton
18 {
19 public StartButton (PlayerController controller)
20: base (controller)
21 {
22 controller. Register (this );
23}
24}
25
26 public class StopButton: PlayerControllerButton
27 {
28 public StopButton (PlayerController controller)
29: base (controller)
30 {
31 controller. Register (this );
32}
33}
34
35 public class PauseButton: PlayerControllerButton
36 {
37 public PauseButton (PlayerController controller)
38: base (controller)
39 {
40 controller. Register (this );
41}
42} then define Mediator.


1 public class PlayerController
2 {
3 private StartButton _ startButton;
4 private StopButton _ stopButton;
5 private PauseButton _ pauseButton;
6
7 public PlayerController ()
8 {
9}
10
11 public void Register (PlayerControllerButton)
12 {
13 if (button. GetType (). ToString () = "DesignPattern. Mediator. StartButton ")
14 {
15_startbutton = button as startButton;
16}
17 else if (button. GetType (). ToString () = "DesignPattern. Mediator. StopButton ")
18 {
19 _ stopButton = button as StopButton;
20}
21 else if (button. GetType (). ToString () = "DesignPattern. Mediator. PauseButton ")
22 {
23 _ pauseButton = button as PauseButton;
24}
25}
26
27 public void ClickButton (PlayerControllerButton)
28 {
29 if (button ==_startbutton)
30 {
31 _ startButton. Enable = true;
32 _ stopButton. Enable = false;
33 _ pauseButton. Enable = false;
34}
35 else if (button ==_stopbutton)
36 {
37 _ startButton. Enable = false;
38 _ stopButton. Enable = true;
39 _ pauseButton. Enable = false;
40}
41 else if (button ==_pausebutton)
42 {
43 _ startButton. Enable = false;
44 _ stopButton. Enable = false;
45 _ pauseButton. Enable = true;
46}
47}
48
49 public void DisplayButtonState ()
50 {
51 Console. writeLine ("StartButton is {0}, StopButton is {1}, PauseButton is {2}", _ startButton. enable, _ stopButton. enable, _ pauseButton. enable );
52}
53}

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.