Home
Core Java
Java
JDBC
Servlets
JSP
EJB
JMS
Struts
Spring
Hibernate
JSF
RMI
CORBA
J2ME
Performance
Tomcat
Weblogic
Design Patterns
Junit
XML
UML
DB2 & SQL
ANT
Free Gift
Contact Us

XML RSS
What is this?
Add to My Yahoo!
Add to My MSN
Add to Google
 

Behavioral Pattern

Mediator Pattern

Your Ad Here

Creational patterns Structural patterns Behavioral pattern J2EE patterns
Abstract Factory Adapter  Chain of Responsibility
MVC 
Builder Bridge  Command  Business Delegate
Factory method Composite  Interpreter  Composite Entity
Prototype Decorator  Iterator  Data Access Object
Singleton Façade
Mediator  Front Controller

Flyweight  Memento  Intercepting Filter

Proxy  Observer  Service Locator


State  Transfer Object


Strategy 


Template Method



Visitor 

Mediator Pattern



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

Where to use & benefits
Partition a system into pieces or small objects.
Centralize control to manipulate participating objects(a.k.a colleagues)
Clarify the complex relationship by providing a board committee.
Limit subclasses.
Improve objects reusabilities.
Simplify object protocols.
The relationship between the control class and other participating classes is multidirectional.

Related patterns include
  • Facade, which abstracts a subsystem to provide a more convenient interface, and its protocol is unidirectional, whereas a mediator enables cooperative behavior and its protocol is multidirectional.
  • Command, which is used to coordinate functionality.
  • Observer, which is used in mediator pattern to enhance communication.

Your Ad Here
Example of mediator pattern
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.

Your Ad Here




footer for Mediator Pattern page