Question

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

Était-ce utile?

La solution

Please try this. If you use an abstract class, then the method that you implement will be available for all sub-classes.

public abstract class Visitor {
    public abstract void visit( Visitable visitable );

    public abstract Visitor clone();

    public abstract <V extends Visitor> V important();

    public boolean isImportant() {
        //logic to store important visitors
    }
}

Autres conseils

I was able to solve this issue using abstract class, as @sunleo mentioned in his answer, and changing the important() builder function to a setter setImportant( boolean isImportant ) and generics.

So now I have the following implementation:

public abstract class Visitor<T> implements Cloneable  {
    private boolean important = false;
    private Graphable host;

    @Override
    public Visitor<T> clone() {
        try {
            return ( Visitor<T> ) super.clone();
        }
        catch( CloneNotSupportedException exception ) {
            log.info( exception, exception );        
        }
        return null;
    }

    public Graphable getHost() {
        return host;
    }

    public boolean isImportant() {
        return important;
    }

    public void setHost( Graphable host ) {
        this.host = host;
    }

    public void setImportant( boolean isImportant ) {
        important = isImportant;
    }

    public void visit( Graphable graphable ) {
        if( graphable instanceof Node ) {
            visit( ( Node ) graphable );
        }
        else if ( graphable instanceof Edge ) {
            visit( ( Edge ) graphable );
        }
    }

    public abstract void clean();

    public abstract void visit( Edge edge );

    public abstract void visit( Node node );
}

With the Visitor being cloneable, it makes it easy to make a copy for retention in my history.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top