Java and mode 26-26th-redeployment Mode

Source: Internet
Author: User
Document directory
  • Source code
  • Source code

The mediator mode is the behavior mode of objects. The Paster mode encapsulates a series of object interaction methods so that these objects do not need to be explicitly referenced to each other. So that they can be loosely coupled. When the interaction between some objects in these objects changes, the interaction between other objects is not immediately affected. This ensures that these interactions can change independently.

Why do users need to be redeployed?

  As shown in, there are a large number of objects. These objects affect other objects and are affected by other objects. Therefore, they are often called colleags. These colleagues interact with each other to form systematic behavior. It can be seen that almost every object needs to interact with other objects, and this interaction represents a direct coupling between an object and another object. This is an over-coupled system.

By introducing a mediator object, you can change the system's mesh structure to a star structure centered on the intermediary, as shown in. In this star structure, the colleague object no longer interacts with another object through direct connections; on the contrary, it interacts with another object through the mediator object. The existence of the Mediator object ensures the stability of the object structure. That is to say, the structure of the system will not be greatly modified because of the introduction of new objects.

A good object-oriented design can increase collaboration between objects and reduce couping ). A well-thought-out design Splits a system into a group of cooperative colleagues, and then gives each colleague an unique responsibility to properly configure the collaboration relationship between them, so that they can work together.

If no motherboard exists

As we all know, the interaction between various accessories in the computer is mainly completed through the motherboard. If no motherboard exists in the computer, each accessory must interact with each other to transmit data to each other. In addition, because the interfaces of each accessory are different, data interfaces must be converted to match each other during interaction.

Fortunately, with the motherboard, the interaction between each accessory is completely completed through the motherboard. Each accessory only needs to interact with the motherboard, And the motherboard knows how to deal with all the accessories, this is much simpler.

Structure of the Mediator Mode

The schematic diagram of the mediation mode is as follows:

The mediator mode includes the following roles:

●Abstract mediator role:Defines the interface from a colleague object to a mediator object. The main method is one or more event methods.

●The following figure shows the roles of the concretemediator:Abstract The event Method declared by the mediator. Specific mediators are aware of all the specific types of colleagues and are responsible for coordinating the interaction between colleagues and objects.

●Abstract colleague role:Defines the interface from the caller to a colleague object. Colleagues only know the callers, but do not know other colleagues.

