SlideShare uma empresa Scribd logo
1 de 17
Baixar para ler offline
Visitor Pattern
Prepared By:
Nikunj Dhameliya(115375)
Student @ gvp
Overview Of Pattern:-
● Problem
● Intent
● Motivation
● Applicability
● Structure
● Participants
● Collaboration
● Consequences
● Related Pattern
Real Life Problem
Problem
● A Person Want to go at Office From Home
Constraint
● Office Is So Far From His Home.
● And He Don't Have Any Personal Vehicle.
Possible Solutions
● Person May Go to Office...
– by Taxi
– by Auto
– by Bus
– by Train
Solution
● The Visitor pattern represents an operation to be performed on the
elements of an object structure without changing the classes on which
it operates. This pattern can be observed in the operation of a taxi
company. When a person calls a taxi company (accepting a visitor), the
company dispatches a cab to the customer. Upon entering the taxi the
customer, or Visitor, is no longer in control of his or her own
transportation.
Structure:-
Class Diagram:-
Intent:-
● Represent an operation to be performed on the
elements of an object structure. Visitor lets you
define a new operation without changing the classes
of the element on which it operates.
Motivation:-
● Problems with this approach:
➢ Adding new operations requires changes to all of
the node classes
➢ It can be confusing to have such a diverse set of
operations in each node class. For example,
mixing type-checking code with pretty-printing
code can be hard to understand and maintain.
● Another solution is to encapsulate a desired operation in a
separate object, called a visitor. The visitor object then
traverses the elements of the tree. When an tree node
"accepts" the visitor, it invokes a method on the visitor that
includes the node type as an argument. The visitor will
then execute the operation for that node - the operation
that used to be in the node class.
Applicability:-
Use the Visitor Pattern When
➢ There are many distinct and unrelated operations needed to
be performed. Visitor pattern allows us to create a separate
visitor concrete class for each type of operation and to
separate this operation implementation from the objects
structure.
➢ The object structure is not likely to be changed but is very
probable to have new operations which have to be added.
Since the pattern separates the visitor (representing
operations, algorithms, behaviors) from the object structure
it's very easy to add new visitors as long as the structure
remains unchanged.
➢ Similar operations have to be performed on objects of
different types grouped in a structure (a collection or a more
complex structure)
Participants:-
● Visitor
➢ Declares a Visit operation for each class of ConcreteElement
in the object structure.The operation's name and signature
identifies the class that sends the Visit request to the
visitor.That lets the visitor determine the concrete class of
the element being visited.Then the visitor can access the
element directly through its particular interface.
● ConcreteVisitor
➢ Implements each operation declared by Visitor.Each
operation implements a fragment of the algorithm defined
for the corresponding class of object in the
structure.ConcretVisitor provides the context for the
algorithm and stores its local state.This state often
accumulates results during the traversal of the structure.
➢ Element
➢ Defines an Accept operation that takes a visitor as an
argument.
● ConcreteElement
➢ Implements an Accept operation that takes a visitor as
an argument
● ObjectStructure
➢ Can enumerate its elements.
➢ May provide a high-level interface to allow the visitor to
visit its elements.
Continue...
Collaboration:-
➢ A client that uses the Visitor pattern must create a
ConcreteVisitor object and then traverse the object
structure,visiting each element with the visitor.
➢ When an element is visited, it calls the Visitor operation
that corresponds to its class. The element supplies itselef as
an argument to this operation to let the visitor access its
state, if necessary.
Consequences:-
● Benefits
➢ Adding new operations is easy.
➢ Related behavior isn't spread over the
classes defining the object structure; it's
localized in a visitor. Unrelated sets of
behavior are partitioned in their own
visitor subclasses.
● Liabilities
● Adding new ConcreteElement classes is
hard. Each new ConcreteElement gives
rise to a new abstract operation on Visitor
and a corresponding implementation in
every ConcreteVisitor class.
● Single-Dispatch
➢ The actual method invoked depends on the
name of the request (method signature) and the
type of the receiver object.
● Double-Dispatch
➢ The actual method invoked depends on the
name of the request and the types of two
receivers.
Continue...
Related Pattern:-
● Composite
➢ Visitors can be used to apply an operation over an
object structure defined by the Composite pattern.
● Interpreter
➢ Visitor may be applied to do the interpretation.
Thank You....

