SlideShare a Scribd company logo
Visitor PatternVisitor Pattern
Michael Fong
v0.11 . 1
ChangelogChangelog
v 0.1
Basic concept plus examples including
AST DFS traversal, BFS
Message processing
Command processing
Applicability 
PRO vs CON
1 . 2
Object Oriented StructureObject Oriented Structure
Mixed data attribute with operations.
Composite Pattern
Part - Whole Hierarchy
Be able to treat treat all objects in the composite
structure uniformly
2 . 1
How Can We Do DifferentlyHow Can We Do Differently
All operations and data structure are defined in one
place. Hard to maintain and understand when
complexity grows.
Operations to be performed on a data structure
separately - Visitor pattern allow developers to define
a new operation without changing the classes of the
data structure on which it operates.
Operations can be added / removed without changing
the data structure interface.
2 . 2
Conceptual ClassConceptual Class
DiagramDiagram
 
2 . 3
Example: Command ProcessingExample: Command Processing
Data Structure
Commander, Sergeant, Soldier extends Unit
Visitor
SergeantVisitor, CommanderVisitor, SoldierVisitor
implements UnitVisitor
[ ]Ref 3 . 1
Example : Message ProcessingExample : Message Processing
Data Structure
MessageType1, MessageType2 implements
Message
Visitor
MessageVisitorImpl implements MessageVisitor
3 . 2
Example : AST TraversalExample : AST Traversal
Data Structure
ASTNode extends Node
Visitor
DepthFirstVisitor / BreadthFirstVisitor /
PrettyPrintVisitor implements Visitor
[ ]Link
3 . 3
AST - Depth First TraversalAST - Depth First Traversal
+
+ 3
3 . 4
1 2
AST - Depth First TraversalAST - Depth First Traversal
+
+ 3
3 . 4
1 2
AST - Depth First TraversalAST - Depth First Traversal
+
+ 3
3 . 4
1 2
AST - Depth First TraversalAST - Depth First Traversal
+
+ 3
3 . 4
1 2
AST - Depth First TraversalAST - Depth First Traversal
+
+ 3
3 . 4
1 2
AST - Depth First TraversalAST - Depth First Traversal
+
+ 3
3 . 4
1 2
AST - Depth First TraversalAST - Depth First Traversal
+
+ 3
3 . 4
1 2
AST - Depth First TraversalAST - Depth First Traversal
+
+ 3
3 . 4
1 2
AST - Depth First TraversalAST - Depth First Traversal
+
+ 3
3 . 4
1 2
AST - Depth First TraversalAST - Depth First Traversal
+
+ 3
3 . 4
1 2
AST - Depth First TraversalAST - Depth First Traversal
+
+ 3
3 . 4
1 2
Breadth First TraversalBreadth First Traversal
3 . 5
Breadth First TraversalBreadth First Traversal
3 . 5
Breadth First TraversalBreadth First Traversal
3 . 5
Breadth First TraversalBreadth First Traversal
3 . 5
Breadth First TraversalBreadth First Traversal
3 . 5
Breadth First TraversalBreadth First Traversal
3 . 5
Other ExamplesOther Examples
AnnotationValueVisitor
FileVisitor
Java Doc
Java Doc
Tutorial
3 . 6
When to use Visitor PatternWhen to use Visitor Pattern
Many distinct and unrelated operations need to be performed
on objects in an object structure. Visitor lets you keep related
operations together by defining them in one class. 
An data structure contains many classes of objects with differing
interfaces, and you want to perform operations on these objects
that depend on their concrete classes. - ** Message Processing
Example **
The classes defining the object structure rarely change, but you
often want to define new operations over the structure.
Changing the object structure classes requires redefining the
interface to all visitors, which is potentially costly. If the object
structure classes change often, then it's probably better to
define the operations in those classes
4 . 1
PRO CON
Recursion Recursive by nature Hard to debug
Data / Operation
Separation
Adding / Maintaining
new operations easy. 
Costly to update
Visitor
implementation if
data structure
interface is
constantly changing
Flexibility of Data
Object Handling
Allow to add any types
of data object to a
Visitor interface
 
