Monday, June 23, 2008

Mediator Design Patter

I present this post an useful adaptation of Mediator Design Pattern. We need often create GUI forms and of course we want to process the information given by users.

Participants of Mediator Design Patter
(based on Wikipedia)

Mediator - defines the interface for communication between Colleague objects.

ConcreteMediator - implements the Mediator interface and coordinates communication between Colleague objects. It is aware of all the Colleagues and their purpose with regards to inter communication.

ConcreteColleague - communicates with other Colleagues through its Mediator



Step 1: Create the Mediator interface
In the 1rst step we create the mediator interface:

1. public interface Mediator {
2. void setData( HashMap<String, Object> data );
3. HashMap<String, Object> getData();
4. }

Import the required classes and our Mediator is done.
Object will communicate over this interface with each other.


Step 2: Create the form
We create a simple GUI frame with two JTextField and two JButton, like this:



Step 3: Create the ConcreteMediator
To create the ConcreteMediator, implement the Mediator interface with our GUI Frame.
Change the
public class MediatorExample extends javax.swing.JFrame {

line to
public class MediatorExample extends javax.swing.JFrame imlements Mediator {

and implement the required methods, declared in Mediator interface:
public void setData(HashMap data) {
textfield_info1.setText((String) data.get("info1"));
textfield_info2.setText((String) data.get("info2"));
}
and
public HashMap getData() {
HashMap data = new HashMap();
data.put("info1", textfield_info1.getText());
data.put("info2", textfield_info2.getText());
return data;
}

Import the required classes and our ConcreteMediator is done (textfield_info1 and textfield_info2 are the name of two JTextField instance variable).


Step 4: Create the ConcreteColleague
In our example the ConcreteColleague has two function:
it can open a new Frame and fill it with initial data throught the Mediator interface
it can process form data throught Mediator interface.
My code:
public class ImportantDataProcessor {

public void showImportantDataDialog() {
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("info1", "This information is very important!");
data.put("info2", "And Top Secret!");

MediatorExample frame = new MediatorExample();
frame.setData(data);
frame.setVisible(true);

}

public void processImportantData(Mediator mediator) {
HashMap<String,Object> data = mediator.getData();

String info1 = (String) data.get("info1");
String info2 = (String) data.get("info2");

JOptionPane.showMessageDialog(null, info1 + "\n" + info2, "Important Information",
JOptionPane.INFORMATION_MESSAGE);
}
}

Import the required classes and our ConcreteColleague is done .


Step 5: Process the form
Now we write the onClick() events of the two buttons.
Exit button will close the application:
this.dispose();
and the Process... button will start the form processing:
new ImportantDataProcessor().processImportantData(this);
The "this" word refer to the GUI frame itself, what implements the Mediator interface.


Step 6: Start the application
Create a class, named Main, and put the following code:
public static void main(String[] args) {
new ImportantDataProcessor().showImportantDataDialog();
}


Downloads
You can download sample code here as NetBeans project.

No comments: