Visitor Design Pattern


GoF Statement: Represents an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
Category: Behavioral
UML Diagram:  

Description of the Demonstration:

In this demonstration a number of instances of Menu are created, each of which contains any number of MenuItem instances. Each MenuItem can contain any number of instances of an Ingredient. Each Ingredient is designed so that it can provide a number of useful bits of information about itself. When some special information is needed about all items in the Menu collection an instance of Visitor is created and each Menu is told to "accept" the Visitor. The Visitor is then able to navigate through all the elements of the Menu hierarchy structure and gather the needed data then produce the needed information. New visitors can be created to handle other information gathering actions. In this demonstration a HealthRatingVisitor was created after the CalorieCountVisitor and CarbCountVisitor just to illustrate how new information can be generate without having to change the structure of the Menus.


Code:

Click here to download a zip file containing all the source files and an executable of the Demonstration. See the _ReadMe.txt file in the zip.