首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >中介者模式和创建

中介者模式和创建
EN

Stack Overflow用户
提问于 2011-06-21 08:14:14
回答 2查看 684关注 0票数 3

我在演示文稿中涉及了几个需要相互交互的“小部件”,但是交互已经变得足够复杂,需要一个新的对象来处理这些交互。

在尝试通过中介器作为该对象时,我对如何有效地构造参与者感到困惑。中介者必须了解小部件,并且小部件必须了解中介者。

使用下面的玩具类,谁能告诉我构造函数是什么样子的,以及它们通常是按照什么顺序创建的?

干杯,

Berryl

代码语言:javascript
复制
class WidgetOne {       
    Mediator _mediator;
} 

class WidgetTwo {       
    Mediator _mediator;
} 

class Mediator {    
    WidgetOne _widgetOne;
    WidgetTwo _widgetTwo;               
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-06-21 10:03:56

这真的取决于许多其他情况,但我可能会这样做:

代码语言:javascript
复制
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);

当然,如果不需要了解这些小部件,那么我会去掉setter,只需要以下代码:

代码语言:javascript
复制
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
    }
}

简而言之,还有几个……简表:

代码语言:javascript
复制
// 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);}
}
票数 2
EN

Stack Overflow用户

发布于 2011-06-21 09:58:48

您没有指定语言,所以我将尽可能保持它的通用性。

代码语言:javascript
复制
abstract class Participant {
    public string Notify(string message);
}

代码语言:javascript
复制
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);
    }
} 

代码语言:javascript
复制
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);
    }
}

代码语言:javascript
复制
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;
    }

}

代码语言:javascript
复制
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");
    }
}

因此,在较高的级别上,您有两个参与者想要交谈,因此您需要设置一个公共接口来实现此目的。

我们创建了一个方法调用Notify( communication.

  • To );,基本上就是你设置的通道,我们实例化一个中介器,然后实例化两个参与者,传递给它们中介器,最后一步insetup,是注入/设置中介器参与者。在我们的例子中,我们只是使用简单的setters.

  • When来进行通信,每个参与者只是调用中介者,传递消息和self作为参数。

  • 中介者查看是谁联系了他们,然后调用相反的方法。

如果你有任何问题,请让我知道,这个模式显然有许多变体,所以如果你想看其他东西,请让我知道。

保重。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6419004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档