3

相互に対話する必要があるプレゼンテーションに関連する「ウィジェット」がいくつかありますが、相互作用は、相互作用を処理する新しいオブジェクトを保証するのに十分なほど複雑になっています。

メディエーターをそのオブジェクトとして処理しようとすると、参加者を効果的に構築する方法がわかりません。メディエーターはウィジェットについて認識している必要があり、ウィジェットはメディエーターについて認識している必要があります。

以下のおもちゃのクラスを使用して、コンストラクターがどのように見えるか、およびそれらが通常どのような順序で作成されるかを誰かが示すことができますか?

乾杯、
ベリル

class WidgetOne {       
    Mediator _mediator;
} 

class WidgetTwo {       
    Mediator _mediator;
} 

class Mediator {    
    WidgetOne _widgetOne;
    WidgetTwo _widgetTwo;               
}
4

2 に答える 2

2

あなたは言語を指定しなかったので、私はそれを可能な限り一般的にします。

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人いるので、そのための共通のインターフェイスを設定する必要があります。

  1. Notify(message);というメソッド呼び出しを作成します。それは基本的にあなたのコミュニケーションのチャネルです。

  2. 設定するために、メディエーターをインスタンス化し、次に両方の参加者をインスタンス化して、メディエーターを渡します。

  3. セットアップの最後のステップは、メディエーターの参加者を注入/設定することです。この場合、単純なセッターを使用します。

  4. 通信する時間になると、各参加者はメディエーターを呼び出し、メッセージと自己をパラメーターとして渡します。

  5. 調停人は、誰が彼らに連絡したかを確認し、反対側に電話をかけます。

ご不明な点がございましたら、このパターンには明らかにバリエーションがたくさんありますので、他に見たいものがあればお知らせください。

気をつけて。

于 2011-06-21T01:58:48.417 に答える
2

それは実際には他の多くの状況に依存しますが、おそらくこれを行うでしょう:

class Mediator {    
    WidgetOne _widgetOne;
    WidgetTwo _widgetTwo;   

    void setWidgetOne(WidgetOne one){_widgetOne = one;}
    void setWidgetTwo(WidgetTwo one){_widgetTwo = one;}            
}

class WidgetOne {
    Mediator me
    void WidgetOne(Mediator me){
        this.me = me
        me.setWidgetOne(this);
    }
}

class WidgetTwo {
    Mediator me
    void WidgetTwo(Mediator me){
        this.me = me
        me.setWidgetTwo(this);
    }
}

Mediator me = new Mediator();
WidgetOne one = new WidgetOne(me);
WidgetTwo two = new WidgetTwo(me);

もちろん、ウィジェットについて他に何も知る必要がない場合は、セッターを取り除き、次のようにします。

class Mediator {    
    WidgetOne _widgetOne;
    WidgetTwo _widgetTwo;   

     void Mediator(){
        _widgetOne = new WidgetOne(this);
        _widgetTwo = new WidgetTwo(this);
     }            
}

class WidgetOne {
    Mediator me
    void WidgetOne(Mediator me){
        this.me = me
    }
}

class WidgetTwo {
    Mediator me
    void WidgetTwo(Mediator me){
        this.me = me
    }
}

他のいくつかの簡単な説明...簡単な形式:

// Factory:

class Mediator {    
    WidgetOne _widgetOne;
    WidgetTwo _widgetTwo;   

     void Mediator(){
        _widgetOne = WidgetFactory.getW1(this);
        _widgetTwo = WidgetFactory.getW2(this);
     }            
}

class W1 {
    Mediator me
    void W1(){
    }
    void setMediator(Mediator med){me = med}
}

class WidgetFactory {
    W1 getW1(Mediator me){ W1 w = new W1(); w.setMediator(me); return me}
}


// Centralized "model" (variant of factory)
class Mediator {
   W1 w1;

   static Mediator getInstance(){ return inst; }// See Singleton

   void registerW1(W1 w){w1 = w; w.setMediator(this);}
}
于 2011-06-21T02:03:55.657 に答える