【问题标题】:Mediator pattern and creation中介者模式和创建
【发布时间】:2011-06-21 00:14:14
【问题描述】:

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

在尝试将 Mediator 作为该对象进行工作时,我对如何有效地构建参与者感到困惑。中介者必须知道小部件,小部件必须知道中介者。

有人可以使用下面的玩具类向我展示构造函数的外观以及它们通常以什么顺序创建?

干杯,
浆果

class WidgetOne {       
    Mediator _mediator;
} 

class WidgetTwo {       
    Mediator _mediator;
} 

class Mediator {    
    WidgetOne _widgetOne;
    WidgetTwo _widgetTwo;               
}

【问题讨论】:

    标签: design-patterns mediator


    【解决方案1】:

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

    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 并只拥有这个:

    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);}
    }
    

    【讨论】:

    • 是的,我喜欢这样。令我惊讶的是,我在 Google 搜索的任何明显网站中都没有发现创建此问题是一个问题,尽管我很确定 GoF 涵盖了它。你能把脑海中浮现的其他一些情况抛诸脑后吗?干杯
    【解决方案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. 调解员看看谁联系了他们,然后打电话给相反的人。

    如果您有任何问题,请告诉我,这种模式显然有很多变体,所以如果您还想看到其他内容,请告诉我。

    保重。

    【讨论】:

    • 很棒的答案。我也在考虑使用某种观察者模式,但对于给定的演示文稿,它似乎松散耦合(我正在用 c# 做一些 MVVM 类型的东西)。我需要做更多的工作。干杯
    • 你总是可以做到的,有时当我必须说明如何使用中介时,我展示了一个利用观察者模式 + 中介的 pub sub 样式系统。您基本上可以执行 mediator.send(from, topic, message) 之类的操作,然后执行 subscribe/register 之类的操作来设置观察者。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多