SlideShare une entreprise Scribd logo
1  sur  25
Visitor Pattern

             Yateen L. Nikharge
         Aravindh Manickavasagam
          Ider Zheng



                            S
The VISITOR Family


The Visitor family allows new methods to be added
  to existing hierarchies without modifying the
  hierarchies.
The Patterns in this family are:
S VISITOR

S ACYCLIC VISITOR

S DECORATOR

S EXTENSION OBJECT
Intent


S Represent an operation to be performed on the
  elements of an object structure.
S Visitor lets you define a new operation without
  changing the classes of the elements on which it
  operates.
Motivation


S Decouple the data structure and algorithms.

S Allow addition of new algorithms without changing the data
   structure.

S Example: Modem Configuration
  S To configure a modem for Unix, create an instance of the visitor
     and pass it to accept.
   S The appropriate derivative call visit(this)
   S New OS configuration can be added by adding a new
     derivative of visitor.
Motivation (contd…)
Motivation(contd…)
Applicability

Use the pattern when-
S An object structure contains many classes of
  objects with differing interfaces…
S Many distinct and unrelated operations need to
  be performed on objects in an object structure…
S The classes defining the object structure rarely
  change, but you often want to define new
  operations over the structure…
Structure
Participants

S Visitor (ModemVisitor):
       Declares a Visit operation or each class…

S ConcreteVisitor (ConfigureUnixVisitor):
       Implements each Visit…

S Element (Modem) :
       Defines an Accept operation…

S ConcreteElement(ZoomModem):
       Implements Accept …
Participants(Contd…)


S ObjectStructure (Program):
     May provide a high-level interface to allow the visitor to visit its
     elements .
Collaborations

S A client using the Visitor pattern must create a ConcreteVisitor
  object and traverse the object structure, visiting each element
  with the visitor.



S When an element is visited, it calls the visitor operation that
  corresponds to its class. The element supplies itself as an
  argument to this operation to let the visitor access its state, if
  necessary.
Collaborations cont.
Consequences-Benefits


S Adding new operations

S Gathers related operations and separates unrelated ones

S Visitors can visit objects that don’t have a common parent
  class

S Visitors can accumulate state as they visit each element
Consequences - Liabilities


S Adding new ConcreteElement classes is hard



S Allowing a Visitor to access the internal state of a
  Concrete Element breaks encapsulation
Implementation - Issues


S Double Dispatch

S Who is Responsible for Traversing the Object Structure?
  S Object Structure


  S   Visitor – whenever operations depend on other operations
      on the object structure

  S   Iterator Object
Sample Codes
Implementation



Function Pointer
Implementation



Global Function
Implementation



Static Methods
   Functor
Implementation


        Graphic Project
From CSE 687 Objected Oriented Design
Graphic

         “v1”           “e5”         “v3”
  “e1”
                “e2”
                                 “e9”
“v4”                   “v7”
                                 “e7”
           “e4”
                                        “v5”
         “v2”            “e3”

                 “e8”                “e6”
                              “v6”
Known Uses


S Smalltalk-80 Compiler

S IRIS Inventor Toolkit

S X Consortium’s Fresco Application Toolkit

S Bistro Programming Language Compiler
Related Patterns


S Composite

S Interpreter
References

S Design Patterns: Elements of Reusable Object-Oriented
  Software

  By Erich Gamma, Richard Helm, Ralph
  Johnson, and John Vlissides

Shttp://www.codeproject.com/KB/aspnet/SoftArch3.aspx

Contenu connexe

Tendances

Design Patterns
Design PatternsDesign Patterns
Design Patterns
soms_1
 
Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
Sara Torkey
 

Tendances (20)

Facade pattern
Facade patternFacade pattern
Facade pattern
 
Chain of responsibility
Chain of responsibilityChain of responsibility
Chain of responsibility
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Bridge Design Pattern
Bridge Design PatternBridge Design Pattern
Bridge Design Pattern
 
The Singleton Pattern Presentation
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern Presentation
 
Bridge pattern
Bridge patternBridge pattern
Bridge pattern
 
Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3
 
Builder Design Pattern (Generic Construction -Different Representation)
Builder Design Pattern (Generic Construction -Different Representation)Builder Design Pattern (Generic Construction -Different Representation)
Builder Design Pattern (Generic Construction -Different Representation)
 
Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory Pattern
 
NestJS
NestJSNestJS
NestJS
 
Nestjs MasterClass Slides
Nestjs MasterClass SlidesNestjs MasterClass Slides
Nestjs MasterClass Slides
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
 
Proxy pattern
Proxy patternProxy pattern
Proxy pattern
 
The State Design Pattern
The State Design PatternThe State Design Pattern
The State Design Pattern
 
Design pattern & categories
Design pattern & categoriesDesign pattern & categories
Design pattern & categories
 
