Some of the benefits and liabilities of the Visitor pattern are as follows:
So the key consideration in applying the Visitor pattern is whether you are mostly likely to change the algorithm applied over an object structure or the classes of objects that make up the structure. The Visitor class hierarchy can be difficult to maintain when new ConcreteElement classes are added frequently. In such cases, it's probably easier just to define operations on the classes that make up the structure. If the Element class hierarchy is stable, but you are continually adding operations or changing algorithms, then the Visitor pattern will help you manage the changes.
This implies that all elements the iterator can visit have a common parent class Item.template class Iterator { // ... Item CurrentItem() const; };
Visitor does not have this restriction. It can visit objects that don't have a common parent class. You can add any type of object to a Visitor interface. For example, in
MyType and YourType do not have to be related through inheritance at all.class Visitor { public: // ... void VisitMyType(MyType*); void VisitYourType(YourType*); };