mercredi 6 décembre 2017

How can I avoid empty (noop) method of a visitor pattern implementation?

I have a large collection of different objects that extend the same base class A. To apply different algorithms to the collection, I created an IVisitor interface that has an Accept method for each child of A.

Some of the visitors that implements IVisitor actually use all childs of A but many of them don't, so I end up having visitors with a bunch of empty (noop) methods that I would like to avoid. Moreover, everytime I add a new child class of A I need to add a new Accept method to IVisitor and all the classes that implements it, which is a common problem of the visitor pattern.

I've tried to implement the Selective Visitor pattern but I don't like to cast things around as in my case it could be a problem.

I've also evaluated this question but I the answers are not really answering the problem. I would lilke to use an interface and not an abstract class, because in my case multiple inheritance is not allowed.

Is there a way to avoid this kind of hassle and get a cleaner code?

Aucun commentaire:

Enregistrer un commentaire