The idea of this mode seems to be very simple. For example, there is a base class, define three methods, and inherited 3 subclasses below. If we must add a function to the base class, this feature is not the basic nature of this object, but to achieve some additional functions, such as counting or configuration, or this feature has different implementations of the three subclasses below. So how do such a function join the class level? ?
If you add a method to the base class, the subclasses achieve this method, then if there is a function of the function, or there is a new subclass, the horse will undoubtedly this implementation will be explosive.
Visitor solves this problem. Just join the Accept method to the base class, allow access to the Visitor class, you can cope with the expansion of the future. It is to do to achieve the Accept method: accept (visitor v) {v.visit (this);}. The specific function is made by the Visitor class. Visitor implements 3 functions: Visit (Derivea), Visit (DeriveB), and Visit (Derivec) for accessing 3 subclasses.
Visitor's methods and subclasses constitute a matrix, one axis is different Visitor, one axis is a subclass. Such a design is not good enough, once you need to add a subclass, all Visitor has to add a VISIT (DeriveX) function, all to change.
The method of solving this problem is called Acyclicvisitor, which implements a sparse matrix, different Visitor does not need to pay attention to all subclasses, and just pay attention to the subclass of interest.
ACYCLICVISITOR is implemented, different VisitorX only Visit (DeriveInterested), the corresponding deriveinterested Accept method has a large change, you need to first put the incoming Visitor, Cast as the specific VisitorX, and then call Visitorx :: Visit (this) Therefore, it affects efficiency.