What is the correct term for an implementation of an interface that delegates method calls to a collection of the same interface?
I have an interface thus:
public interface Doer
{
public void do(Object arg);
}
And I have an implementation that keeps a list of Doers
, and does whatever on each:
public class DoerCollectionThing
implements Doer
{
private List<Doer> doers....
public void addDoer(Doer d)
{
doers.add(d);
}
public void do(Object arg)
{
for (Doer d : doers)开发者_运维技巧{
d.do(arg);
}
}
}
So, what do I call DoerCollectionThing
? Is it a DoerAggregator
? Or maybe DoerCollectionDoer
? What do you all use for this type of thing?
The correct name for this is a Composite
, because you're composing many implementations of an interface together into a single object.
I would call it a MulticastDoer
or BroadcastDoer
. Possibly AggregatingDoer
although that may give the impression that it's performing aggregation of the results, which it doesn't in your example.
In Effective Java 2nd Edition Item 16 this is called Forwarding class.
Well, the correct answer to you question (as asked in the title) was given by Gary.
Concerning your other question, I'd have to say: pick a meaningfull name. If the "CollectionThing" only servers as a collection, that implements some interface Foo
, then I suppose calling it FooGroup
makes the most sense. It's neutral and self-explanatory.
But for example if you assume the following:
interface Drawable {
void draw();
}
class Line implements Drawable {
//methods to set start and end point as well as stroke thickness, color, etc.
void draw() {
//implementation here
}
}
class LineGroup implements Drawable {
//methods to add/remove lines
void draw() {
//call draw-method of all children
}
}
This is also OK. But really Path
would maybe make a better name than LineGroup
. When I read a class name, I don't want to know, how it is implemented, i.e. delegation, composition, composite pattern or whatever. I want to know its purpose. Always use identifiers to convey purpose.
greetz
back2dos
精彩评论