<
 

Mediator


Definition

Define an object that encapsulates details and other objects interact with such object. The relationships are loosely decoupled.

Where to use & benefits

Example

If you have a complex GUI, whenever a button has been clicked, the related actions should be disabled or enabled. You may design a Mediator class to include all related classes:

interface Command {
    void execute();
}
class Mediator {
    BtnView btnView;
    BtnSearch btnSearch;
    BtnBook btnBook;
    LblDisplay show;;
	//....
    void registerView(BtnView v) {
        btnView = v;
    }
    void registerSearch(BtnSearch s) {
        btnSearch = s;
    }
    void registerBook(BtnBook b) {
        btnBook = b;
    }
    void registerDisplay(LblDisplay d) {
        show = d;
    }
    void book() {
       btnBook.setEnabled(false);
       btnView.setEnabled(true);
       btnSearch.setEnabled(true);
       show.setText("booking...");
    }
    void view() {
       btnView.setEnabled(false);
       btnSearch.setEnabled(true);
       btnBook.setEnabled(true);
       show.setText("viewing...");
    }
    void search() {
       btnSearch.setEnabled(false);
       btnView.setEnabled(true);
       btnBook.setEnabled(true);
       show.setText("searching...");
    }
}

Then, you may define classes which should be controlled by the Mediator class.

class BtnView extends JButton implements Command {
    Mediator med;
    BtnView(ActionListener al, Mediator m) {
        super("View");
        addActionListener(al);
        med = m;
        med.registerView(this);
    }
    public void execute() {
       med.view();
    }
}

class BtnSearch extends JButton implements Command {
    Mediator med;
    BtnSearch(ActionListener al, Mediator m) {
        super("Search");
        addActionListener(al);
        med = m;
        med.registerSearch(this);
    }
    public void execute() {
       med.search();
    }
}

class BtnBook extends JButton implements Command {
    Mediator med;
    BtnBook (ActionListener al, Mediator m) {
        super("Book");
        addActionListener(al);
        med = m;
        med.registerBook(this);
    }
    public void execute() {
       med.book();
    }
}

class LblDisplay extends JLabel{
    Mediator med;
    LblDisplay (Mediator m) {
        super("Just start...");
        med = m;
        med.registerDisplay(this);
        setFont(new Font("Arial",Font.BOLD,24));
    }
}

From the above design, you can see that the relationships among the classes, which also known as collegues or participating classes, are multidirectional. Mediator class contains all the information about these classes and knows what these classes are going to do. The participating classes have to register themselves to the Mediator class.

The MediatorDemo class will show the cooperation among the classes.

class MediatorDemo extends JFrame implements ActionListener {
    Mediator med = new Mediator();
    MediatorDemo() {
       JPanel p = new JPanel();
       p.add(new BtnView(this,med));
       p.add(new BtnBook(this,med));
       p.add(new BtnSearch(this, med));
       getContentPane().add(new LblDisplay(med), "North");
       getContentPane().add(p, "South");
       setSize(400,200);
       setVisible(true);
    
    }
    public void actionPerformed(ActionEvent ae) {
        Command comd = (Command)ae.getSource();
        comd.execute();
    }
	public static void main(String[] args) {
	    new MediatorDemo();
    }
}

The following is a complete code for the above program.

interface Command {
    void execute();
}
class Mediator {
    BtnView btnView;
    BtnSearch btnSearch;
    BtnBook btnBook;
    LblDisplay show;;
    //....
    void registerView(BtnView v) {
        btnView = v;
    }
    void registerSearch(BtnSearch s) {
        btnSearch = s;
    }
    void registerBook(BtnBook b) {
        btnBook = b;
    }
    void registerDisplay(LblDisplay d) {
        show = d;
    }
    void book() {
       btnBook.setEnabled(false);
       btnView.setEnabled(true);
       btnSearch.setEnabled(true);
       show.setText("booking...");
    }
    void view() {
       btnView.setEnabled(false);
       btnSearch.setEnabled(true);
       btnBook.setEnabled(true);
       show.setText("viewing...");
    }
    void search() {
       btnSearch.setEnabled(false);
       btnView.setEnabled(true);
       btnBook.setEnabled(true);
       show.setText("searching...");
    }
}
class BtnView extends JButton implements Command {
    Mediator med;
    BtnView(ActionListener al, Mediator m) {
        super("View");
        addActionListener(al);
        med = m;
        med.registerView(this);
    }
    public void execute() {
       med.view();
    }
}

class BtnSearch extends JButton implements Command {
    Mediator med;
    BtnSearch(ActionListener al, Mediator m) {
        super("Search");
        addActionListener(al);
        med = m;
        med.registerSearch(this);
    }
    public void execute() {
       med.search();
    }
}

class BtnBook extends JButton implements Command {
    Mediator med;
    BtnBook (ActionListener al, Mediator m) {
        super("Book");
        addActionListener(al);
        med = m;
        med.registerBook(this);
    }
    public void execute() {
       med.book();
    }
}

class LblDisplay extends JLabel{
    Mediator med;
    LblDisplay (Mediator m) {
        super("Just start...");
        med = m;
        med.registerDisplay(this);
        setFont(new Font("Arial",Font.BOLD,24));
    }
}
class MediatorDemo extends JFrame implements ActionListener {
    Mediator med = new Mediator();
    MediatorDemo() {
       JPanel p = new JPanel();
       p.add(new BtnView(this,med));
       p.add(new BtnBook(this,med));
       p.add(new BtnSearch(this, med));
       getContentPane().add(new LblDisplay(med), "North");
       getContentPane().add(p, "South");
       setSize(400,200);
       setVisible(true);
    
    }
    public void actionPerformed(ActionEvent ae) {
        Command comd = (Command)ae.getSource();
        comd.execute();
    }
	public static void main(String[] args) {
	    new MediatorDemo();
    }
}
java MediatorDemo

A window will pop up. Try the features.

Return to top