Design Pattern 11-Mediator Pattern (behavior pattern)

Source: Internet
Author: User
1. Scenario Problems

If the computer does not have a motherboard, the structure shown in the figure is messy.

 
What if the above situation occurs in software development?
If software development requires many modules, should each module interact with each other? Isn't that too difficult to use? So how can we simplify this interaction? Just like a computer, just get an intermediary. Interaction between all modules and intermediaries reduces the coupling between modules.
2. A specific example

Watch a movie on a computer:
First, the optical drive reads the disc data and tells the motherboard that the status changes.
The organizer receives data and submits it to the CPU for processing.
After the CPU completes processing, the data is divided into video and audio, and the main board is notified. The processing is complete.
The motherboard obtains the data processed by the CPU, delivers the data to the video card and sound card, and then displays the video and sound.
3. Solution: intermediary mode 3.1 intermediary definition:

An intermediary object is used to encapsulate a series of object interactions. The intermediary makes the objects do not need to be displayed and referenced, so that they are loosely coupled and can independently change the interaction between them. This will facilitate object modification and maintenance.
3.2 intermediary structure:

 
4. Sample Code: 4.1 abstract parent class of the colleague class (note that it is an abstract class)

Package demo09.mediator. example1;/*** abstract parent class of the colleague class */public abstract class colleague {/*** holds the intermediary object, every colleague class knows its intermediary object */private mediator;/*** constructor, pass in the intermediary object ** @ Param mediator * intermediary object */Public colleague (mediator) {This. mediator = mediator;}/*** get the intermediary object corresponding to the current colleague class ** @ return corresponding intermediary object */public mediator getmediator () {return mediator ;}}
4.2 colleague a note: Inherit from Austria
Package demo09.mediator. example1;/*** specific colleague Class A */public class concretecolleaguea extends colleague {public concretecolleaguea (mediator) {super (mediator);}/*** probe method, execute some business functions */Public void someoperation () {// notify the intermediary object getmediator () when you need to communicate with other colleagues (). changed (this );}}
4.3 colleague B Note: Inheritance
Package demo09.mediator. example1;/*** specific colleague Class B */public class concretecolleagueb extends colleags {public concretecolleagueb (mediator) {super (mediator);}/*** probe method, execute some business functions */Public void someoperation () {// notify the intermediary object getmediator () when you need to communicate with other colleagues (). changed (this );}}
4.4 intermediary, defining interfaces for communication between objects of colleagues
Package demo09.mediator. example1;/*** intermediary, which defines the interface for communication between objects of various colleagues */public interface mediator {/*** the method for notifying the intermediary when the colleague object changes itself, * The intermediary is responsible for interacting with other colleagues' objects * @ Param collegou, so that the intermediary object can get the state of the colleague object through the object instance */Public void changed (colleague );}
4.5 specific intermediary implementation

All colleagues' interactions are implemented in the intermediary.

Package demo09.mediator. example1;/*** specific intermediary implementation */public class concretemediator implements mediator {/*** holds and maintains colleague a */private concretecolleaguea colleaguea; /*** hold and maintain colleague B */private concretecolleagueb colleagueb; /*** set the object that the intermediary needs to know and maintain * @ Param colleague a A */Public void setconcretecolleaguea (concretecolleaguea colleague) {colleaguea = colleague ;} /*** set the colleague B object that the intermediary needs to understand and maintain * @ Param colleague B object */Public void setconcretecolleagueb (concretecolleagueb colleague) {colleagueb = colleague ;} public void changed (colleague) {// a colleague class has changed. It is usually necessary to submit a user with another colleague. // coordinate the corresponding colleague object to implement collaborative behavior }}
5. Use intermediary implementation example: 5.1 Structure

 
5.2 abstract parent class of the colleague class (Note: it is an abstract class)

Package demo09.mediator. example2;/*** abstract parent class of the colleague class */public abstract class colleague {/*** holds the intermediary object, every colleague class knows its intermediary object */private mediator;/*** constructor, pass in the intermediary object * @ Param mediator intermediary object */Public colleague (mediator) {This. mediator = mediator;}/*** get the intermediary object corresponding to the current colleague class * @ return corresponding intermediary object */public mediator getmediator () {return mediator ;}}
5.3 optical drive, a colleague
Package demo09.mediator. example2;/*** optical drive, a colleague class */public class cddriver extends colleague {public cddriver (mediator) {super (mediator );} /*** data read from the optical drive */private string data = ""; /*** get the data read from the optical drive * @ return the data read from the optical drive */Public String getdata () {return this. data;}/*** read the disc */Public void readcd () {// The data displayed in the video before the comma (,) and the sound after the comma. data = "design mode, worth studying"; // notify the motherboard that its status has changed this. getmediator (). changed (this );}}
5.4cpu, a colleague
Package demo09.mediator. example2;/*** CPU class, a colleague class */public class CPU extends colleague {public CPU (mediator) {super (mediator );} /***** decomposed video data */private string videodata = "";/***** decomposed audio data */private string sounddata = ""; /*** get the decomposed video data * @ return the decomposed video data */Public String getvideodata () {return videodata ;} /*** obtain the decomposed sound data * @ return the decomposed sound data */Public String getsounddata () {return sounddata;}/*** process the data, split the data into audio and video data * @ Param data the data to be processed */Public void executedata (string data) {// split the data into video data, followed by the audio data string [] Ss = data. split (","); this. videodata = ss [0]; this. sounddata = ss [1]; // notifies the motherboard that CPU work is completed this. getmediator (). changed (this );}}
5.5 graphics card, a colleague
Package demo09.mediator. example2;/*** video card class, a colleague class */public class videocard extends colleague {public videocard (mediator) {super (mediator );} /*** display video data ** @ Param data * The data to be displayed */Public void showdata (string data) {system. out. println ("You are watching:" + Data );}}
5.6 sound card, a colleague
Package demo09.mediator. example2;/*** sound card class, a colleague class */public class Soundcard extends colleague {public Soundcard (mediator) {super (mediator );} /*** sound Based on Audio Frequency Data * @ Param data refers to the sound data */Public void sounddata (string data) {system. out. println ("Voiceover:" + Data );}}
5.7 intermediary Interface
Package demo09.mediator. example2;/*** intermediary object interface */public interface mediator {/*** the method of notifying the intermediary when the colleague object changes itself, * The intermediary is responsible for interacting with other colleagues' objects * @ Param collegou, so that the intermediary object can get the state of the colleague object through the object instance */Public void changed (colleague );}
5.8 main board class, implementing intermediary Interface
Package demo09.mediator. example2;/*** main board class, implement the intermediary interface */public class motherboard implements mediator {/*** need to know the colleague class to be interacted-optical drive class */private cddriver = NULL; /*** need to know the colleague class to interact with-CPU class */private CPU = NULL; /*** you need to know the colleague class to interact with-video card class */private videocard = NULL; /*** need to know the colleague class to interact with-Sound Card class */private Soundcard = NULL; Public void setcddriver (cddriver) {This. cddriver = cddriver;} public void setcpu (CPU) {This. CPU = CPU;} public void setvideocard (videocard) {This. videocard = videocard;} public void setsoundcard (Soundcard) {This. soundcard = Soundcard;} public void changed (colleague) {If (colleague = cddriver) {// indicates that the optical drive has read data this. opecddriverreaddata (cddriver) colleague);} else if (colleague = CPU) {// indicates that the CPU has finished processing this. opecpu (CPU) colleague);}/*** interaction with other objects after the data is read by the optical drive * @ Param CD drive colleague object */private void opecddriverreaddata (cddriver CD) {// 1: first obtain the data string data = CD read by the optical drive. getdata (); // 2: Pass the data to the CPU for processing this.cpu.exe cutedata (data );} /*** interaction with other objects after processing CPU processed data * @ Param CPU colleague class */private void opecpu (CPU) {// 1: first obtain the data processed by the CPU string videodata = CPU. getvideodata (); string sounddata = CPU. getsounddata (); // 2: Pass the data to the video card and sound card to display this. videocard. showdata (videodata); this. soundcard. sounddata (sounddata );}}
5.9 client usage: watching movies
Package demo09.mediator. example2; public class client {public static void main (string [] ARGs) {// 1: Create an intermediary -- motherboard object motherboard mediator = new motherboard (); // 2: create a colleague class cddriver Cd = new cddriver (mediator); CPU = new CPU (mediator); videocard Vc = new videocard (mediator); Soundcard SC = new Soundcard (mediator ); // 3: Let the intermediary know all the colleagues mediator. setcddriver (CD); mediator. setcpu (CPU); mediator. setvideocard (VC); mediator. setsoundcard (SC); // 4: Start to watch a movie, put the CD into the optical drive, and start to read the CD. readcd ();}}
6. Thinking about the functions and call sequence of intermediary mode 6.1:

Encapsulate interaction between objects. Colleagues interact as follows.

 
6.2 nature of intermediary Model

Encapsulation Interaction
6.3 Advantages and Disadvantages of intermediary Mode

Advantages: loose coupling, centralized control of interaction, changing from many to one.
Disadvantages: over-centralization makes it difficult to manage and maintain intermediary objects.

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.