define

A mediation object encapsulates a set of object interactions so that system objects do not need to explicitly call each other, allowing them to be loosely coupled and independently change their interactions.

nature

Encapsulation interaction

His role

  • Mediators are other mediators.

    The Mediator role is responsible for defining the interface to communicate and make decisions with the Colleague role.

  • ConcreteMediator: a concrete arbiter or mediator

    Implements the interface to the Mediator role, which is responsible for actually making the decision.

  • Colleague

    Responsible for defining the interface to communicate with the Mediator role.

  • Concrete Colleagues

    Interfaces responsible for implementing the Colleague role.

The sample code

public abstract class Mediator { abstract void change(Colleague c); } public class MainBoard extends Mediator{ private CDDevice cdDevice; // Private CPU CPU; //CPU private SoundCard soundCard; // Sound card device private GraphicsCard GraphicsCard; @override void change(Colleague c) {if(c == cdDevice){handlerCD((cdDevice) c); }else if(c == cpu){ handleCPU((CPU)c); } } private void handlerCD(CDDevice cdDevice){ cpu.decodeData(cdDevice.getDataVideo()); } private void handleCPU(CPU cpu){ soundCard.soundPlay(cpu.getDataSound()); graphicsCard.videoPlay(cpu.getDataVideo()); } public void setCdDevice(CDDevice cdDevice) { this.cdDevice = cdDevice; } public void setCpu(CPU cpu) { this.cpu = cpu; } public void setSoundCard(SoundCard soundCard) { this.soundCard = soundCard; } public void setGraphicsCard(GraphicsCard graphicsCard) { this.graphicsCard = graphicsCard; } } public abstract class Colleague { protected Mediator mediator; public Colleague(Mediator mediator) { this.mediator = mediator; } } public class CPU extends Colleague{ String dataVideo,dataSound; Public CPU(Mediator) {public CPU(Mediator) {public CPU(Mediator); } public String getDataVideo() { return dataVideo; } public void setDataVideo(String dataVideo) { this.dataVideo = dataVideo; } public String getDataSound() { return dataSound; } public void setDataSound(String dataSound) { this.dataSound = dataSound; } public void decodeData(String data){system.out.println ("CPU decodeData "); System.out.println(" Notify the mediator of its own state change "); mediator.change(this); } } public class CDDevice extends Colleague{ String dataVideo; // Public CDDevice(Mediator) {public CDDevice(Mediator); } public String getDataVideo() { return dataVideo; } public void load(){dataVideo = "dataVideo "; mediator.change(this); } } public class GraphicsCard extends Colleague{ public GraphicsCard(Mediator mediator) { super(mediator); } @param data */ public void videoPlay(String data){system.out.println (" Play video "); } } public class SoundCard extends Colleague{ public SoundCard(Mediator mediator) { super(mediator); } @param data */ public void soundPlay(String data){system.out.println (" play audio "); }} public class Client {public static void main(String[] args){// Construct new MainBoard(); CDDevice = new CDDevice(mediator); CPU cpu = new CPU(mediator); GraphicsCard graphicsCard = new GraphicsCard(mediator); SoundCard soundCard = new SoundCard(mediator); Mediator.setcddevice (cdDevice); mediator.setCpu(cpu); mediator.setGraphicsCard(graphicsCard); mediator.setSoundCard(soundCard); // Play video cddevice.load (); }}Copy the code

The results

The CPU decodes audio and video data to notify the mediator of its own state changes playing audio playing videoCopy the code

function

Encapsulate the interaction between objects. If an operation of one object causes changes to other objects, or if an operation needs to cause changes to other objects, and the object does not want to handle these relationships itself, the mediator pattern can be implemented.

advantages

  • Loose coupling

    The interaction between colleagues of multiple objects is encapsulated into the intermediary object, which makes colleague objects loosely coupled and basically independent of each other.

  • Centralized control interaction

    Interactions between multiple colleague objects are encapsulated in a mediator object for centralized management so that when these interactions change, only the mediator object can be changed.

  • Many-to-many becomes one-to-many

disadvantages

  • One potential drawback is excessive centralization.

Usage scenarios

  • If the communication between a group of objects is complex, resulting in interdependence and confusion, the mediator pattern can be used.
  • If an object references many objects and interacts directly with them, making it difficult to reuse the object, the mediator pattern can be used.