....and other creepy things from John Vlissides The Visitor Pattern EXISTS! And its INTENT is to represent an operation to be performed on the elements.

Slides:



Advertisements
Similar presentations
Behavioral Pattern: Visitor C h a p t e r 5 – P a g e 224 When an algorithm is separated from an object structure upon which it operates, new operations.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
GoF State Pattern Aaron Jacobs State(305) Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
INTERPRETER Main Topics What is an Interpreter. Why should we learn about them.
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
Design Patterns In OPM Presented by: Galia Shlezinger Instructors: Prop. Dov Dori, Dr. Iris Berger.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Design Patterns CS is not simply about programming
Visitor Pattern Jeff Schott CS590L Spring What is the Purpose of the Visitor Pattern ? n Represent an operation to be performed on the elements.
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
1 Computer Science 340 Software Design & Testing © Ken Rodham 2003 The “Visitor” Design Pattern Source: "Design Patterns: Elements of Reusable Software"
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
CSE 331 Software Design & Implementation Hal Perkins Winter 2013 Design Patterns Part 3 (Slides by Mike Ernst and David Notkin) 1.
Composite Design Pattern. Motivation – Dynamic Structure.
Design Patterns Trends and Case Study John Hurst June 2005.
CSE 332: Design Patterns (Part I) Introduction to Design Patterns Design patterns were mentioned several times so far –And the Singleton Pattern was discussed.
ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Design Pattern Interpreter By Swathi Polusani. What is an Interpreter? The Interpreter pattern describes how to define a grammar for simple languages,
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Design Patterns – II Lecture IV. Singleton Pattern Intent – Ensure a class only has one instance, and provide a global point of access to it Motivation.
CSE 403 Lecture 14 Design Patterns. Today’s educational objective Understand the basics of design patterns Be able to distinguish them from design approaches.
Structural Design Patterns
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Where Do Surrogates Fit into This Proxy Pattern Observer Pattern Visitor Pattern By Kurt Rehwinkel.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Wei-Tek Tsai who’s.
Design Patterns Introduction
CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
CSE 332: Design Patterns (Part II) Last Time: Part I, Familiar Design Patterns We’ve looked at patterns related to course material –Singleton: share a.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
Design Patterns (II) Lecture Three. Solution to Homework Two Used framework Used design patterns: composite and state Question: what are the differences.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Design Patterns: MORE Examples
Mediator Design Pattern
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Behavioral Design Patterns
CS 4240 Principles of SW Design
Jim Fawcett CSE776 – Design Patterns Summer 2003
Design Pattern: Visitor
Informatics 122 Software Design II
Third lecture Review Visitor pattern DemeterJ 12/25/2018 SD.
Overriding Methods & Class Hierarchies
Third lecture Review Visitor pattern DemeterJ 2/17/2019 SD.
Strategy Design Pattern
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Informatics 122 Software Design II
Composite Design Pattern By Aravind Reddy Patlola.
16. Visitors SE2811 Software Component Design
16. Visitors SE2811 Software Component Design
Visitor Pattern Intent
Presentation transcript:

....and other creepy things from John Vlissides

The Visitor Pattern EXISTS! And its INTENT is to represent an operation to be performed on the elements of an object structure. And its INTENT is to represent an operation to be performed on the elements of an object structure.

How? By letting the user define a new operation without changing the classes of the elements on which it operates! By letting the user define a new operation without changing the classes of the elements on which it operates!

Motivation Consider a compiler with abstract syntax trees... Consider a compiler with abstract syntax trees...

Abstract Syntax Trees Compiler may need to define operations for: Type-Checking Code Optimization Flow Analysis Variable Checking...and more!

Meaning.... There will be one class for every single thing that needs to be performed... There will be one class for every single thing that needs to be performed... Distributing operations amongst various node classes is hard to understand, maintain and change! Distributing operations amongst various node classes is hard to understand, maintain and change! Adding a new operation would be a pain in the butt Adding a new operation would be a pain in the butt

What we want... Each new operation should be added separately Each new operation should be added separately All of the node classes to be independent of their operations All of the node classes to be independent of their operations

The Solution Package related operations from each class in a separate object, and pass it to elements of the abstract syntax tree as it is traversed. Package related operations from each class in a separate object, and pass it to elements of the abstract syntax tree as it is traversed. We call this object...

On a serious note When an element accepts a visitor, it sends a request to it that encodes the element’s class. When an element accepts a visitor, it sends a request to it that encodes the element’s class. It also includes the element as an argument. It also includes the element as an argument. The visitor is left to execute the operation for that element, which used to be in the class of the element. The visitor is left to execute the operation for that element, which used to be in the class of the element.