Accumulating State Visitors can accumulate
states as they visit data
class Visitor {
void visitIData1(DataImpl1);
void visitIData2(DataImpl2);
}
4 . 2

More Related Content

Similar to Visitor pattern

What is the difference between struts 1 vs struts 2
What is the difference between struts 1 vs struts 2What is the difference between struts 1 vs struts 2
What is the difference between struts 1 vs struts 2
Santosh Singh Paliwal
 
Software Architecture and Project Management module III : PATTERN OF ENTERPRISE
Software Architecture and Project Management module III : PATTERN OF ENTERPRISESoftware Architecture and Project Management module III : PATTERN OF ENTERPRISE
Software Architecture and Project Management module III : PATTERN OF ENTERPRISE
sreeja_rajesh
 
Declarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi StyleDeclarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi Style
Felix Meschberger
 
Breaking down data silos with the open data protocol
Breaking down data silos with the open data protocolBreaking down data silos with the open data protocol
Breaking down data silos with the open data protocol
Woodruff Solutions LLC
 

Similar to Visitor pattern (20)

Advance oops concepts
Advance oops conceptsAdvance oops concepts
Advance oops concepts
 
Developing Actors in Azure with .net
Developing Actors in Azure with .netDeveloping Actors in Azure with .net
Developing Actors in Azure with .net
 
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScriptLotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
 
What is the difference between struts 1 vs struts 2
What is the difference between struts 1 vs struts 2What is the difference between struts 1 vs struts 2
What is the difference between struts 1 vs struts 2
 
1861 1865
1861 18651861 1865
1861 1865
 
1861 1865
1861 18651861 1865
1861 1865
 
Hibernate
HibernateHibernate
Hibernate
 
1. Mini seminar intro
1. Mini seminar intro1. Mini seminar intro
1. Mini seminar intro
 
Skillwise Struts.x
Skillwise Struts.xSkillwise Struts.x
Skillwise Struts.x
 
Go f designpatterns 130116024923-phpapp02
Go f designpatterns 130116024923-phpapp02Go f designpatterns 130116024923-phpapp02
Go f designpatterns 130116024923-phpapp02
 
A sane approach to microservices
A sane approach to microservicesA sane approach to microservices
A sane approach to microservices
 
Soaosbcourse_content
Soaosbcourse_contentSoaosbcourse_content
Soaosbcourse_content
 
Software Architecture and Project Management module III : PATTERN OF ENTERPRISE
Software Architecture and Project Management module III : PATTERN OF ENTERPRISESoftware Architecture and Project Management module III : PATTERN OF ENTERPRISE
Software Architecture and Project Management module III : PATTERN OF ENTERPRISE
 
Declarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi StyleDeclarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi Style
 
Declarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi StyleDeclarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi Style
 
Breaking down data silos with the open data protocol
Breaking down data silos with the open data protocolBreaking down data silos with the open data protocol
Breaking down data silos with the open data protocol
 
Bp301
Bp301Bp301
Bp301
 
Struts 2 – Interceptors
Struts 2 – InterceptorsStruts 2 – Interceptors
Struts 2 – Interceptors
 
Mca 504 dotnet_unit3
Mca 504 dotnet_unit3Mca 504 dotnet_unit3
Mca 504 dotnet_unit3
 
Hibernate3 q&a
Hibernate3 q&aHibernate3 q&a
Hibernate3 q&a
 

Recently uploaded

AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
Alluxio, Inc.
 

Recently uploaded (20)

KLARNA - Language Models and Knowledge Graphs: A Systems Approach
KLARNA -  Language Models and Knowledge Graphs: A Systems ApproachKLARNA -  Language Models and Knowledge Graphs: A Systems Approach
KLARNA - Language Models and Knowledge Graphs: A Systems Approach
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM Integration
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
A Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data MigrationA Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data Migration
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysis
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
INGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by DesignINGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by Design
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
 
Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdfImplementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
 
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesGraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 

