The Visitor Pattern – Part 3

Now we can write a visitor that just counts the shapes visited:

Pretty easy and effective, isn’t it?

But there is still some drawback, as the visitor is limited to visit TAbstractShape and its descendants. What if we have other classes to visit that don’t fit in this inheritance scheme? Can’t we find a more general approach? Here it is:

With this TAbstractShape or any other class to be visited has to implement IVisited and we are done.

More in Part 4…

Author: Uwe Raabe

Addicted to Pascal/Delphi since the late 70's