I am preparing for the OOAD exam and I just can�t see why would one use the visitor pattern instead of the strategy pattern.
As far as I understand, both allows to change the implementation class at runtime. So, what is the subtle difference between them?
Maybe on the visitor pattern, maintainance can be a problem if it is necessary to add new classes that also needed to be visited, since it would mean to add a new method on all the classes that implement the Visitor interface?
The intent of the visitor pattern is to add operations to a class which has rich data structure. If we are not able to identfiy all the operations in for the class but we expect in future we may need to add method for the class, but it is not advisible to alter the class, so to be on safer side we can put a visitor and share the data to the visitor class
The intent of steategy patten is to define a family of algorithm, encapsulate each one, and make them interchangeable. Lets the algorithm vary independently from the clients that use it. this pattern is used to add different algorithm nothing to do with the data in the class
hope this helps
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
Two software engineers solve most of the world's problems in one K&R sized book