Introduction to design patterns
Introduction to design patternsIntroduction to design patterns
Introduction to design patterns
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
 

En vedette

Android Bonnees pratiques
Android Bonnees pratiques Android Bonnees pratiques
Android Bonnees pratiques
Patrick Bashizi
 
Iterator Pattern Baljeet Sandhu 20060621
Iterator Pattern Baljeet Sandhu 20060621Iterator Pattern Baljeet Sandhu 20060621
Iterator Pattern Baljeet Sandhu 20060621
melbournepatterns
 

En vedette (20)

Design Pattern introduction
Design Pattern introductionDesign Pattern introduction
Design Pattern introduction
 
Sibtel&Swift
Sibtel&SwiftSibtel&Swift
Sibtel&Swift
 
De java à swift en 2 temps trois mouvements
De java à swift en 2 temps trois mouvementsDe java à swift en 2 temps trois mouvements
De java à swift en 2 temps trois mouvements
 
Android Bonnees pratiques
Android Bonnees pratiques Android Bonnees pratiques
Android Bonnees pratiques
 
Design Pattern
Design PatternDesign Pattern
Design Pattern
 
Cours1
Cours1Cours1
Cours1
 
Design patterns through java
Design patterns through javaDesign patterns through java
Design patterns through java
 
Iterator Pattern Baljeet Sandhu 20060621
Iterator Pattern Baljeet Sandhu 20060621Iterator Pattern Baljeet Sandhu 20060621
Iterator Pattern Baljeet Sandhu 20060621
 
Eclipse e4 Overview
Eclipse e4 OverviewEclipse e4 Overview
Eclipse e4 Overview
 
Design Pattern: Développement et Bonnes pratiques
Design Pattern: Développement et Bonnes pratiquesDesign Pattern: Développement et Bonnes pratiques
Design Pattern: Développement et Bonnes pratiques
 
Java Design Patterns: The State Pattern
Java Design Patterns: The State PatternJava Design Patterns: The State Pattern
Java Design Patterns: The State Pattern
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2
 
Getting Started with IntelliJ IDEA as an Eclipse User
Getting Started with IntelliJ IDEA as an Eclipse UserGetting Started with IntelliJ IDEA as an Eclipse User
Getting Started with IntelliJ IDEA as an Eclipse User
 
Iterator Design Pattern
Iterator Design PatternIterator Design Pattern
Iterator Design Pattern
 
Design pattern
Design patternDesign pattern
Design pattern
 
Composite pattern
Composite patternComposite pattern
Composite pattern
 
Swift, opportunités et perspectives du dernier langage d'Apple
Swift, opportunités et perspectives du dernier langage d'AppleSwift, opportunités et perspectives du dernier langage d'Apple
Swift, opportunités et perspectives du dernier langage d'Apple
 
Design patterns - Exemples en Java
Design patterns - Exemples en JavaDesign patterns - Exemples en Java
Design patterns - Exemples en Java
 
Composite Design Pattern
Composite Design PatternComposite Design Pattern
Composite Design Pattern
 
JAVA design patterns and Basic OOp concepts
JAVA design patterns and Basic OOp conceptsJAVA design patterns and Basic OOp concepts
JAVA design patterns and Basic OOp concepts
 

Similaire à Visitor Pattern

Александр Белецкий "Архитектура Javascript приложений"
 Александр Белецкий "Архитектура Javascript приложений" Александр Белецкий "Архитектура Javascript приложений"
Александр Белецкий "Архитектура Javascript приложений"
Agile Base Camp
 
Design patterns intro
Design patterns introDesign patterns intro
Design patterns intro
Jean Pаoli
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
danhaley45372
 
GUFRAN_CV(.NET DEVELOPER)
GUFRAN_CV(.NET DEVELOPER)GUFRAN_CV(.NET DEVELOPER)
GUFRAN_CV(.NET DEVELOPER)
bhutu
 

Similaire à Visitor Pattern (20)

Prophecy Of Design Patterns
Prophecy Of Design PatternsProphecy Of Design Patterns
Prophecy Of Design Patterns
 
L03 Software Design
L03 Software DesignL03 Software Design
L03 Software Design
 
Developing Android and iOS Apps With C#, .NET, Xamarin, Mono, and Windows Azure
Developing Android and iOS Apps With C#, .NET, Xamarin, Mono, and Windows AzureDeveloping Android and iOS Apps With C#, .NET, Xamarin, Mono, and Windows Azure
Developing Android and iOS Apps With C#, .NET, Xamarin, Mono, and Windows Azure
 
Visual C++ project model
Visual C++ project modelVisual C++ project model
Visual C++ project model
 
From Backbone to Ember and Back(bone) Again
From Backbone to Ember and Back(bone) AgainFrom Backbone to Ember and Back(bone) Again
From Backbone to Ember and Back(bone) Again
 