●The following figure shows the role of a colleague class (concretecolleague:All the specific colleagues' classes are inherited from the abstract colleagues' classes. Implement your own business. When you need to communicate with other colleagues, you need to communicate with the on-demand contact. The on-demand contact will be responsible for interacting with other colleagues.

Source code

Abstract mediator class

Public interface mediator {/*** notify the mediator method when a colleague object changes. * The mediator is responsible for interacting with other colleagues' objects. */Public void changed (colleague C );}

Specific Mediators

Public class concretemediator implements mediator {// hold and maintain colleague a private concretecolleaguea colleaguea; // hold and maintain colleague B private license colleagueb; Public void setcolleaguea (license colleaguea) {This. colleaguea = colleaguea;} public void setcolleagueb (concretecolleagueb colleagueb) {This. colleagueb = colleagueb;} @ override public void changed (colleague c) {/*** a colleague class has changed, usually, you need to interact with other colleagues * coordinate the corresponding colleague objects to implement collaborative behavior */}}

Abstract colleague class

Public abstract class colleague {// hold a mediation object private mediator;/*** constructor */Public colleague (mediator) {This. mediator = mediator;}/*** get the mediator object corresponding to the current colleague class */public mediator getmediator () {return mediator ;}}

Colleagues

Public class concretecolleaguea extends colleague {public concretecolleaguea (mediator) {super (mediator) ;}/ *** indicates a method to execute some operations */Public void operation () {// notify the contact object getmediator () when you need to communicate with other colleagues (). changed (this );}}

 

Public class concretecolleagueb extends colleague {public concretecolleagueb (mediator) {super (mediator) ;}/ *** indicates a method to execute some operations */Public void operation () {// notify the contact object getmediator () when you need to communicate with other colleagues (). changed (this );}}

 

Use a computer to watch movies

In our daily life, we often use computers to watch movies and describe the process. After simplification, we assume that there will be the following interaction process:

(1) first, the optical drive needs to read the data on the disc and then tell the motherboard that its status has changed.

(2) The motherboard obtains the data of the optical drive and submits the data to the CPU for analysis.

(3) After the CPU completes processing, it divides the data into video data and audio data and notifies the main board that it has finished processing.

(4) The motherboard obtains the data processed by the CPU and submits the data to the video card and sound card respectively to display the video and sound.

To implement the example in the mediator mode, we need to distinguish between the colleague object and the mediator object. It is obvious that the motherboard is a pause, and the optical drive, sound card, CPU, graphics card and other accessories are all as colleagues.

Source code

Abstract colleague class

Package COM. bankht. mediator;/*** @ Author: -AK47 * @ Creation Time: 03:37:23, January 3, ** @ Class description: abstract colleague class */public abstract class colleague {// hold a mediation object private mediator;/*** constructor */Public colleague (mediator) {This. mediator = mediator;}/*** get the mediator object corresponding to the current colleague class */public mediator getmediator () {return mediator ;}}

Colleagues-Optical Drive

Package COM. bankht. mediator;/*** @ Author: -AK47 * @ Creation Time: 03:37:44, January 3, ** @ Class description: colleague class-optical drive */public class cddriver extends colleague {// Private string data = "";/*** constructor */Public cddriver (mediator) {super (mediator);}/*** get the data read from the disc */Public String getdata () {return data ;} /*** read the disc */Public void readcd () {// The data displayed in the video before the comma (,) and the sound after the comma. data = "one piece, I decided on one piece"; // notifies the motherboard that its status has changed to getmediator (). changed (this );}}

Colleague class-CPU

Package COM. bankht. mediator;/*** @ Author: -AK47 * @ Creation Time: 03:38:17, January 3, ** @ Class description: colleagues' class-CPU */public class CPU extends colleags {// The decomposed video data private string videodata = ""; // The decomposed audio data private string sounddata = ""; /*** constructor */Public CPU (mediator) {super (mediator);}/*** get the decomposed video data */Public String getvideodata () {return videodata;}/*** get the decomposed sound data */Public String getsounddata () {return sounddata;}/*** process the data, split the data into audio and video data */Public void executedata (string data) {// split the data into video data, followed by the audio data string [] array = data. split (","); this. videodata = array [0]; this. sounddata = array [1]; // notifies the motherboard that the CPU is finished. getmediator (). changed (this );}}

Colleague class-video card

Package COM. bankht. mediator;/*** @ Author: -AK47 * @ Creation Time: 03:38:37, January 3, ** @ Class description: colleague class-video card */public class videocard extends colleague {/*** constructor */Public videocard (mediator) {super (mediator );} /*** display video data */Public void showdata (string data) {system. out. println ("You are watching:" + Data );}}

Colleagues-Sound Card

Package COM. bankht. mediator;/*** @ Author: -AK47 * @ Creation Time: 03:38:59, January 3, ** @ Class description: colleague class-sound card */public class Soundcard extends colleague {/*** constructor */Public Soundcard (mediator) {super (mediator );} /*** sound Based on Audio Frequency Data */Public void sounddata (string data) {system. out. println ("Voiceover:" + Data );}}

Abstract mediator class

Package COM. bankht. mediator;/*** @ Author: -AK47 * @ Creation Time: 03:39:18, January 3, ** @ Class description: abstract The mediator class */public interface mediator {/*** colleague object to notify the mediator method when it changes so that the mediator is responsible for interacting with other colleagues' objects */Public void changed (colleague C );}

Specific Mediators

Package COM. bankht. mediator;/*** @ Author: -AK47 * @ Creation Time: 03:39:43, January 3, ** @ Class description: specific mediator class */public class mainboard implements mediator {// The colleague class to be interacted with-drive class private cddriver = NULL; // You Need To Know the colleague class to interact with-CPU class private CPU = NULL; // You Need To Know the colleague class to interact-graphics class private videocard = NULL; // You Need To Know the colleague class to interact with-the 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;} @ overridepublic void changed (colleague c) {If (C instanceof cddriver) {// indicates that the optical drive has read data this. opecddriverreaddata (cddriver) C);} else if (C instanceof CPU) {This. opecpu (CPU) C);}/*** interaction with other objects after the optical drive reads data */private void opecddriverreaddata (cddriver CD) {// obtain the data read by the optical drive string data = CD. getdata (); // transmits the data to the CPU for processing cpu.exe cutedata (data);}/*** interaction with other objects after processing CPU data */private void opecpu (CPU) {// obtain the data processed by the CPU string videodata = CPU. getvideodata (); string sounddata = CPU. getsounddata (); // pass the data to the video card and sound card to display the video card. showdata (videodata); Soundcard. sounddata (sounddata );}}

Client type

Package COM. bankht. mediator;/*** @ Author: -AK47 * @ Creation Time: 03:40:07, January 3, ** @ Class description: client class */public class client {public static void main (string [] ARGs) {// create a dispatcher -- mainboard mediator = new mainboard (); // create a colleague class cddriver Cd = new cddriver (mediator); CPU = new CPU (mediator); videocard Vc = new videocard (mediator ); soundcard SC = new Soundcard (mediator); // Let the dispatcher know all the colleagues mediator. setcddriver (CD); mediator. setcpu (CPU); mediator. setvideocard (VC); mediator. setsoundcard (SC); // starts to watch a movie, puts the CD into the optical drive, and starts to read the CD. readcd ();}}

The running result is as follows:

What you are watching is: one piece Voiceover: one piece.

 

Advantages of the Mediator Mode

●Loose coupling

The Interworking mode encapsulates interaction between multiple colleague objects into the called object, so that the colleague objects are loosely coupled and can be complementary. In this way, the objects of colleagues can be changed and reused independently, instead of "taking one place and moving the whole body" as before.

●Centralized interaction control

Interaction between multiple colleague objects is encapsulated in the mediation object for centralized management, so that when these interactions change, you only need to modify the mediation object, of course, if it is a ready system, it will expand the object of the mediation, and each colleague class does not need to be modified.

●Multiple to multiple becomes one to multiple

When the Paster mode is not used, the relationship between the colleague objects is usually many to many. After the Paster object is introduced, the relationship between the Paster object and the colleague object is usually changed to two-way one to many, this makes the relationship between objects easier to understand and implement.

Disadvantages of the Mediator Mode

One potential disadvantage of the mediation model is that it is overly centralized. If the interaction between colleagues and objects is very large and complex, when these complexities are all concentrated to the callers, it will lead to the complex and difficult to manage and maintain the callers.

 

 

 

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.