1 / 5

Visitor Design Pattern

Visitor Design Pattern. When Need to Add Methods Often. Double Dispatch. static dispatch - compile time selection of which version of the polymorphic function to execute dynamic dispatch – run-time selection

marra
Download Presentation

Visitor Design Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Visitor Design Pattern When Need to Add Methods Often

  2. Double Dispatch • static dispatch - compile time selection of which version of the polymorphic function to execute • dynamic dispatch – run-time selection • single dispatch – selection based on a single object, supported in C++ using virtual functions • double dispatch – selection based on multiple objects, not directly supported by C++ but implemented using Visitor Design Pattern

  3. Visitor • separates data from operations on it by defining visitor class that implements the operations • allows to easily add the operations since they are added to visitor • participants • element– has accept() method that take visitor as argument, calls visit()method of the visitor, element passes itself to the visit() method • visitor – defines visit() with parameter corresponding to the particular concrete element • when accept()is invoked, its implementation is based on type of (concrete) element • when visit() within accept() is invoked, its implementation is based on • (concrete) type of the visitor and • (concrete) type of the element (passed as parameter to visit()) that is, the implementation depends on two objects

  4. Visitor UML Diagram

  5. Visitor Review • what is static/dynamic/single/double dispatch? How is single/double dispatch implemented in C++ • what is the motivation for Visitor Design Pattern? • what is data/operations? • what is element? visitor? • what is easier to add to the code element or visitor? • what is accept()? visit()?

More Related