Stéphane Ducasse 1 Visitor.

Slides:



Advertisements
Similar presentations
Stéphane Ducasse 1 Design Points - Subclassing vs Subtyping Stéphane Ducasse
Advertisements

OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Stéphane Ducasse9.1 Inheritance Semantics and Lookup.
Design Patterns In OPM Presented by: Galia Shlezinger Instructors: Prop. Dov Dori, Dr. Iris Berger.
Stéphane Ducasse6.1 Essential Concepts Why OO? What is OO? What are the benefits? What are the KEY concepts? Basis for all the lectures.
Visitor Pattern Jeff Schott CS590L Spring What is the Purpose of the Visitor Pattern ? n Represent an operation to be performed on the elements.
Visitor Matt G. Ellis. Intent Metsker: Let developers define a new operation for a hierarchy without changing the hierarchy classes. GoF: Represent an.
CS 280 Data Structures Professor John Peterson. Lexer Project Questions? Must be in by Friday – solutions will be posted after class The next project.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
עקרונות תכנות מונחה עצמים תרגול 9 – Design Patterns.
The course builder architecture & project planning presented by: Bahareh Agha Jafari.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
The Interpreter Pattern. Defining the Interpreter Intent Given a language, define a representation for its grammar along with an interpreter that uses.
Design Patterns.
S.Ducasse Stéphane Ducasse 1 Selected Design Patterns.
Programmatic Building of Models Just for Pretty Printing DSM October 2006 Tero Hasu Helsinki Institute for Information Technology.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
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,
S.Ducasse Stéphane Ducasse 1 Design Heuristics.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Centralized vs. Decentralized Interpreter Pattern Visitor Pattern.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
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.
CS453 LectureIntroduction1 CS453 Compiler Construction Instructor:Wim Bohm Computer Science Building 470 TA: tba
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Inheritance Semantics and Method Lookup.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Stéphane Ducasse 1 Singleton.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Design Reuse Earlier we have covered the re-usable Architectural Styles as design patterns for High-Level Design. At mid-level and low-level, design patterns.
S.Ducasse Stéphane Ducasse 1 Essential OO Concepts Stéphane Ducasse.
CONFIDENTIAL1 Good Afternoon! Today we will be learning about Variables & equations Let’s warm up : Write an algebraic expression for the following: 3)
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Stéphane Ducasse 1 Strategy.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
S.Ducasse Stéphane Ducasse 1 Decorator.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable.
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
Stéphane Ducasse 1 Elements of Design - Unit of Reuse.
Stéphane Ducasse 1 Abstract Classes.
S.Ducasse Stéphane Ducasse 1 Adapter.
Syntax-Directed Definitions CS375 Compilers. UT-CS. 1.
Patterns in Sequences. Number patterns Sequences of numbers can have interesting patterns. Here we list the most common patterns and how they are made.
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Introduction to Design Patterns
Behavioral Design Patterns
Presentation by Julie Betlach 7/02/2009
Behavioral and Structural Patterns
“The Trouble with Observer” and “Visitor Revisited”
עקרונות תכנות מונחה עצמים תרגול 9 – Design Patterns
Design Pattern: Visitor
Informatics 122 Software Design II
Object Oriented Design Patterns - Structural Patterns
Let’s Review -- An equation is similar to a scale. Both sides of the scale need to be equal in order for the scale to balance. Properties of equality.
Decorator Pattern Richard Gesick.
Object Oriented Design Patterns - Behavioral Patterns
Course Overview PART I: overview material PART II: inside a compiler
Software Design Lecture : 28.
עקרונות תכנות מונחה עצמים תרגול 7 – Design Patterns
Presentation transcript:

Stéphane Ducasse 1 Visitor

S.Ducasse 2 Intent: Represent an operation to be performed on the elements of an object structure in a class separate from the elements themselves. Visitor lets you define a new operation without changing the classes of the elements on which it operates. Visitor Intent

S.Ducasse 3 Visitor Possible Structure

S.Ducasse 4 Whenever you have a number of items on which you have to perform a number of actions When you ‘decouple’ the actions from the items. Examples: the parse tree (ProgramNode) uses a visitor for the compilation (emitting code on CodeStream) GraphicsContext is a visitor for VisualComponents, Geometrics, and some other ones (CharacterArray,...) Rendering documents When to use a Visitor