Visitor pattern

  • 2. ChangelogChangelog v 0.1 Basic concept plus examples including AST DFS traversal, BFS Message processing Command processing Applicability  PRO vs CON 1 . 2
  • 3. Object Oriented StructureObject Oriented Structure Mixed data attribute with operations. Composite Pattern Part - Whole Hierarchy Be able to treat treat all objects in the composite structure uniformly 2 . 1
  • 4. How Can We Do DifferentlyHow Can We Do Differently All operations and data structure are defined in one place. Hard to maintain and understand when complexity grows. Operations to be performed on a data structure separately - Visitor pattern allow developers to define a new operation without changing the classes of the data structure on which it operates. Operations can be added / removed without changing the data structure interface. 2 . 2
  • 6. Example: Command ProcessingExample: Command Processing Data Structure Commander, Sergeant, Soldier extends Unit Visitor SergeantVisitor, CommanderVisitor, SoldierVisitor implements UnitVisitor [ ]Ref 3 . 1
  • 7. Example : Message ProcessingExample : Message Processing Data Structure MessageType1, MessageType2 implements Message Visitor MessageVisitorImpl implements MessageVisitor 3 . 2
  • 8. Example : AST TraversalExample : AST Traversal Data Structure ASTNode extends Node Visitor DepthFirstVisitor / BreadthFirstVisitor / PrettyPrintVisitor implements Visitor [ ]Link 3 . 3
  • 9. AST - Depth First TraversalAST - Depth First Traversal + + 3 3 . 4 1 2
  • 10. AST - Depth First TraversalAST - Depth First Traversal + + 3 3 . 4 1 2
  • 11. AST - Depth First TraversalAST - Depth First Traversal + + 3 3 . 4 1 2
  • 12. AST - Depth First TraversalAST - Depth First Traversal + + 3 3 . 4 1 2
  • 13. AST - Depth First TraversalAST - Depth First Traversal + + 3 3 . 4 1 2
  • 14. AST - Depth First TraversalAST - Depth First Traversal + + 3 3 . 4 1 2
  • 15. AST - Depth First TraversalAST - Depth First Traversal + + 3 3 . 4 1 2
  • 16. AST - Depth First TraversalAST - Depth First Traversal + + 3 3 . 4 1 2
  • 17. AST - Depth First TraversalAST - Depth First Traversal + + 3 3 . 4 1 2
  • 18. AST - Depth First TraversalAST - Depth First Traversal + + 3 3 . 4 1 2
  • 19. AST - Depth First TraversalAST - Depth First Traversal + + 3 3 . 4 1 2
  • 20. Breadth First TraversalBreadth First Traversal 3 . 5
  • 21. Breadth First TraversalBreadth First Traversal 3 . 5
  • 22. Breadth First TraversalBreadth First Traversal 3 . 5
  • 23. Breadth First TraversalBreadth First Traversal 3 . 5
  • 24. Breadth First TraversalBreadth First Traversal 3 . 5
  • 25. Breadth First TraversalBreadth First Traversal 3 . 5
  • 27. When to use Visitor PatternWhen to use Visitor Pattern Many distinct and unrelated operations need to be performed on objects in an object structure. Visitor lets you keep related operations together by defining them in one class.  An data structure contains many classes of objects with differing interfaces, and you want to perform operations on these objects that depend on their concrete classes. - ** Message Processing Example ** The classes defining the object structure rarely change, but you often want to define new operations over the structure. Changing the object structure classes requires redefining the interface to all visitors, which is potentially costly. If the object structure classes change often, then it's probably better to define the operations in those classes 4 . 1
  • 28. PRO CON Recursion Recursive by nature Hard to debug Data / Operation Separation Adding / Maintaining new operations easy.  Costly to update Visitor implementation if data structure interface is constantly changing Flexibility of Data Object Handling Allow to add any types of data object to a Visitor interface   Accumulating State Visitors can accumulate states as they visit data class Visitor { void visitIData1(DataImpl1); void visitIData2(DataImpl2); } 4 . 2