This is important too To make visitors work for more than just type-checking, we need an abstract parent NodeVisitor class for all visitors of an abstract syntax tree. To make visitors work for more than just type-checking, we need an abstract parent NodeVisitor class for all visitors of an abstract syntax tree.

NodeVisitor NodeVisitor must declare an operation for each node class. NodeVisitor must declare an operation for each node class.

Visitor – Up Close and Personal There are two class hierarchies with this pattern (page 332 – 333): There are two class hierarchies with this pattern (page 332 – 333): Node - for elements being operated on NodeVisitor - for the visitors that define operations on the elements

Visitor, continued A new operation is created by adding a new subclass in the visitors class hierarchy. A new operation is created by adding a new subclass in the visitors class hierarchy. New functionality can simply be added by defining new NodeVisitor subclasses. New functionality can simply be added by defining new NodeVisitor subclasses.

You know you wanna use this when... 1) An object structure contains many classes of objects with differing interfaces, and you want to perform operations on these objects that depend on their concrete classes. 2) Many distinct and unrelated operations need to be performed on objects in an object structure and you don’t want to “pollute” their classes with these operations. 3) The classes defining the object structure rarely change, but you often want to define new operations over the structure.

Participants Visitor – declares a visit operation for each class of ConcreteElement in the object structure Visitor – declares a visit operation for each class of ConcreteElement in the object structure ConcreteVisitor (PriceVisitor) – implements each operation declared by Visitor ConcreteVisitor (PriceVisitor) – implements each operation declared by Visitor Element (Trash) – defines an Accept operation that takes a visitor as an argument Element (Trash) – defines an Accept operation that takes a visitor as an argument

More Participants! ConcreteElement (Aluminum, Paper, Glass) – implements an Accept operation that takes a visitor as an argument ConcreteElement (Aluminum, Paper, Glass) – implements an Accept operation that takes a visitor as an argument ObjectStructure (next to Trash) ObjectStructure (next to Trash) - can enumerate its elements - can enumerate its elements - may provide a high-level interface to allow the visitor to visit its elements - may provide a high-level interface to allow the visitor to visit its elements - may be a composite or a collection like a set or list. - may be a composite or a collection like a set or list.

Collaborations A client that uses Visitor must create a ConcreteVisitor object and then traverse the object structure, visiting each element with the visitor (page 335) A client that uses Visitor must create a ConcreteVisitor object and then traverse the object structure, visiting each element with the visitor (page 335) When an element is visited, it calls the visitor operation that corresponds to its class. The element supplies itself as an argument to this operation to let the visitor access its state, if necessary. When an element is visited, it calls the visitor operation that corresponds to its class. The element supplies itself as an argument to this operation to let the visitor access its state, if necessary.

Consequences 1. Visitor makes adding new operations easy! 2. A Visitor gathers related operations and separates unrelated ones, simplifying classes defining elements and algorithms defined by visitors. 3. Adding new ConcreteElement classes is hard. 4. Visitor does not have access restrictions like an iterator does. It can visit objects that don’t have a common parent class, and you can add any type to a visitor interface. 5. Visitors can accumulate state as they visit each element in the object structure. 6. The Visitor pattern often forces you to provide public operations that access an element’s internal state, which may compromise encapsulation.

The Implementation I s on page 337.

Technical Details Double Dispatch – Visitor uses this to let us add operations to classes without changing them. Not all programming languages support it directly (like C++). Double Dispatch – Visitor uses this to let us add operations to classes without changing them. Not all programming languages support it directly (like C++). It means that operations get executed depending on the kind of request and types of two receivers, NOT one. It means that operations get executed depending on the kind of request and types of two receivers, NOT one.

This is Crucial! Because of double dispatch, an executed operation depends on the type of Visitor and the Element it visits. Because of double dispatch, an executed operation depends on the type of Visitor and the Element it visits. Binding operations can be done at run- time with “Accept” in lieu of statically with the element interface. Binding operations can be done at run- time with “Accept” in lieu of statically with the element interface.

And who is responsible...for object structure traversal?...for object structure traversal? After all, we know that a visitor must visit each element of the object structure.

Hmmm Responsibility can fall on: Responsibility can fall on: 1) the structure, 1) the structure, 2) the visitor, or 2) the visitor, or 3) a separate iterator 3) a separate iterator Most common is to use the structure itself, but an iterator is used just as effectively. Most common is to use the structure itself, but an iterator is used just as effectively. The visitor is used least often to do it, because traversal code often gets duplicated. The visitor is used least often to do it, because traversal code often gets duplicated.