Александр Белецкий "Архитектура Javascript приложений"
 Александр Белецкий "Архитектура Javascript приложений" Александр Белецкий "Архитектура Javascript приложений"
Александр Белецкий "Архитектура Javascript приложений"
 
Building modular software with OSGi - Ulf Fildebrandt
Building modular software with OSGi - Ulf FildebrandtBuilding modular software with OSGi - Ulf Fildebrandt
Building modular software with OSGi - Ulf Fildebrandt
 
Angular js workshop
Angular js workshopAngular js workshop
Angular js workshop
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Design patterns intro
Design patterns introDesign patterns intro
Design patterns intro
 
Design patterns in Swift Ch4: Iterator & Template Method
Design patterns in Swift Ch4: Iterator & Template MethodDesign patterns in Swift Ch4: Iterator & Template Method
Design patterns in Swift Ch4: Iterator & Template Method
 
07_UIAndroid.pdf
07_UIAndroid.pdf07_UIAndroid.pdf
07_UIAndroid.pdf
 
MVC pattern for widgets
MVC pattern for widgetsMVC pattern for widgets
MVC pattern for widgets
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
 
Gof design pattern
Gof design patternGof design pattern
Gof design pattern
 
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
 
Dicoding Developer Coaching #31: Android | Menerapkan Clean Architecture di A...
Dicoding Developer Coaching #31: Android | Menerapkan Clean Architecture di A...Dicoding Developer Coaching #31: Android | Menerapkan Clean Architecture di A...
Dicoding Developer Coaching #31: Android | Menerapkan Clean Architecture di A...
 
GUFRAN_CV(.NET DEVELOPER)
GUFRAN_CV(.NET DEVELOPER)GUFRAN_CV(.NET DEVELOPER)
GUFRAN_CV(.NET DEVELOPER)
 
02 objective-c session 2
02  objective-c session 202  objective-c session 2
02 objective-c session 2
 

Dernier

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Dernier (20)

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 

Visitor Pattern

  • 1. Visitor Pattern Yateen L. Nikharge Aravindh Manickavasagam Ider Zheng S
  • 2. The VISITOR Family The Visitor family allows new methods to be added to existing hierarchies without modifying the hierarchies. The Patterns in this family are: S VISITOR S ACYCLIC VISITOR S DECORATOR S EXTENSION OBJECT
  • 3. Intent S Represent an operation to be performed on the elements of an object structure. S Visitor lets you define a new operation without changing the classes of the elements on which it operates.
  • 4. Motivation S Decouple the data structure and algorithms. S Allow addition of new algorithms without changing the data structure. S Example: Modem Configuration S To configure a modem for Unix, create an instance of the visitor and pass it to accept. S The appropriate derivative call visit(this) S New OS configuration can be added by adding a new derivative of visitor.
  • 7. Applicability Use the pattern when- S An object structure contains many classes of objects with differing interfaces… S Many distinct and unrelated operations need to be performed on objects in an object structure… S The classes defining the object structure rarely change, but you often want to define new operations over the structure…
  • 9. Participants S Visitor (ModemVisitor): Declares a Visit operation or each class… S ConcreteVisitor (ConfigureUnixVisitor): Implements each Visit… S Element (Modem) : Defines an Accept operation… S ConcreteElement(ZoomModem): Implements Accept …
  • 10. Participants(Contd…) S ObjectStructure (Program): May provide a high-level interface to allow the visitor to visit its elements .
  • 11. Collaborations S A client using the Visitor pattern must create a ConcreteVisitor object and traverse the object structure, visiting each element with the visitor. S When an element is visited, it calls the visitor operation that corresponds to its class. The element supplies itself as an argument to this operation to let the visitor access its state, if necessary.
  • 13. Consequences-Benefits S Adding new operations S Gathers related operations and separates unrelated ones S Visitors can visit objects that don’t have a common parent class S Visitors can accumulate state as they visit each element
  • 14. Consequences - Liabilities S Adding new ConcreteElement classes is hard S Allowing a Visitor to access the internal state of a Concrete Element breaks encapsulation
  • 15. Implementation - Issues S Double Dispatch S Who is Responsible for Traversing the Object Structure? S Object Structure S Visitor – whenever operations depend on other operations on the object structure S Iterator Object
  • 18.
  • 21. Implementation Graphic Project From CSE 687 Objected Oriented Design
  • 22. Graphic “v1” “e5” “v3” “e1” “e2” “e9” “v4” “v7” “e7” “e4” “v5” “v2” “e3” “e8” “e6” “v6”
  • 23. Known Uses S Smalltalk-80 Compiler S IRIS Inventor Toolkit S X Consortium’s Fresco Application Toolkit S Bistro Programming Language Compiler
  • 25. References S Design Patterns: Elements of Reusable Object-Oriented Software By Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Shttp://www.codeproject.com/KB/aspnet/SoftArch3.aspx