728x90 AdSpace

Monday, 31 July 2017

Design Patterns - Mediator Pattern

Mediator Pattern

Definition

With the mediator pattern communication between objects is encapsulated with a mediator object. Objects no longer communicate directly with each other, but instead communicate through the mediator. This results in a more cohesive implementation of the logic and decreased coupling between the other objects.

Where to use

The Mediator pattern can be used when a set of objects communicate in well-specified but complex ways and the resulting interdependencies are unstructured and hard to grasp. If it is difficult to reuse an object because it refers to and communicates with many other objects this pattern is a good
solution. Communicating objects' identities will be protected when using the Mediator pattern which is good when security is important. Also, if you like to customize some behavior which is spread out between several classes without a lot of subclassing this pattern should be applied. The pattern is
used in many modern systems that reflect a send/receive protocol, such as list servers and chat rooms. Another area of use is graphical user interfaces, where the mediator can encapsulate a collective behavior to control and coordinate the interactions of a group of GUI widgets. The mediator serves
as an intermediary that keeps objects in the group from referring to each other explicitly. The objects only know the mediator which reduces the number of interconnections.

Benefits

The Mediator pattern has five benefits and drawbacks:
1. Limited subclassing, since a mediator localizes behavior that otherwise would be distributed among several objects. Changing some behavior requires us to subclass only the mediator.
2. Colleagues become decoupled which allows us to vary and reuse colleague and mediator classes independently.
3. A mediator simplifies object protocols since it replaces many-to-many interactions with one-to-many interactions between the mediator and its colleagues. One-to-many interactions are easier to understand, maintain, and extend.
4. The mediator abstracts object cooperation. The mediator lets you focus on how objects interact apart from their individual behaviors which can help clarify how objects interact in a system.
5. Centralized control is achieved by trading complexity of interaction for complexity in the mediator. A mediator encapsulates protocols and can become more complex than any individual colleague. This can make the mediator itself a very complex and large piece of code that is hard to maintain.

Drawbacks/consequences

Besides the benefits and drawbacks described above, one important drawback is that the Mediator pattern can have a performance impact on a system. Since all communication must go through the mediator, it can become a bottleneck.

 Mediator Pattern Class Diagram



This is the class diagram of the small example that follows below.

 Mediator Pattern example

Consider a dialog where there are a display label and three buttons: view, book and search. When clicking the view button it should be disabled and its two colleague buttons should be enabled. Also, the display label should change to reflect which button was pressed. We create four classes ButtonView, ButtonBook, ButtonSearch and LabelDisplay. Then we let the button classes implement an interface Command which allows us to execute the actions of the buttons from a common interface. All of these GUI classes are unaware of each other; they should only refer to the mediator. We now create the Mediator class which holds references to all the GUI objects to control and coordinate the interactions of these objects. Finally, we create the MediatorExample class to display the components in a frame. The class implements the ActionListener interface to which all the buttons should register. The complete source code of this example is shown below:

import javax.swing.JButton;
public class ButtonView extends JButton implements Command {
Mediator mediator;
public ButtonView(ActionListener listener, Mediator mediator){
super("View");
addActionListener(listener);
this.mediator = mediator;
mediator.registerView(this);
}
public void execute() {
mediator.view();
}
}
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class ButtonSearch extends JButton implements Command {
Mediator mediator;
ButtonSearch(ActionListener listener, Mediator mediator{
super("Search");
addActionListener(listener);
this.mediator = mediator;
mediator.registerSearch(this);
}
public void execute() {
mediator.search();
}
}
public interface Command {
public void execute();
}

public class Mediator {
ButtonView buttonView;
ButtonBook buttonBook;
ButtonSearch buttonSearch;
LabelDisplay labelDisplay;
public void registerView(ButtonView buttonView) {
this.buttonView = buttonView;
}
public void registerBook(ButtonBook buttonBook) {
this.buttonBook = buttonBook;
}
public void registerSearch(ButtonSearch buttonSearch) {
this.buttonSearch = buttonSearch;
}
public void registerDisplay(LabelDisplay labelDisplay) {
this.labelDisplay = labelDisplay;
}
public void view() {
buttonView.setEnabled(false);
buttonBook.setEnabled(true);
buttonSearch.setEnabled(true);
labelDisplay.setText("Viewing...");
}
public void book() {
buttonBook.setEnabled(false);
buttonView.setEnabled(true);
buttonSearch.setEnabled(true);
labelDisplay.setText("Booking...");
}
public void search() {
buttonSearch.setEnabled(false);
buttonBook.setEnabled(true);
buttonView.setEnabled(true);
labelDisplay.setText("Searching...");
}
}

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
class MediatorExample extends JFrame implements ActionListener {
Mediator mediator = new Mediator();
public MediatorExample() {
JPanel p = new JPanel();
p.add(new ButtonView(this, mediator));
p.add(new ButtonBook(this, mediator));
p.add(new ButtonSearch(this, mediator));
getContentPane().add(new LabelDisplay(mediator),
BorderLayout.NORTH);
getContentPane().add(p, BorderLayout.SOUTH);
setTitle("Mediator Example");
setSize(300, 200);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof Command) {
Command c = (Command)e.getSource();
c.execute();
}
}
public static void main(String[] args) {
new MediatorExample();
}
}
When mediatorExample is executed the result is:

Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Design Patterns - Mediator Pattern Rating: 5 Reviewed By: Annamalai Thangaraj