Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design Pattern: Visitor

Similar presentations


Presentation on theme: "Design Pattern: Visitor"— Presentation transcript:

1 Design Pattern: Visitor
11/24/2018

2 Intent Represent operations to be performed on an object structure
Define new operations without changing the classes of the elements on which they operate 11/24/2018

3 Motivation Compiler represents programs as abstract syntax trees (AST)
Need to perform operations on AST for semantic analysis, code-generation, pretty-printing and so on AST has different types of nodes for variable reference, assignment, operator, etc. Code for an operation is specific to the node type Node TypeCheck() GenerateCode() PrettyPrint() AssignmentNode VariableRefNode 11/24/2018

4 Motivation (contd.) Adding operation code to each node type has the following drawbacks that makes it hard to understand and maintain: Code for each operation is distributed across multiple classes (AssignmentNode, VariableRefNode, etc) Code for different unrelated operations are mixed together as methods on a single Adding a new operation would mean changing and recompiling all the node classes Better Approach: Node classes independent of the operations that apply to them Can be achieved using a visitor design pattern 11/24/2018

5 Visitor Solution using Visitor:
Visitor is an abstract class that has a different method for each type of object on which it operates Each operation is a subclass of Visitor and overloads the type-specific methods Objects that are operated on, accept a Visitor and call back their type-specific method passing themselves as operands Object types are independent of the operations that apply to them New operations can be added without modifying the object types 11/24/2018

6 Visitor Solution Node Accept( NodeVisitor v ) VariableRefNode Accept(NodeVisitor v) {v->VisitVariableRef(this)} AssignmentNode {v->VisitAssignment(this)} Nodes accept visitors and call appropriate method of the visitor Visitors implement the operations and have one method for each type of node they visit NodeVisitor VisitAssignment( AssignmentNode ) VisitVariableRef( VariableRefNode ) TypeCheckingVisitor CodeGeneratingVisitor 11/24/2018

7 Applicability Use the Visitor pattern when:
When an object structure contains different classes of objects with different interfaces and operations on these objects depend on their concrete classes Many unrelated operations need to be performed on the objects and you want to keep related operations together The classes defining the object structure rarely change, but you want to define new operations over the structure If the object structure classes change often, it is better to define the operations in those classes 11/24/2018

8 Consequences of using Visitor
Addition of new operations is easy New operations can be created by simply adding a new visitor Gathers related operations together All operation related code is in the visitor Code for different operations are in different sub-classes of visitor Unrelated operations are not mixed together in the object classes Adding a new concrete type in the object structure is hard Each Visitor has to be recompiled with an appropriate method for the new type 11/24/2018

9 Reference E. Gamma, and others, Design Patterns: Elements of Reusable Object-Oriented Software, Addison-Wesley, 1994. 11/24/2018


Download ppt "Design Pattern: Visitor"

Similar presentations


Ads by Google