M
markspace
Stefan said:public void process( final Command command )
{
if( command instanceof clockCommand )
{
This feels like a Observer pattern. At runtime, components get to
decide who they want to talk to via the Observer pattern. To find each
other in the first place, you might need a Mediator pattern. Both
Observer and Observable should register with the Mediator, so that they
have a common place to find each other.
<http://en.wikipedia.org/wiki/Observer_pattern>
<http://en.wikipedia.org/wiki/Mediator_pattern>
I'm not 100% sure those are exactly what you need, but they should at
least get you thinking.
/* This command is intended for me! Let me process it: */
if( command instanceof quitClockCommand )
{ /* Some party wants me to quit */ }
else if( command instanceof adjustClockCommand )
{ /* I should adjust myself now */ }
else { /* There are only these two clock commands,
so this is unexpected. */ }
The part above that I quoted feels like a Visitor pattern.
<http://en.wikipedia.org/wiki/Visitor_pattern>
Each component has one method to call -- "visit()" but could also be
called "command()" or "process()" or "eval()"; whatever makes sense in
your context -- and the Visitor pattern takes care of the dispatch.
else
{ /* This command is just passing-through, I will broadcast
it to my neighbors, maybe it is intended for one of them. */
broadcastToNeighbors( command );
/* Yes: if they do not know the command, and broadcast it,
too, this will give an endless loop of command reposts.
The real code will address this, the code posted here into
Usenet is simplified. */ }}
This bit feels like a Chain Of Responsibility pattern. However, I like
the Mediator pattern better, just because it's more direct.
<http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern>
Just some ideas.