あなたは言語を指定しなかったので、私はそれを可能な限り一般的にします。
abstract class Participant {
public string Notify(string message);
}
class WidgetOne extends Participant {
Mediator _mediator;
public WidgetOne(Mediator theMediator){
_mediator = theMediator;
}
public string Notify(string message){
#do whatever
}
public string Talk(string message){
return _mediator.Talk(message, this);
}
}
class WidgetTwo extends Participant {
Mediator _mediator;
public WidgetOne(Mediator theMediator){
_mediator = theMediator;
}
public string Notify(string message){
#do whatever
}
public string Talk(string message){
return _mediator.Talk(message, this);
}
}
class Mediator {
WidgetOne _widgetOne;
WidgetTwo _widgetTwo;
public void setWidgetOne(WidgetOne theWidget){
_wiidgetOne = theWidget;
}
public void setWidgetTwo(WidgetTwo theWidget){
_wiidgetTwo = theWidget;
}
public string Talk(string message, Participant p){
#make sure you do the correct ==/equals/etc.
if(p == _widgetOne){
response = _widgetTwo.Notify(message);
}else if (p == _widgetTwo){
response = _widgetOne.Notify(message);
}
return response;
}
}
class Main {
public void run(){
Mediator theMediator = new Mediator();
WidgetOne one = new WidgetOne(theMediator);
WidgetTwo two = new WidgetTwo(theMediator);
theMediator.setWidgetOne(one);
theMediator.setWidgetTwo(two);
one.Talk("hi there");
}
}
つまり、大まかに言えば、話したい参加者が2人いるので、そのための共通のインターフェイスを設定する必要があります。
Notify(message);というメソッド呼び出しを作成します。それは基本的にあなたのコミュニケーションのチャネルです。
設定するために、メディエーターをインスタンス化し、次に両方の参加者をインスタンス化して、メディエーターを渡します。
セットアップの最後のステップは、メディエーターの参加者を注入/設定することです。この場合、単純なセッターを使用します。
通信する時間になると、各参加者はメディエーターを呼び出し、メッセージと自己をパラメーターとして渡します。
調停人は、誰が彼らに連絡したかを確認し、反対側に電話をかけます。
ご不明な点がございましたら、このパターンには明らかにバリエーションがたくさんありますので、他に見たいものがあればお知らせください。
気をつけて。