Visitor Matt G. Ellis. Intent Metsker: Let developers define a new operation for a hierarchy without changing the hierarchy classes. GoF: Represent an.

Slides:



Advertisements
Similar presentations
JDBC Session 4 Tonight: Design Patterns 1.Introduction To Design Patterns 2.The Factory Pattern 3.The Facade Pattern Thursday & Next Tuesday: Data Access.
Advertisements

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Composite Design Pattern (1) –A structural design pattern.
INTERPRETER Main Topics What is an Interpreter. Why should we learn about them.
Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!
Iterator Matt G. Ellis. Intent Metsker: Provide a way to access elements of a collection sequentially. GoF: Provide a way to access the elements of an.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Factory Method Joey Richey Kevin Gorski. Definition Allows a class developer define the interface for creating an object while retaining control of which.
Visitor Pattern Jeff Schott CS590L Spring What is the Purpose of the Visitor Pattern ? n Represent an operation to be performed on the elements.
Patterns – Day 10 Composite Responsibility Reminder: Faculty candidate talk Today 4:20 PM O-267. Salman Azhar: Optimal Prediction via Data Compression.
Automatically Extracting and Verifying Design Patterns in Java Code James Norris Ruchika Agrawal Computer Science Department Stanford University {jcn,
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
CASE Tools And Their Effect On Software Quality Peter Geddis – pxg07u.
The Interpreter Pattern. Defining the Interpreter Intent Given a language, define a representation for its grammar along with an interpreter that uses.
Design Patterns.
Design Patterns in Java Chapter 5 Composite Summary prepared by Kirk Scott 1.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
More Design Patterns In Delphi Jim Cooper Falafel Software Session Code: D3.03 Track: Delphi.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Design Pattern Interpreter By Swathi Polusani. What is an Interpreter? The Interpreter pattern describes how to define a grammar for simple languages,
Unit 25 Composite Summary prepared by Kirk Scott 1.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
Go4 Visitor Pattern Presented By: Matt Wilson. Introduction 2  This presentation originated out of casual talk between former WMS “C++ Book Club” (defunct.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
CPSC 871 John D. McGregor Module 7 Session 1 More UML.
3-1 State Design Pattern C Sc 335 Rick Mercer. State Design Pattern State is one of the Behavioral patterns It is similar to Strategy Allows an object.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Testing OO software. State Based Testing State machine: implementation-independent specification (model) of the dynamic behaviour of the system State:
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Visitor Design Pattern and Java Tree Builder
Advanced Object-oriented Design Patterns Creational Design Patterns.
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Design Patterns: MORE Examples
Sections Inheritance and Abstract Classes
Design Patterns Lecture part 2.
Factory Patterns 1.
Behavioral Design Patterns
Intent (Thanks to Jim Fawcett for the slides)
Presentation by Julie Betlach 7/02/2009
Jim Fawcett CSE776 – Design Patterns Summer 2003
Design Pattern: Visitor
Decorator Pattern Richard Gesick.
Composite Design Pattern By Aravind Reddy Patlola.
Presentation transcript:

Visitor Matt G. Ellis

Intent Metsker: Let developers define a new operation for a hierarchy without changing the hierarchy classes. GoF: Represent an operation to be preformed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates

Visitor by Example - UML

Visitor by Example – Accessibility One aspect of allowing for later extensions is that the hierarchy classes make their attributes accessible. getId() method in MachineComponent and getComponents() in MachineComposite provide access to critical attributes of hierarchy.

Visitor by Example – Supporting Visitor To support Visitor, each class in the hierarchy must have a accept method. This is defined abstractly in the parent class, but the children implement it the same way:

Challenge 29.1 Why can’t we move the definition of the accept method up to the parent class, if it is the same in all the subclasses? Hint: What difference will a Java compiler see between the accept() methods in the Machine and MachineComposite classes?

Visitor by Example - Interface The accept methods in the Class hierarchy along with this interface allow developers to provide new operations on the hierarchy.

Visitor by Example – The Irish You’ve been given the task to bring a new Factory in Dublin online. For some reason you want a representation of the factory in a tree.

Visitor by Example – The Irish Suppose that you need to find a particular machine within the factory model. To add this ability without modifying the MachineComponent hierarchy, you can create a FindVisitor class.

Visitor by Example – FindVisitor The FindVisitor effectively adds a find() method to the MachineComponent hierarchy, without touching the code! The visit() methods do not return an object, so the FindVisitor class records the status of a search in its sought instance variable.

Visitor by Example – FindVisitor

Visitor by Example – Accept/Visit Visitor doesn’t need to know about the type of the underlying classes. Accept asks the class to call the Visit method, which gets the type from the class being visited.

Visitor by Example – RakeVisitor Let’s say we want to find all the leaf nodes – machines – in a machine component, how do we do this? We use a Visitor, of course!

Challenge 29.3 – RakeVisitor

Visitor by Example - Disclaimer The FindVisitor and RakeVisitor classes each add a new behavior to the MachineComponent hierarchy. A danger in writing visitors is that they require an understanding of the hierarchy that you are extending. A change in the hierarchy may break your visitor, and you may misunderstand the mechanics of the hierarchy initially. In particular, you may have to handle cycles if the composite you are visiting does not prevent them.

Writing a Pretty Printer with Visitor Using Iterator we were able to create an ad-hoc pretty printer, but with Visitor we can do a better job.

Pretty Printer – Old vs. New

Pretty Printer - Setup

Pretty Printer – Visit Methods

Pretty Printer – Print Composite

Pretty Printer – Print Tag

The Great Visitor Debate Some argue Visitor is fragile  Adding new subclasses to the hierarchy can break the Visitor  The developer of the Visitor needs to understand the underlying framework (for example, they might need to avoid loops) Other’s argue that it is useful  Developers offer ways to strengthen the pattern, but often with added complexity

A Classical Use Visitors are commonly used when developing compilers to work on Abstract Syntax Trees generated by a parser. However, in this case, the visited class has little or no behavior, all the behavior is handled by the visiting class

Challenge 29.5 (The Last One) Like any pattern, Visitor is never necessary; if it were, it would automatically appear everywhere it was needed. For Visitor, though, alternatives may provide a sturdier design. List two alternatives to building Visitor into the Oozinoz machine and process hierarchies.