My dilema is in creating one method that follows the builder pattern, which sets a variable and returns itself, but I have an interface Visitor
which is implemented by BasicVisitor
and that is extended by other visitor implementations.
Furthermore, I have a Visitable
interface, which is implemented by SampleVisitable
and that has methods that keep a history of all visitors that have visited.
So the problem is that I want to only keep a full reference of the visitor in the history of SampleVisitable
if that visitor is "important", since the host calls Visitor.clean()
before the visitor visits, which cleans all the data from the last visit, but I want the "important" visitors' data to remain in the history.
This is what I have so far for the implementation:
public interface Visitor {
public void visit( Visitable visitable );
public Visitor clone();
public <V extends Visitor> V important();
public boolean isImportant();
}
This is a snippet from the SampleVisitable
class:
public void accept( Visitor visitor ) {
visitor.clean();
visitor.visit( this );
addToHistory( visitor ); // only adds if visitor.isImportant() == true
}
Then I have BasicVisitor
which implements the important()
method. Now I don't want to have to override the important()
method every time, but with my implementation I would have to, since any other visitors would have an error of the form "cannot convert from BasicVisitor to [OtherVisitor]" if they use the important()
method without overriding it:
OtherVisitor otherVisitor = new OtherVisitor().important(); // cannot convert from BasicVisitor to OtherVisitor error.
What is the best way to do this? I need to have this feature implemented once, and available for use by all other sub-classes, without requiring them to override it. Thank you!
Edit:
Wow, abstract classes are exactly what I was looking for! I did some more research and found this post on differences between interfaces and abstract classes, and I hope it helps others looking for an answer.
After I posted this answer I realized that I didn't have to use the builder pattern in this situation, I could have just created a setImportant( boolean isImportant )
and would have basically what I needed, but I'm glad I asked anyways, because this is what I needed for another design issue.
Edit2:
After trying the abstract class, my main problem is still there, I still have to override the important()
method for each sub-class of Visitor
.
Ilya