Properties:
- Provides communication between set of classes without communicating classes needing to know each other. Thus mediator pattern helps to reduce coupling.
- If number of communicating classes is high, mediator class may get too complicated.
Implementation:
- A mediator interface declares actions of the mediator.
- Concrete Meadiator implementations of mediator defines those actions.
- Concrete Colleagues are classes that communicates.
Java Standard Library Implementations:
- javax.swing.ButtonGroup
Example Usage:
//Create the radio buttons.
JRadioButton firstButton = new JRadioButton("FirstButton"); // colleague 1
JRadioButton secondButton = new JRadioButton("SecondButton"); // colleague 2
JRadioButton thirdButton = new JRadioButton(dogString); // colleague 3
firstButton.setSelected(true);
// Once a button is selected, other buttons must be de-selected.
// So buttons must communicate.
ButtonGroup group = new ButtonGroup();// Mediator class
group.add(firstButton);
group.add(secondButton);
group.add(thirdButton);
Comments
Post a Comment