SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

En vedette (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
 

Similaire à 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
 

Similaire à 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
 

Plus de 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
 

Dernier

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 

Dernier (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

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