Search code examples
design-patternsmediator

Mediator pattern and creation


I have several "widgets" involved in a presentation that need to interact with each other, but the interactions have gotten complex enough to warrant a new object to handle the interactions.

In trying to work through a Mediator as that object, I am confused as how to construct the participants effectively. The mediator has to know about the widgets, and the widgets have to know about the mediator.

Using the toy classes below can someone show me how the constructors would look and in what order they would typically be created?

Cheers,
Berryl

class WidgetOne {       
    Mediator _mediator;
} 

class WidgetTwo {       
    Mediator _mediator;
} 

class Mediator {    
    WidgetOne _widgetOne;
    WidgetTwo _widgetTwo;               
}

Solution

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