SlideShare a Scribd company logo
1 of 27
Stéphane Ducasse 1
Stéphane Ducasse
stephane.ducasse@inria.fr
http://stephane.ducasse.free.fr/
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
1 + 1
gives = 2
1 + (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
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

More Related Content

Viewers also liked (20)

15 - Streams
15 - Streams15 - Streams
15 - Streams
 
11 bytecode
11 bytecode11 bytecode
11 bytecode
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
5 - OOP - Smalltalk in a Nutshell (c)
5 - OOP - Smalltalk in a Nutshell (c)5 - OOP - Smalltalk in a Nutshell (c)
5 - OOP - Smalltalk in a Nutshell (c)
 
Stoop 434-composite
Stoop 434-compositeStoop 434-composite
Stoop 434-composite
 
Stoop 300-block optimizationinvw
Stoop 300-block optimizationinvwStoop 300-block optimizationinvw
Stoop 300-block optimizationinvw
 
01 intro
01 intro01 intro
01 intro
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Stoop 303-advanced blocks
Stoop 303-advanced blocksStoop 303-advanced blocks
Stoop 303-advanced blocks
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
Stoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvwStoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvw
 
Stoop 415-design points
Stoop 415-design pointsStoop 415-design points
Stoop 415-design points
 
8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model
 
Stoop 437-proxy
Stoop 437-proxyStoop 437-proxy
Stoop 437-proxy
 
Stoop 423-smalltalk idioms
Stoop 423-smalltalk idiomsStoop 423-smalltalk idioms
Stoop 423-smalltalk idioms
 
10 reflection
10 reflection10 reflection
10 reflection
 
Stoop 439-decorator
Stoop 439-decoratorStoop 439-decorator
Stoop 439-decorator
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 
Stoop 421-design heuristics
Stoop 421-design heuristicsStoop 421-design heuristics
Stoop 421-design heuristics
 

Similar to Stoop 431-visitor

Funtional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWTFuntional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWTVasil Remeniuk
 
Tooled Composite Design Pattern presentation
Tooled Composite Design Pattern presentationTooled Composite Design Pattern presentation
Tooled Composite Design Pattern presentationtcab22
 
Tooled Composite Design Pattern
Tooled Composite Design PatternTooled Composite Design Pattern
Tooled Composite Design Patterntcab22
 
Design Patterns By Sisimon Soman
Design Patterns By Sisimon SomanDesign Patterns By Sisimon Soman
Design Patterns By Sisimon SomanSisimon Soman
 
Choose'10: Stephane Ducasse - Powerful DSL engineering in Smalltalk
Choose'10: Stephane Ducasse - Powerful DSL engineering in SmalltalkChoose'10: Stephane Ducasse - Powerful DSL engineering in Smalltalk
Choose'10: Stephane Ducasse - Powerful DSL engineering in SmalltalkCHOOSE
 
LDOW2015 - Uduvudu: a Graph-Aware and Adaptive UI Engine for Linked Data
LDOW2015 - Uduvudu: a Graph-Aware and Adaptive UI Engine for Linked DataLDOW2015 - Uduvudu: a Graph-Aware and Adaptive UI Engine for Linked Data
LDOW2015 - Uduvudu: a Graph-Aware and Adaptive UI Engine for Linked DataeXascale Infolab
 
4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)The World of Smalltalk
 
Tooled Composite Design Pattern Andy
Tooled Composite Design Pattern   AndyTooled Composite Design Pattern   Andy
Tooled Composite Design Pattern Andymelbournepatterns
 
World Usability Day Keyboard Accessibility 12.11.2009
World Usability Day Keyboard Accessibility 12.11.2009World Usability Day Keyboard Accessibility 12.11.2009
World Usability Day Keyboard Accessibility 12.11.2009Patrick Lauke
 
3D Design with OpenSCAD
3D Design with OpenSCAD3D Design with OpenSCAD
3D Design with OpenSCADVickyTGAW
 
CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35Bilal Ahmed
 

Similar to Stoop 431-visitor (20)

Funtional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWTFuntional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWT
 
