Mediator pattern and creation

混江龙づ霸主 提交于 2019-12-06 03:26:19

That really depends on a number of other circumstances, but I would probably do this:

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

Of course, if nothing else needs to know about the widgets, then I would get rid of the setters and just have this:

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

A couple of others in brief... brief form:

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

You didn't specify a language so I will keep it as generic as possible.

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

So at a highlevel, you have 2 Participants that want to talk so you need to setup a common interface to do so.

  1. We create a method call Notify(message); that basically is your channel of communication.

  2. To set things up we instantiate a mediator, and then instantiate both participants passing them the mediator.

  3. The last step insetup, is to inject/set the mediators participants. In our case we just use simple setters.

  4. When its time to communicate, each participant just calls the mediator, passes the message and self as a parameter.

  5. Mediator see who contacted them, and then calls the opposite.

Please let me know if you have any questions, there are obviously numerous variations of this pattern, so let me know if there is something else you would like to see.

Take care.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!