I'd like to use the Composite pattern for forwarding calls received by one object to others.
At present, objects on the receiving end are all of the same Abstract
type but the snag is that they selectively accept different types of objet as parameters according to their concrete type (think different models).
As far as I can see, there are two solutions but neither is satisfactory:
Lists
as there are input types. This brings the problem that a List
has to be added to accomodate a new input type and each List
has to be explicitely processed in turn.I've been thinking in terms of interfaces but have not come up with a feasible idea as of yet. What would be a solution to this design issue? Is a composite appropriate at all?
P.S: This is in the context of an mvc.
Composite pattern allows you to treat a collection of objects the same as a leaf object.
I'd say that you could do something like this:
public interface Command
{
void execute(Object parameter);
}
public class LeafCommand implements Command
{
public void execute(Object parameter)
{
// do something for a leaf
}
}
public class CompositeCommand implements Command
{
private List<Command> commands;
void execute(Object parameter)
{
for (Command child : commands)
{
child.execute(parameter);
}
}
}
That's what Composite means to me. You're right - if you have to use instanceof
you've done it wrong.