4 - OOP - Taste of Smalltalk (Squeak)
4 - OOP - Taste of Smalltalk (Squeak)4 - OOP - Taste of Smalltalk (Squeak)
4 - OOP - Taste of Smalltalk (Squeak)
 
Model Based Development For 3 D User Interfaces
Model Based Development For 3 D User InterfacesModel Based Development For 3 D User Interfaces
Model Based Development For 3 D User Interfaces
 
An Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked ExamplesAn Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked Examples
 
Stoop ed-class forreuse
Stoop ed-class forreuseStoop ed-class forreuse
Stoop ed-class forreuse
 
Tooled Composite Design Pattern presentation
Tooled Composite Design Pattern presentationTooled Composite Design Pattern presentation
Tooled Composite Design Pattern presentation
 
Tooled Composite Design Pattern
Tooled Composite Design PatternTooled Composite Design Pattern
Tooled Composite Design Pattern
 
Design Patterns By Sisimon Soman
Design Patterns By Sisimon SomanDesign Patterns By Sisimon Soman
Design Patterns By Sisimon Soman
 
Choose'10: Stephane Ducasse - Powerful DSL engineering in Smalltalk
Choose'10: Stephane Ducasse - Powerful DSL engineering in SmalltalkChoose'10: Stephane Ducasse - Powerful DSL engineering in Smalltalk
Choose'10: Stephane Ducasse - Powerful DSL engineering in Smalltalk
 
LDOW2015 - Uduvudu: a Graph-Aware and Adaptive UI Engine for Linked Data
LDOW2015 - Uduvudu: a Graph-Aware and Adaptive UI Engine for Linked DataLDOW2015 - Uduvudu: a Graph-Aware and Adaptive UI Engine for Linked Data
LDOW2015 - Uduvudu: a Graph-Aware and Adaptive UI Engine for Linked Data
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 
Stoop 305-reflective programming5
Stoop 305-reflective programming5Stoop 305-reflective programming5
Stoop 305-reflective programming5
 
DSL in scala
DSL in scalaDSL in scala
DSL in scala
 
4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)
 
Tooled Composite Design Pattern Andy
Tooled Composite Design Pattern   AndyTooled Composite Design Pattern   Andy
Tooled Composite Design Pattern Andy
 
World Usability Day Keyboard Accessibility 12.11.2009
World Usability Day Keyboard Accessibility 12.11.2009World Usability Day Keyboard Accessibility 12.11.2009
World Usability Day Keyboard Accessibility 12.11.2009
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
3D Design with OpenSCAD
3D Design with OpenSCAD3D Design with OpenSCAD
3D Design with OpenSCAD
 
CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35
 
Howto curses
Howto cursesHowto curses
Howto curses
 

More from The World of Smalltalk (15)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 
05 seaside
05 seaside05 seaside
05 seaside
 
04 idioms
04 idioms04 idioms
04 idioms
 
02 basics
02 basics02 basics
02 basics
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Stoop 450-s unit
Stoop 450-s unitStoop 450-s unit
Stoop 450-s unit
 
Stoop 440-adaptor
Stoop 440-adaptorStoop 440-adaptor
Stoop 440-adaptor
 

Recently uploaded

[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 

Recently uploaded (20)

[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 

Stoop 431-visitor

  • 1. Stéphane Ducasse 1 Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ Visitor
  • 2. 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
  • 4. 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
  • 5. 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
  • 6. 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
  • 7. S.Ducasse 7 Example Evaluating 1 + 1 gives = 2 1 + (3 * 2) gives 7 Printing +1*32
  • 8. S.Ducasse 8 Visitor Toy Example: ParseTree
  • 9. 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:.....
  • 10. 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
  • 11. S.Ducasse 11 Visitor Toy Example Solution 1
  • 12. 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
  • 13. 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
  • 15. 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
  • 16. 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
  • 17. 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.
  • 18. 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
  • 19. 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
  • 22. 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
  • 23. S.Ducasse 23 doNode: is invoked from doVariables: and doSequence:temporaries:statements: Granularity of Visit Methods
  • 24. S.Ducasse 24 Here doTemporaryVariable: provides the context for treating doVariable: Refined Granularity
  • 25. 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
  • 27. 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