S.Ducasse 5 So all our problems are solved, no? Well... how to define it when to use a visitor control over item traversal choosing the granularity of visitor methods implementation tricks Applying the Visitor

S.Ducasse 6 Language to deal with arithmetic expressions. It supports one kind of number, and has +, *, (, ) We want to evaluate expressions, and print them. Visitor Toy Example

S.Ducasse 7 Example Evaluating gives = (3 * 2) gives 7 Printing +1*32

S.Ducasse 8 Visitor Toy Example: ParseTree

S.Ducasse 9 Expressions creation 1 ENumber value: 1 (3 * 2) Times left: (ENumber value: 3) right: (ENumber value: 2) 1 + (3 * 2) Plus left: (ENumber value: 1) right: (Times left: (ENumber value: 3) right: (ENumber value: 2)) Of course in Smalltalk we can just extend Number so no need of ENumber value:.....

S.Ducasse 10 Two solutions: add methods for evaluating, printing,... on Expression and its subclasses create a Visitor, add the visit methods on Expression and its subclasses, and implement visitors for evaluation, printing,... Implementing the Actions

S.Ducasse 11 Visitor Toy Example Solution 1

S.Ducasse 12 Expressions creation (ENumber value: 1) evaluate > 1 (Times left: (ENumber value: 3) right: (ENumber value: 2)) evaluate > 6 (Plus left: (ENumber value: 1) right: (Times left: (ENumber value: 3) right: (ENumber value: 2))) evaluate > 7

S.Ducasse 13 printing is not easy may need specific instance variables adding it directly on Expression clutters Expression may need to add instance variables that are not part of the tree behavior of the printer are mixed with expressions Limits of the approach

S.Ducasse 14 Visitor Toy Example 2

S.Ducasse 15 Expressions creation Evaluator evaluate: (ENumber value: 1) > 1 Evaluator evaluate: (Times left: (ENumber value: 3) right: (ENumber value: 2)) > 6 Evaluator evaluate: (Plus left: (ENumber value: 1) right: (Times left: (ENumber value: 3) right: (ENumber value: 2))) > 7 Evaluator>>evaluate: anExpression ^ anExpression acceptVisitor: self

S.Ducasse 16 Evaluator Evaluator>>visitNumber: aNumber ^ aNumber value Evaluator>>visitPlus: anExpression |l r| l := anExpression left acceptVisitor: self. r := anExpression right acceptVisitor: self. ^ l + r Evaluator>>visitPlus: anExpression |l r| l := anExpression left acceptVisitor: self. r := anExpression right acceptVisitor: self. ^ l * r

S.Ducasse 17 Printer Visitor subclass: #Printer iv: ‘stream level’ Printer>>visitNumber: aNumber stream nextPutAll: aNumber value asString Printer>>visitPlus: anExpression stream nextPutAll: ‘+’. anExpression left acceptVisitor: self. anExpression right acceptVisitor: self. Printer>>visitPlus: anExpression stream nextPutAll: ‘*’. anExpression left acceptVisitor: self. anExpression right acceptVisitor: self.

S.Ducasse 18 Smalltalk has class extensions: method addition method replacement So ‘Decoupling’ actions from items can be done: e.g., put all the printing methods together. take care: works only for methods makes it also really easy to package a visitor! Note: this is a static solution! Smalltalk’s class extensions

S.Ducasse 19 Somewhere in the visitor, items are traversed. Different places where the traversal can be implemented: in the visitor on the items hierarchy Controlling the traversal

S.Ducasse 20 Traversal on the Visitor

S.Ducasse 21 Traversal on the Items

S.Ducasse 22 Sometimes you can represent contextual information by having more specialized visit methods So visitors have more information for implementing their operations Granularity of Visit Methods

S.Ducasse 23 doNode: is invoked from doVariables: and doSequence:temporaries:statements: Granularity of Visit Methods

S.Ducasse 24 Here doTemporaryVariable: provides the context for treating doVariable: Refined Granularity

S.Ducasse 25 You can implement it as we have shown before. But notice the general structure of the methods! This can be taken as advantage: code can be generated for a visitor. the method can be performed/invoked But take care: only works when there is a full correspondence. can make the code hard to understand. Implementation Tricks

S.Ducasse 26 Using #perform:

S.Ducasse 27 Use a Visitor: when the operations on items change a lot. Do not use a visitor: when the items you want to visit change a lot. Question: But how do we know what to choose up- front? When to Use a Visitor