Visitor Pattern Intent

Slides:



Advertisements
Similar presentations
Behavioral Pattern: Visitor C h a p t e r 5 – P a g e 224 When an algorithm is separated from an object structure upon which it operates, new operations.
Advertisements

©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
Winter 2007ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Visitor Pattern Jeff Schott CS590L Spring What is the Purpose of the Visitor Pattern ? n Represent an operation to be performed on the elements.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.
Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
1 Dept. of Computer Science & Engineering, York University, Toronto Software Development CSE3311 Composite Pattern.
The Interpreter Pattern. Defining the Interpreter Intent Given a language, define a representation for its grammar along with an interpreter that uses.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Composite Design Pattern. Motivation – Dynamic Structure.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
Design Patterns.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
Case Studies on Design Patterns Design Refinements Examples.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Software Components Creational Patterns.
 How are you going to collaborate?  How are you going to divide up work?  How are you going to make sure that changes work with other people’s code?
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
....and other creepy things from John Vlissides The Visitor Pattern EXISTS! And its INTENT is to represent an operation to be performed on the elements.
ECE450S – Software Engineering II
Where Do Surrogates Fit into This Proxy Pattern Observer Pattern Visitor Pattern By Kurt Rehwinkel.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
ANU COMP2110 Software Design in 2004 Lecture 17Slide 1 COMP2110 in 2004 Software Design Lecture 17: Software design patterns (4) 1The Abstract Factory.
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Testing OO software. State Based Testing State machine: implementation-independent specification (model) of the dynamic behaviour of the system State:
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Wei-Tek Tsai who’s.
CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
CS 5150 Software Engineering Lecture 16 Program Design 3.
Week 6: Software Design HNDIT Software Engineering Software Design Learning Outcomes  Understand the activities involved in the Design process.
Design Patterns (II) Lecture Three. Solution to Homework Two Used framework Used design patterns: composite and state Question: what are the differences.
The Visitor Design Pattern. What’s It All About? Allows for new operations to be defined and used on elements of an object structure with out changing.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Design Patterns: MORE Examples
Abstract Factory Pattern
Strategy Design Pattern
Factory Patterns 1.
Introduction to Design Patterns
Behavioral Design Patterns
Software Design and Architecture
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
More Design Patterns 1.
Interfaces.
Design Patterns - A few examples
More Design Patterns 1.
Abstract Factory Pattern
Design Pattern Detection
Jim Fawcett CSE776 – Design Patterns Summer 2003
Software Development CSE3311
Design Pattern: Visitor
Informatics 122 Software Design II
Composite Pattern Context:
Third lecture Review Visitor pattern DemeterJ 12/25/2018 SD.
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Third lecture Review Visitor pattern DemeterJ 2/17/2019 SD.
Context Objects Evolution of object behavior Behavioral patterns
Lecture 8 Evolution of object behavior Behavioral patterns
Strategy Design Pattern
Informatics 122 Software Design II
16. Visitors SE2811 Software Component Design
16. Visitors SE2811 Software Component Design
Refactoring.
Presentation transcript:

Visitor Pattern Intent Represent an operation to be performed on all of the components of an object structure Define new operations on a structure without changing the classes representing the components

Visitor – Motivation Representing composite computer equipment Operations spread out OO style but ... difficult to add a new operation describe * price * FLOPPY_DISK + COMPOSITE_EQUIPMENT + .... describe + price + describe + price +

Visitor Architecture – Equipment Example Equipment classes are independent of the operations Equipment accept visitors and direct them to the appropriate operation – through polymorphism * EQUIPMENT_VISITOR accept * EQUIPMENT + PRICING_VISITOR + CHASSIS accept visit_chassis One subclass of EQUIPMENT_VISITOR for each operation One subclass of EQUIPMENT for each concrete piece of equipment

Visitor – Applicability Object structure contains many classes of objects with differing interfaces and want to perform operations that depend on their concrete classes Many distinct and unrelated operations need to be performed Do not want to or are unable to clutter the concrete classes with these operations Keep the related operations together, e.g. operations on pricing various types of EQUIPMENT are all in the PRICING_VISITOR class If the object structure is shared by many applications, Visitor puts operations into only those applications that need them

Visitor – Applicability – 2 The classes defining the object structure rarely change, but you often want to define new operations over the structure Changing object structure means redefining interface to all visitors, which is costly If object structure changes often, Visitor is inappropriate

Visitor – Abstract Architecture ELEMENT * visit_elem_A ( ELEM_A ) * visit_elem_B ( ELEM_B ) * ... accept ( VISITOR ) * ... ELEM_A + CONCRETE_VISITOR_1 + accept ( VISITOR ) + ... visit_elem_A ( ELEM_A ) + visit_elem_B ( ELEM_B ) + ... ELEM_B + CONCRETE_VISITOR_2 +

Visitor – Participants Element Declares accept method that takes a visitor as an argument Concrete element Implements the accept method Visitor Declares a visit operation for each concrete element class

Visitor – Participants – 2 Concrete visitor Implements every visit operation declared in Visitor Each visit operation implements a fragment of the algorithm defined for the concrete visitor Provides the context for the algorithm composed of all of the visit fragments State accumulates with each visit Implements the high level organization Iteration over the components Processing each in turn

Visitor – Scenario A pricing visitor visits a composite equipment For each equipment, the pricing visitor selects which method from the EQUIPMENT_VISITOR interface to execute Scenario: Visit a cabinet 1 Accept the pricing visitor 2 visit_cabinet 3 visit_children 4 Accept the pricing visitor MAIN 1 2 PRICING_VISITOR CABINET 3 4 CHASSIS

Visitor – Consequences Adding new operations is easy New operation implements the Visitor interface, e.g. the methods in EQUIPMENT_VISITOR All the fragments of the visitor algorithm are in one file – related behaviours are together Easier to make sure that elements are working in unison Unrelated operations and fragments are in other visitor classes Contrast with having to change each of the concrete element classes to have the operation fragment Each class has a fragment of each of the operations

Visitor – Consequences – 2 Adding new concrete elements is difficult Need to modify the Visitor interface Need to modify each concrete visitor Can sometimes simplify as many elements have common behaviour (default behaviour) that can be specified at concrete visitor level 1. Create subclasses of level 1 for more specific behaviour for the new elements Only program the new elements For many structures elements do not change rapidly so this is not a problem

Visitor – Consequences – 3 One can visit object structures composed of unrelated objects The various visit_* methods do not have to take arguments that are related The main difference between Visitor and Iterator State accumulation Visitors contain data structures related to the operation they implement, e.g. the PRICING_VISITOR contains the total price Without a visitor, such data would have to be passed as arguments (or become global variables)

Visitor – Related Patterns Visitor pattern is used to apply an operation over a Composite