Design Patterns (9) - Mediator Pattern

mediator model

1. Definition

       Use an intermediary object to encapsulate a series of object interactions. Mediators make objects loosely coupled without explicit mutual references, and can independently alter their direct interactions.

2. Sample code

      

/*All objects that need to interact are regarded as colleague classes, and colleague classes hold mediator objects*/
pubic abstract class Colleague{
    // Holds the interface of the mediator object
    private Mediator mediator;
    public Colleague(Mediator mediator){
        this.mediator = mediator;
    }
    public Mediator getMediator(){
        return mediator;
    }
}

/* CD-ROM class, a colleague class, read CD-ROM data and transfer it to the motherboard */
public class CDDriver extends Colleague{
   public CDDriver(Mediator mediator){
       super (mediator);
   }
   /*Data read from CD-ROM*/
   private String data = "";
   /* Get read data */
   public String getData(){
      return this.data;
   }
   /*Read disc data*/
   public void readCD(){
      // Before the comma is the video display data, after the comma is the sound
      this.data = "Design pattern, worth studying";
      //Notify the motherboard that its state has changed
      this.getMediator().change(this);
   }
}

/*CPU class, a colleague class, parses the data and passes it to the motherboard*/
public class CPU extends Colleague{
    public CPU(Mediator mediator){
       super (mediator);
    }
    //decomposed video data and sound data
    private String videoData = "";
    private String soundData = "";
    public String getVideoData(){
        return videoData;
    }
    public String getSoundData(){
        return soundData;
    }
    /*Process data, parse out video and audio data*/
    public void executeData(String data){
        String[] ss = data.split(",");
        this.videoData = ss[0];
        this.soundData = ss[1];
        //Notify the motherboard that the cpu work is complete
        this.getMediator().change(this);
    }
}

/* Graphics card colleague class, responsible for displaying data */
pubic class VideoCard extends Colleague{
    public VideoCard(Mediator mediator){
       super (mediator);
    }
    /* display video data */
    pubilc void showData(String data){
       System.out.println("You are watching: " + data);
    }
}

/* Sound card colleague class, responsible for playing data */
pubic class SoundCard extends Colleague{
    public SoundCard(Mediator mediator){
       super (mediator);
    }
    /* display video data */
    pubilc void soundData(String data){
       System.out.println("画外音:" + data);
    }
}

   

/*Intermediary object interface*/
pubic interface Mediator{
    /* The method of the colleague object notifying the mediator when its own state changes */
    public void changed(Colleague colleague);
}

/* Mainboard class, implements the mediator object */
public class MotherBoard implements Mediator{
    /* Colleagues who need to know the interaction class - CD-ROM class */
    private CDDirver cdDriver = null ;
    /* Need to know the interaction class of colleagues - cpu class */
    private CPU cpu = null ;
    /* Colleague class that needs to know the interaction - graphics card class */
    private VideoCard videoCard = null ;
    /* Colleagues who need to know the interaction class - sound card class */
    private SoundCard soundCard = null ;
    public void setCdDriver(CDDriver cdDriver){
        this.cdDriver = cdDriver;
    }
    public void setCpu(CPU cpu){
        this.cpu= cpu;
    }
    public void setVideoCard(VideoCard videoCard){
        this.videoCard= videoCard;
    }
    public void setSoundCard(SoundCard soundCard){
        this.soundCard= soundCard;
    }
    public void changed(Colleague colleague){
         if(colleague == cdDriver){
            // CD-ROM has read data
            this.openCDDriverReadData((CDDriver)colleague);
         }else if(colleague == cpu){
            //CPU has parsed the data
            this.openCPU((CPU)colleague);
         }
    }
    /* Process the data read from the CD-ROM and interact with other objects */
    private void openCDDriverReadData(CDDriver cd){
         //Read data from CD-ROM
         String data = cd.getData();
         // send data to cpu for processing
         this.cpu.executeData(data);
    }
    /* Process the data parsed by the CPU and interact with other objects */
    private void openCPU(CPU cpu){
         //read data processed by cpu
         String videoData = cd.getVedioData ();
         String soundData = cd.getSoundData();
         // Hand over the data to the graphics card and sound card for processing
         this.videoCard.showData(videoData);
         this.soundData.soundData(soundData);
    }
}

   

/*Play the movie to indicate the operation*/
public class Client{
    public static void main(String args[]){
        //1. Create a mediator object
        MotherBoard mediator = new MotherBoard();
        //2. Create a colleague class
        CDDriver cd = new CDDriver(mediator);
        CPU cpu = new CPU(mediator);
        VideoCard vc = new VideoCard(mediator);
        SoundCard sc = new SoundCard(mediator);
        //3. Let the intermediary know all colleagues
        mediator.setCdDriver (cd);
        mediator.setCpu(cpu);
        mediator.setVideoCard(vc);
        mediator.setSoundCard(sc);
        //4. Open the CD-ROM to watch movies
        cd.readCD();
    }
}

 

3. Practical application

       The mediator pattern enables loose coupling between colleague objects by encapsulating the interaction between multiple colleague objects into the mediator object. The interaction of multiple colleague objects is encapsulated into the mediator for centralized management. The downside is over-centralization.

 

The essence of the mediator pattern: encapsulating interactions

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326387680&siteId=291194637