The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor...
Transcript of The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor...
![Page 1: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/1.jpg)
The Visitor Design Pattern
• the visitor design pattern is a way of separating an algorithm from an object structure upon which it operates.
• A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures.
• One creates a visitor class that implements all of the appropriate specializations of the virtual function.
• The visitor takes the instance reference as input, and implements the goal through double dispatch.
• In the GoF classification this is a behavioral design pattern
![Page 2: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/2.jpg)
Acknowledgements
• Materials were borrowed from
– Design Patterns in Java by Steven Metsker and William Wake (textbook for course)
– Head First Design Patterns by Elisabeth Freeman, Eric Freeman, Bert Bates, and Kathy Sierra
– The compiler example was borrowed from lecture notes on Design Patterns by Brian Malloy at Clemson University
– The main bullets on the previous slide came from the wikipedia article on the Interpreter Pattern
![Page 3: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/3.jpg)
Motivation• Consider a compiler that represents programs as abstract
syntax trees (ASTs)
• need to perform operations on ASTs
– type checking
– code generation
– printing
• One option: place all of these operations in the nodes of the AST
![Page 4: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/4.jpg)
Node class hierarchy
Node
typeCheck()generateCode();prettyPrint();
VariableRefNode
typeCheck()generateCode();prettyPrint();
AssignmentNode
typeCheck()generateCode();prettyPrint();
![Page 5: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/5.jpg)
Problem with Node class hierarchy
• Distributing operations across Node classes leads to a system that’s hard to understand, maintain and change.
• it’s confusing to have type-checking code in the same class with code to perform pretty printing.
• also, adding a new operation, e.g. data flow analysis, requires recompiling all of the classes!
![Page 6: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/6.jpg)
Visitor• allows us to separate Node classes from operations “on”
Node classes
• allows each new operation to be added separately, withoutchanging Node class!
![Page 7: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/7.jpg)
Nodes “accept” visitors
Node
accept(NodeVisitor)
VariableRefNode
accept(NodeVisitor)
AssignmentNode
accept(NodeVisitor v)
program
v.visitAssignment(this)
![Page 8: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/8.jpg)
Visitor for Nodes
NodeVisitor
visitAssignment(node);visitVariableRef(node);
VariableRefNode
visitAssignment(node);visitVariableRef(node);
AssignmentNode
visitAssignment(node)visitVariableRef(node)
![Page 9: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/9.jpg)
Visitor Pattern
• Define two class hierarchies
– one for elements being operated on (nodes)
– one for visitors that define operations on the elements
• create new operations by adding a new subclass to Visitor class hierarchy
![Page 10: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/10.jpg)
Structure of Visitor Pattern
![Page 11: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/11.jpg)
Collaborations
![Page 12: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/12.jpg)
Double Dispatch• operation that gets executed depends on
– kind of request
– types of two receivers
– example: accept() is double dispatched operation. Its meaning depends on two types: Visitor’s and Element’s
![Page 13: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/13.jpg)
Benefits and Drawbacks
![Page 14: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/14.jpg)
Using Visitor at Oozinoz
• This initial work has to be done at design time
• The value of this approach is to allow new operations on machines without changing the machine hierarchy
![Page 15: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/15.jpg)
The accept methods• The accept method in both the Machine class and the
MachineComponent class has identical code
• Even though the code looks identical, it cannot be pulled up to the parent class because of the “this”
![Page 16: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/16.jpg)
Solution 29.1
• Here is the implementation of the MachineVisitor interface
![Page 17: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/17.jpg)
The Oozinoz Factory in Dublin• The developers in Dublin, Ireland
have created an object model accessible through the static method dublin
• They display the components in a JTree
![Page 18: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/18.jpg)
Finding a Machine within a Factory• We want to write a find method
to search for a particular machine in a factory yet not have to modify or even recompile the code for the factory composite
• Since the factory composite has now been set up to allow visitors this is possible
• We implement the MachineVisitor interface in a FindVisitor class
![Page 19: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/19.jpg)
FindVisitor• The visit method does not
return a value
• So the FindVisitor records the status of the search in the found variable
• The tree traversal ends once the desired component is found
![Page 20: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/20.jpg)
Solution 29.2
![Page 21: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/21.jpg)
Double Dispatching
• Here we see the double dispatch typical of the visitor design pattern
• Accept passes in a handle to the caller
• The node in the hierarchy, where it is a MachineComposite or a Machine, calls visit to return a handle to itself
• This allows the visitor to access the node and perform whatever operation is needed
![Page 22: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/22.jpg)
• We want to add a visitor that finds all leaf nodes in the tree
• We will collect the machines at the leaves in a set
![Page 23: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/23.jpg)
Solution 29.3
![Page 24: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/24.jpg)
Showing Our New Visitor at Work
![Page 25: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/25.jpg)
Composites with Cycles• Our ProcessComponent
may have cycles
• We need to avoid infinite loops
• After disassembling an aeriel shell it is remade again
• This isn’t shown since the iterator doesn’t visit the same node twice
• We want some want to indicate a repeated step
![Page 26: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/26.jpg)
Reworking the Code • Our desired printout is shown below
• We indicate the repeated step by printing … after the step is repeated
![Page 27: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/27.jpg)
• We use a Hashset to store visited nodes
• If visited we print …
Visited Nodes
• We will indicate visiting a ProcessAlternation by printing a ? as a prefix
![Page 28: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/28.jpg)
Demonstrating our New Output
![Page 29: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/29.jpg)
• How can developers of the hierarchy, the ProcessComponent hierarchy in this case, help use detect cycles?
![Page 30: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/30.jpg)
Solution 29.4
![Page 31: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/31.jpg)
Use Visitor with Care
• Visitor is a “fragile” design pattern and must be used with care; if the base hierarchy changes frequently, this may not be a good choice
• Use Visitor if
![Page 32: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/32.jpg)
Solution 29.5
![Page 33: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/33.jpg)
![Page 34: The Observer Design Patternblk/cs3667/Extension... · The Visitor Design Pattern •the visitor design pattern is a way of separating an algorithm from an object structure upon which](https://reader035.fdocuments.us/reader035/viewer/2022071212/60259baa839c340e4c177db4/html5/thumbnails/34.jpg)