Mais conteúdo relacionado

Mais procurados

Design Pattern - Observer Pattern
Design Pattern - Observer PatternDesign Pattern - Observer Pattern
Design Pattern - Observer PatternMudasir Qazi
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooksSamundra khatri
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooksMaulik Shah
 
React-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsReact-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsANKUSH CHAVAN
 
Decorator Design Pattern
Decorator Design PatternDecorator Design Pattern
Decorator Design PatternAdeel Riaz
 
Design pattern - Facade Pattern
Design pattern - Facade PatternDesign pattern - Facade Pattern
Design pattern - Facade PatternMudasir Qazi
 
Important React Hooks
Important React HooksImportant React Hooks
Important React HooksKnoldus Inc.
 
Observer design pattern
Observer design patternObserver design pattern
Observer design patternSara Torkey
 
Basics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdfBasics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdfKnoldus Inc.
 
Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternMudasir Qazi
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1Shahzad
 
Observer Software Design Pattern
Observer Software Design Pattern Observer Software Design Pattern
Observer Software Design Pattern Nirthika Rajendran
 

Mais procurados (20)

Design Pattern - Observer Pattern
Design Pattern - Observer PatternDesign Pattern - Observer Pattern
Design Pattern - Observer Pattern
 
Facade Pattern
Facade PatternFacade Pattern
Facade Pattern
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
React Hooks
React HooksReact Hooks
React Hooks
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
React-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsReact-JS Component Life-cycle Methods
React-JS Component Life-cycle Methods
 
Decorator Design Pattern
Decorator Design PatternDecorator Design Pattern
Decorator Design Pattern
 
Design pattern - Facade Pattern
Design pattern - Facade PatternDesign pattern - Facade Pattern
Design pattern - Facade Pattern
 
Important React Hooks
Important React HooksImportant React Hooks
Important React Hooks
 
Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
 
Flyweight pattern
Flyweight patternFlyweight pattern
Flyweight pattern
 
Proxy Design Pattern
Proxy Design PatternProxy Design Pattern
Proxy Design Pattern
 
Basics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdfBasics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdf
 
Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory Pattern
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
 
React hooks
React hooksReact hooks
React hooks
 
Observer Software Design Pattern
Observer Software Design Pattern Observer Software Design Pattern
Observer Software Design Pattern
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
 
Servlets
ServletsServlets
Servlets
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 

Destaque

The visitor design pattern
The visitor design patternThe visitor design pattern
The visitor design patternSamnang Chhun
 
Applying Design Patterns in Practice
Applying Design Patterns in PracticeApplying Design Patterns in Practice
Applying Design Patterns in PracticeGanesh Samarthyam
 
Thomas hardy
Thomas hardyThomas hardy
Thomas hardyfoster2
 
Introduction to Thomas Hardy
Introduction to Thomas HardyIntroduction to Thomas Hardy
Introduction to Thomas HardyKaajal Teemal
 
The Role of Enterprise Integration in Digital Transformation
The Role of Enterprise Integration in Digital TransformationThe Role of Enterprise Integration in Digital Transformation
The Role of Enterprise Integration in Digital TransformationKasun Indrasiri
 
Design Patterns
Design PatternsDesign Patterns
Design Patternssoms_1
 

Destaque (9)

The visitor design pattern
The visitor design patternThe visitor design pattern
The visitor design pattern
 
The Visitor Pattern
The Visitor PatternThe Visitor Pattern
The Visitor Pattern
 
Applying Design Patterns in Practice
Applying Design Patterns in PracticeApplying Design Patterns in Practice
Applying Design Patterns in Practice
 
Composite Design Pattern
Composite Design PatternComposite Design Pattern
Composite Design Pattern
 
Thomas Hardy
Thomas HardyThomas Hardy
Thomas Hardy
 
Thomas hardy
Thomas hardyThomas hardy
Thomas hardy
 
