1
edit
Changes
Mediator
,→Example
==Explain==
==Example==
===General Java Example===
This example is a general example taken from [[http://my.execpc.com/~gopalan/design/behavioral/mediator/mediator.html Execpc]]
<pre>
//
// Mediator.java
//
import java.io.*;
abstract class Mediator
{
public abstract void colleagueChanged(Colleague theChangedColleague);
public static void main(String args[])
{
ConcreteMediator aConcreteMediator = new ConcreteMediator();
aConcreteMediator.createConcreteMediator();
(aConcreteMediator.getOneColleage()).Display();
(aConcreteMediator.getNextColleague()).Display();
OneColleague newColleague = new OneColleague(aConcreteMediator, "OneColleague");
aConcreteMediator.colleagueChanged( (OneColleague)newColleague );
(aConcreteMediator.getOneColleage()).Display();
(aConcreteMediator.getNextColleague()).Display();
}
}
//
// Colleague.java
//
abstract class Colleague
{
private Mediator _aMediator;
public Colleague(Mediator m)
{
_aMediator = m;
}
public void changed()
{
_aMediator.colleagueChanged(this);
}
public Mediator getMediator()
{
return(_aMediator);
}
public abstract void Display();
}
//
// ConcreteMediator.java
//
class ConcreteMediator extends Mediator
{
private OneColleague _aOneColleague;
private NextColleague _aNextColleague;
public void colleagueChanged( Colleague theChangedColleague )
{
if ((( OneColleague)theChangedColleague).getSelection().equals( _aOneColleague.getSelection() ) )
{
_aNextColleague.setText( _aOneColleague.getSelection() );
}
}
public void createConcreteMediator()
{
_aOneColleague = new OneColleague(this, "OneColleague");
_aNextColleague = new NextColleague(this, "NextColleague");
}
public OneColleague getOneColleage()
{
return(_aOneColleague);
}
public NextColleague getNextColleague()
{
return(_aNextColleague);
}
}
//
// OneColleague.java
//
class OneColleague extends Colleague
{
private String _text;
public OneColleague(Mediator m, String t)
{
super( m );
_text = t;
}
public void setText(String text)
{
_text = text;
}
public String getSelection()
{
return(_text);
}
public void Display()
{
System.out.println("OneColleague = " + _text);
}
}
</pre>
===LimeWire===
LimeWire uses several mediators in their coding. This Mediator is used to control the tip of the day window in limewire.