|
||||||
|
The purpose of the Visitor Pattern is to encapsulate an operation that you want to perform on the elements of a data structure. In this way, you can change the operation being performed on a structure without the need of changing the classes of the elements that you are operating on. Using a Visitor pattern allows you to decouple the classes for the data structure and the algorithms used upon them. Each node in the data structure "accepts" a Visitor, which sends a message to the Visitor which includes the node's class. The visitor will then execute its algorithm for that element. This process is known as "Double Dispatching." The node makes a call to the Visitor, passing itself in, and the Visitor executes its algorithm on the node. In Double Dispatching, the call made depends upon the type of the Visitor and of the Host (data structure node), not just of one component.
Let's look more closely at where the Double Dispatching takes place: class ConcreteElementA extends AElement The key is the Accept method in the ConcreteElement classes. The body of this method shows the double dispatching call, where the Visitor is passed in to the accept method, and that visitor is told to execute its visit method, and is handed the node by the node itself. This makes for very robust code, since all of the decision making as to what to execute where and when it taken care of by the dispatching. Nobody ever needs to check anything: they just do what it is that they do, with whatever they're handed. Pretty slick, eh? One very nice thing about this Double Dispatching technique is that it entirely replaces conditional statements, making for much more robust code. Because the Host passes itself in to the visitor, the visitor knows where to execute the algorithm, and because of the type of the Host, it knows which method to run. This eliminates a lot of decision making: normally, you would have to decide (many times) at run-time what to do where, but here, all of the decision making has been completed in the design stage. No object ever has to ask any questions, they just do what it is that they do. The polymorhic dispatching takes care of all of the decision making. One major application of this Design Pattern you are already familiar with: LRStruct . The IAlgo is the Visitor, and each element of a LRStruct is a node. One key advantage of using the Visitor Pattern is that adding new operations to perform upon your data structure is very easy. All you have to do is create a new Visitor and define the operation there. This is the result of the very distinct separation of variant and invariant behavior in the Visitor pattern. The invariant behaviors are represented by the data structure elements and the abstract Visitor. The variant behaviors are encapsulated in the concrete Visitors. Every Visitor has a method for every data structure element type. The data structure elements however, only deal with the abstract Visitor, and hence only have one method (accept()) that deals with it. That method is overriden in each concrete element, which only calls its respective method in the Visitor. this page written by Antonio García |