Introduction to Thomas Hardy
Introduction to Thomas HardyIntroduction to Thomas Hardy
Introduction to Thomas Hardy
 
The Role of Enterprise Integration in Digital Transformation
The Role of Enterprise Integration in Digital TransformationThe Role of Enterprise Integration in Digital Transformation
The Role of Enterprise Integration in Digital Transformation
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 

Semelhante a Visitor pattern

C# Advanced L07-Design Patterns
C# Advanced L07-Design PatternsC# Advanced L07-Design Patterns
C# Advanced L07-Design PatternsMohammad Shaker
 
note2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptxnote2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptxReemaAsker1
 
Design Patterns Part1
Design Patterns  Part1Design Patterns  Part1
Design Patterns Part1Tom Chen
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesWalking Tree Technologies
 
The Singleton Pattern Presentation
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern PresentationJAINIK PATEL
 
Pavel_Kravchenko_Mobile Development
Pavel_Kravchenko_Mobile DevelopmentPavel_Kravchenko_Mobile Development
Pavel_Kravchenko_Mobile DevelopmentCiklum
 
Lecture 2 cst 205-281 oop
Lecture 2   cst 205-281 oopLecture 2   cst 205-281 oop
Lecture 2 cst 205-281 oopktuonlinenotes
 
Introduction to Design Patterns in Javascript
Introduction to Design Patterns in JavascriptIntroduction to Design Patterns in Javascript
Introduction to Design Patterns in JavascriptSanthosh Kumar Srinivasan
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxanguraju1
 
Gof design pattern
Gof design patternGof design pattern
Gof design patternnaveen kumar
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptAyush Sharma
 
Building a Recommender systems by Vivek Murugesan - Technical Architect at Cr...
Building a Recommender systems by Vivek Murugesan - Technical Architect at Cr...Building a Recommender systems by Vivek Murugesan - Technical Architect at Cr...
Building a Recommender systems by Vivek Murugesan - Technical Architect at Cr...Rajasekar Nonburaj
 

Semelhante a Visitor pattern (20)

C# Advanced L07-Design Patterns
C# Advanced L07-Design PatternsC# Advanced L07-Design Patterns
C# Advanced L07-Design Patterns
 
note2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptxnote2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptx
 
Design Patterns Part1
Design Patterns  Part1Design Patterns  Part1
Design Patterns Part1
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Angular js-crash-course
Angular js-crash-courseAngular js-crash-course
Angular js-crash-course
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree Technologies
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Mobile Application Development class 006
Mobile Application Development class 006Mobile Application Development class 006
Mobile Application Development class 006
 
The Singleton Pattern Presentation
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern Presentation
 
Angularjs Basics
Angularjs BasicsAngularjs Basics
Angularjs Basics
 
Pavel_Kravchenko_Mobile Development
Pavel_Kravchenko_Mobile DevelopmentPavel_Kravchenko_Mobile Development
Pavel_Kravchenko_Mobile Development
 
Lecture 2 cst 205-281 oop
Lecture 2   cst 205-281 oopLecture 2   cst 205-281 oop
Lecture 2 cst 205-281 oop
 
Introduction to Design Patterns in Javascript
Introduction to Design Patterns in JavascriptIntroduction to Design Patterns in Javascript
Introduction to Design Patterns in Javascript
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
 
Gof design pattern
Gof design patternGof design pattern
Gof design pattern
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
Recommender systems
Recommender systemsRecommender systems
Recommender systems
 
Building a Recommender systems by Vivek Murugesan - Technical Architect at Cr...
Building a Recommender systems by Vivek Murugesan - Technical Architect at Cr...Building a Recommender systems by Vivek Murugesan - Technical Architect at Cr...
Building a Recommender systems by Vivek Murugesan - Technical Architect at Cr...
 

Último

Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...BookNet Canada
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Nikki Chapple
 
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
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
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
 
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...amber724300
 
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
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFMichael Gough
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...Karmanjay Verma
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Karmanjay Verma
 
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
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Mark Simos
 

Último (20)

Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
 
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
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
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...
 
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
 
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
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDF
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#
 
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
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
 

