Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Computer Science 340 Software Design & Testing © Ken Rodham 2003 The “Visitor” Design Pattern Source: "Design Patterns: Elements of Reusable Software"

Similar presentations


Presentation on theme: "1 Computer Science 340 Software Design & Testing © Ken Rodham 2003 The “Visitor” Design Pattern Source: "Design Patterns: Elements of Reusable Software""— Presentation transcript:

1 1 Computer Science 340 Software Design & Testing © Ken Rodham 2003 The “Visitor” Design Pattern Source: "Design Patterns: Elements of Reusable Software" by Gamma, Helm, Johnson, and Vlissides, Addison-Wesley, 1995

2 2 Visitor Intent –Implement an operation to be performed on the elements of an object structure without changing the classes of the objects in the structure Motivation –Compilers represent programs internally using abstract syntax trees (parse trees) –The nodes in the tree represent the various constructs used in the program IfNode, ForNode, WhileNode, SwitchNode, ExpressionNode, BlockNode, etc.

3 3 Visitor Motivation (continued) –A node's children represent its sub-parts E.g., WhileNode has children for its termination expression and body –Many operations need to be performed on the tree Type checking, optimization, code generation –How are these operations implemented?

4 4 Visitor Motivation (continued) –Every node class could have a method for each operation typeCheck(), optimize(), generateCode(), etc. –Disadvantages Code for each operation is distributed across many node classes Adding a new operation requires modifying every node class –The visitor pattern overcomes these disadvantages All code for an operation is on one class Adding a new operation does not require changes to the node classes

5 5 Visitor

6 6


Download ppt "1 Computer Science 340 Software Design & Testing © Ken Rodham 2003 The “Visitor” Design Pattern Source: "Design Patterns: Elements of Reusable Software""

Similar presentations


Ads by Google