Analysis of Android source code design patterns and practices (17th)
Chapter 2 intermediary Mode
The intermediary mode, also known as the mediation mode or the mediation mode, is a behavior mode.
1. Definition
The intermediary mode encapsulates a series of ways in which objects interact, so that these objects do not have to interact explicitly. So that they can be loosely coupled. When the roles of some objects change, the roles of other objects are not immediately affected. Ensure that these functions can be changed independently of each other.
2. Use Cases
When there are many interaction operations between objects and each object's behavior operations is dependent on each other, to prevent modifying the behavior of an object and involving the behavior of many other objects at the same time, you can use the intermediary mode.
3. UML class diagram
(1) Mediator:Abstract intermediary roleDefines the interface between the colleague object and the intermediary object, which is generally implemented in an abstract class.
(2) ConcreteMediator:Intermediary roleIt inherits from the abstract intermediary and implements the parent class definition method. It receives messages from a specific colleague object and sends commands to a specific colleague object.
(3) Colleague:Abstract colleague roleDefines the interface of the intermediary object. It only knows the intermediary but does not know other colleagues' objects.
(4) ConcreteColleague1 and ConcreteColleague2:Specific colleagues' rolesIt inherits from abstract colleagues' classes. Each specific colleagues' class knows its behavior in a small scope, but does not know its purpose in a large scope.
4. Simple implementation
In the computer, the host is mainly divided into: CPU, memory, graphics card, IO device, and the main board is integrated with them, where the main board is an intermediary. Take this as an example.
Abstract intermediary:
Public abstract class Mediator {/*** how to notify the intermediary when a colleague object changes * when a colleague object changes, the intermediary notifies another colleague object ** @ param c colleague object */public abstract void changed (Colleague c );}
Abstract colleague:
Public abstract class Colleague {protected Mediator mediator; // each Colleague should know its intermediary public Colleague (Mediator mediator) {this. mediator = mediator ;}}
CPU colleagues:
Public class CPU extends Colleague {private String dataVideo, dataSound; // public CPU (Mediator mediator) {super (mediator );} /*** get video data ** @ return video data */public String getDataVideo () {return dataVideo ;} /*** obtain audio data ** @ return audio data */public String getDataSound () {return dataSound ;} /***** decoded data ** @ param data audio, video data */public void decodeData (String data) {// split audio, video data String [] tmp = data. split (","); // parses audio and video data dataVideo = tmp [0]; dataSound = tmp [1]; // tells the intermediary that its status changes mediator. changed (this );}}
Drive colleagues:
Public class CDDevice extends Colleague {private String data; // video data public CDDevice (Mediator mediator) {super (mediator );} /*** read video data ** @ return video data */public String read () {return data ;} /*** load video data ** @ return audio data */public void load () {data = "video data, audio data "; // tell the intermediary to change the status of the mediator. changed (this );}}
Graphics colleagues:
Public class GraphicsCard extends Colleague {public GraphicsCard (Mediator mediator) {super (mediator );} /*** play video data ** @ param video data */public void videoPlay (String data) {System. out. println ("Video:" + data );}}
Sound Card colleagues:
Public class SoundCard extends Colleague {public SoundCard (Mediator mediator) {super (mediator );} /*** play audio data ** @ param audio data */public void soundPlay (String data) {System. out. println ("audio:" + data );}}
Motherboard intermediary:
Public class MainBoard extends Mediator {private CDDevice cdDevice; // private CPU of the optical drive device; // private SoundCard soundCard; // private GraphicsCard graphicsCard of the sound card device; // video card device @ Override public void changed (Colleague c) {// if the optical drive reads data if (c = cdDevice) {handleCD (CDDevice) c );} // if the CPU completes Data Processing else if (c = cpu) {handleCD (CPU) c );}} /*** process the interaction between the optical drive and other devices after reading data ** @ param cdDevice Optical Drive Device */public void handleCD (CDDevice cdDevice) {cpu. decodeData (cdDevice. read ();}/*** interaction with other devices after processing CPU read data ** @ param cpu CPU */public void handleCD (CPU cpu) {soundCard. soundPlay (cpu. getDataSound (); graphicsCard. videoPlay (cpu. getDataVideo ();}/*** set the CD device ** @ param CDDevice CD device */public void setCDDevice (CDDevice cdDevice) {this. cdDevice = cdDevice;}/*** set CPU ** @ param cpu CPU */public void setCPU (CPU cpu) {this. cpu = cpu;}/*** set the sound card device ** @ param soundCard sound card device */public void setSoundCard (SoundCard soundCard) {this. soundCard = soundCard;}/*** set the video card device ** @ param graphicsCard video card device */public void setGraphicsCard (GraphicsCard graphicsCard) {this. graphicsCard = graphicsCard ;}}
Play a movie:
Public class Client {public static void main (String [] args) {// construct the MainBoard mediator = new MainBoard (); // construct each part, CDDevice cd = new CDDevice (mediator), CPU = new cpu (mediator), and GraphicsCard gc = new GraphicsCard (mediator ); soundCard SC = new SoundCard (mediator); // install each part on the Main Board mediator. setCDDevice (cd); mediator. setCPU (cpu); mediator. setGraphicsCard (gc); mediator. setSoundCard (SC); // plays a movie cd. load ();}}
Result:
Audio: audio data video: video data
It can be seen that the intermediary mode converts the multi-to-many interaction into one-to-many interaction, and changes the system from a mesh structure to a star structure centered on the intermediary (here the main board ), to reduce system complexity and improve scalability.
5. intermediary mode in Android Source Code 6. Summary
In fact, in Android development, we may accidentally use the intermediary mode. For example, on the login registration page, we use addTextChangedListener of EditText to listen to the number of digits of the entered password and whether the user name is empty, when determining whether the password is consistent with the password, multiple controls interact at this time, that is, the Activity acts as the intermediary to coordinate.
1. Advantages
(1) using the intermediary mode appropriately can avoid excessive coupling between colleague classes, so that each colleague class can be used relatively independently.
(2) The intermediary mode can be used to abstract the behavior and collaboration of objects and flexibly process the interaction between objects.
(3) using the intermediary mode, you canMany-to-manyToOne-to-multipleMakes the relationship between objects easy to understand and maintain.
2. Disadvantages
Intermediary mode is a common mode and a mode that is easy to abuse. In most cases, the relationships between colleague classes are not complex to messy mesh structures. Therefore, in most cases, the inter-object dependency can be encapsulated inside the colleague classes, there is no need to introduce the intermediary mode. Misuse of the intermediary model only makes things more complicated. Therefore, we have to consider and weigh the advantages and disadvantages of using the intermediary model.