Visitor pattern

  • 1. Visitor Pattern Prepared By: Nikunj Dhameliya(115375) Student @ gvp
  • 2. Overview Of Pattern:- ● Problem ● Intent ● Motivation ● Applicability ● Structure ● Participants ● Collaboration ● Consequences ● Related Pattern
  • 3. Real Life Problem Problem ● A Person Want to go at Office From Home Constraint ● Office Is So Far From His Home. ● And He Don't Have Any Personal Vehicle.
  • 4. Possible Solutions ● Person May Go to Office... – by Taxi – by Auto – by Bus – by Train
  • 5. Solution ● The Visitor pattern represents an operation to be performed on the elements of an object structure without changing the classes on which it operates. This pattern can be observed in the operation of a taxi company. When a person calls a taxi company (accepting a visitor), the company dispatches a cab to the customer. Upon entering the taxi the customer, or Visitor, is no longer in control of his or her own transportation.
  • 8. Intent:- ● Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the element on which it operates.
  • 9. Motivation:- ● Problems with this approach: ➢ Adding new operations requires changes to all of the node classes ➢ It can be confusing to have such a diverse set of operations in each node class. For example, mixing type-checking code with pretty-printing code can be hard to understand and maintain. ● Another solution is to encapsulate a desired operation in a separate object, called a visitor. The visitor object then traverses the elements of the tree. When an tree node "accepts" the visitor, it invokes a method on the visitor that includes the node type as an argument. The visitor will then execute the operation for that node - the operation that used to be in the node class.
  • 10. Applicability:- Use the Visitor Pattern When ➢ There are many distinct and unrelated operations needed to be performed. Visitor pattern allows us to create a separate visitor concrete class for each type of operation and to separate this operation implementation from the objects structure. ➢ The object structure is not likely to be changed but is very probable to have new operations which have to be added. Since the pattern separates the visitor (representing operations, algorithms, behaviors) from the object structure it's very easy to add new visitors as long as the structure remains unchanged. ➢ Similar operations have to be performed on objects of different types grouped in a structure (a collection or a more complex structure)
  • 11. Participants:- ● Visitor ➢ Declares a Visit operation for each class of ConcreteElement in the object structure.The operation's name and signature identifies the class that sends the Visit request to the visitor.That lets the visitor determine the concrete class of the element being visited.Then the visitor can access the element directly through its particular interface. ● ConcreteVisitor ➢ Implements each operation declared by Visitor.Each operation implements a fragment of the algorithm defined for the corresponding class of object in the structure.ConcretVisitor provides the context for the algorithm and stores its local state.This state often accumulates results during the traversal of the structure. ➢ Element ➢ Defines an Accept operation that takes a visitor as an argument.
  • 12. ● ConcreteElement ➢ Implements an Accept operation that takes a visitor as an argument ● ObjectStructure ➢ Can enumerate its elements. ➢ May provide a high-level interface to allow the visitor to visit its elements. Continue...
  • 13. Collaboration:- ➢ A client that uses the Visitor pattern must create a ConcreteVisitor object and then traverse the object structure,visiting each element with the visitor. ➢ When an element is visited, it calls the Visitor operation that corresponds to its class. The element supplies itselef as an argument to this operation to let the visitor access its state, if necessary.
  • 14. Consequences:- ● Benefits ➢ Adding new operations is easy. ➢ Related behavior isn't spread over the classes defining the object structure; it's localized in a visitor. Unrelated sets of behavior are partitioned in their own visitor subclasses. ● Liabilities ● Adding new ConcreteElement classes is hard. Each new ConcreteElement gives rise to a new abstract operation on Visitor and a corresponding implementation in every ConcreteVisitor class.
  • 15. ● Single-Dispatch ➢ The actual method invoked depends on the name of the request (method signature) and the type of the receiver object. ● Double-Dispatch ➢ The actual method invoked depends on the name of the request and the types of two receivers. Continue...
  • 16. Related Pattern:- ● Composite ➢ Visitors can be used to apply an operation over an object structure defined by the Composite pattern. ● Interpreter ➢ Visitor may be applied to do the interpretation.