I’m struggling with in combining the visitor and the composite patterns in Java.
I have an Element interface for the Composite. It only has the accept method.
I have an abstract class Composite to handle the child management function(add, remove and getChild). I would like to define the acccept method in the compiste class to avoid having to do it in each subclass. Is there a way to do that?
public abstract class Composite implements Element {
protected List<Element> elements;
public Composite() {
elements = new ArrayList<Element>();
}
public void add(Element e) {
elements.add(e);
}
public void remove(Element e) {
elements.remove(e);
}
public Element getChild(int i) {
return elements.get(i);
}
}
Isn’t this what you are looking for?