Usage Visitor is not used often, at least according to and Visitor is not used often, at least according to and The Second site considers it totally useless. The Second site considers it totally useless. But don’t let that discourage you, because it’s a cool pattern. But don’t let that discourage you, because it’s a cool pattern.

And lastly... Visitor can be used to apply an operation over an object structure defined by the composite pattern. Visitor can be used to apply an operation over an object structure defined by the composite pattern. Visitor can also do the interpretation for the Interpreter pattern. Visitor can also do the interpretation for the Interpreter pattern.

Onto Vlissides!

Visiting Rights So far we’ve used Composite and Proxy for supporting symbolic links. So far we’ve used Composite and Proxy for supporting symbolic links. See Figure 2.3, page 29 See Figure 2.3, page 29 Composite contributed Node, File and Directory classes Composite contributed Node, File and Directory classes Proxy contributed the Link class. Proxy contributed the Link class.

Alexandrian Density The Node class is the Component in The Node class is the Component in the COMPOSITE pattern and the Subject in the PROXY pattern. Christopher Alexander (above) called Christopher Alexander (above) called this “density”. So he got the term named after him. So he got the term named after him. Lucky guy. Lucky guy.

Our File System The Node Interface is up for The Node Interface is up fordiscussion We want a minimal set of operations that lets clients build open-ended functionality. We want a minimal set of operations that lets clients build open-ended functionality. “Performing surgery” on the interface would be a hassle every time we wanted to add something new to it. “Performing surgery” on the interface would be a hassle every time we wanted to add something new to it.

Is there an alternative? What if we take functionality out of the node classes and put it into a client? What if we take functionality out of the node classes and put it into a client? We need Downcasts to do this, making the client complicated. We need Downcasts to do this, making the client complicated.

But Downcasts are yucky! Dynamic_cast (node) Dynamic_cast (node)

Visitor...TO THE RESCUE! Our file system problem is similar to the abstract syntax tree example Our file system problem is similar to the abstract syntax tree example By using the accept method like we did before, we let the visitor pattern do the work for us. By using the accept method like we did before, we let the visitor pattern do the work for us. Bottom line: No type tests or downcasting Bottom line: No type tests or downcasting

More on this Once we establish visiting rights by adding accept(Visitor &) to Node classes, those classes NEVER need to be modified again. Once we establish visiting rights by adding accept(Visitor &) to Node classes, those classes NEVER need to be modified again. Vlissides uses function overloading, but encoding the type of Node in the visit operation’s name works just as well (page 35-36) Vlissides uses function overloading, but encoding the type of Node in the visit operation’s name works just as well (page 35-36)

Is the Visitor right for you? The Visitor pattern isn’t The Visitor pattern isn’t for everyone. Consult for everyone. Consult your textbook to find out your textbook to find out if it’s the pattern that if it’s the pattern that you’re looking for. you’re looking for.

Things to Ask Yourself Is the class hierarchy I’m visiting stable? Or will it be changing like crazy? Is the class hierarchy I’m visiting stable? Or will it be changing like crazy? “Adding a new kind of Node may force us to change all the classes in the Visitor hierarchy just to add a corresponding visit operation.” Will the circular dependency between Visitor and Node class hierarchies bother you? Will the circular dependency between Visitor and Node class hierarchies bother you? A change to either base class interface is likely to prompt a recompile of both hierarchies.

Kevlin Henney: The guy who was referenced on Page 37 “C++ Overloading does not “C++ Overloading does not require having to overload all versions of visit or that you abandon overloading the visit member.”

Valuable Insight The “using” declaration allows us to inject names from a base class into a class for overloading. The “using” declaration allows us to inject names from a base class into a class for overloading. This maintains overloading regularity. This maintains overloading regularity. And it’s noninvasive because users are not forced to remember names or conventions from the visit function. And it’s noninvasive because users are not forced to remember names or conventions from the visit function.

Up Next: Security 1) Protecting the system from inadvertent and malicious corruption 1) Protecting the system from inadvertent and malicious corruption 2) Maintaining system integrity in the face of hardware and software errors. 2) Maintaining system integrity in the face of hardware and software errors.

Any Questions? Manne or Nick would love to answer them.

Credits The “Gang”: Erich Gamma Richard Helm Ralph Johnson John Vlissides References: Christopher Alexander Kevlin Henney Patterns Mentioned: VisitorCompositeProxyInterpreter Thanks For Watching: CSE 432 Inspiration Provided By: The Nick Yianilos Design Pattern Experience Good Teacher: Chris Gill Good TA’s: Chris Swope Yuling Liang