问题
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;
}
回答1:
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);}
}
回答2:
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.
We create a method call Notify(message); that basically is your channel of communication.
To set things up we instantiate a mediator, and then instantiate both participants passing them the mediator.
The last step insetup, is to inject/set the mediators participants. In our case we just use simple setters.
When its time to communicate, each participant just calls the mediator, passes the message and self as a parameter.
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.
来源:https://stackoverflow.com/questions/6419004/mediator-pattern-and-creation