16. Visitors SE2811 Software Component Design

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

Chapter 11 Component-Level Design
4. Object-Oriented Programming Procedural programming Structs and objects Object-oriented programming Concepts and terminology Related keywords.
Containers and Components 1.Containers collect GUI components 2.Sometimes, want to add a container to another container 3.Container should be a component.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Design Patterns In OPM Presented by: Galia Shlezinger Instructors: Prop. Dov Dori, Dr. Iris Berger.
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.
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.
CSE 331 Software Design & Implementation Hal Perkins Winter 2013 Design Patterns Part 3 (Slides by Mike Ernst and David Notkin) 1.
SE2811 Week 7, Class 1 Composite Pattern Applications Conceptual form Class structure Coding Example Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark.
Go4 Visitor Pattern Presented By: Matt Wilson. Introduction 2  This presentation originated out of casual talk between former WMS “C++ Book Club” (defunct.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Lexi case study (Part 2) Presentation by Matt Deckard.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Information Systems Engineering
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1 Class 1-2.
....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.
SE-2811 Software Component Design Week 1, Day 2 (and 1-3 and 2-1) SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick 1.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Use Case Textual Analysis
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Visitor Design Pattern and Java Tree Builder
04 - OOD Intro.CSC4071 Software Design ‘Requirements’ defines –The goals the system needs to satisfy. ‘Specification’ defines –The externally-observable.
Week 6, Day 3 The Gang of Four and more … A new design pattern SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Model-View-Controller A Design Pattern SE-2030 Dr. Rob Hasker 1 Based on slides written by Dr. Mark L. Hornick Used with permission.
Design Patterns (II) Lecture Three. Solution to Homework Two Used framework Used design patterns: composite and state Question: what are the differences.
High Level Design Use Case Textual Analysis SE-2030 Dr. Mark L. Hornick 1.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
SE-2811 Software Component Design Week 1, Day 1 Design pattern defined Code that needs a design pattern… SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick.
WELCOME TO OUR PRESENTATION UNIFIED MODELING LANGUAGE (UML)
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
Common Design Patterns
Behavioral Design Patterns
Composite Pattern SE2811 Software Component Design
SE-2811 Software Component Design
Model-View-Controller
Component-Level Design
SE-2811 Software Component Design
Informatics 122 Software Design II
Behavioral Patterns Part-I introduction UNIT-VI
Menu item at a restaurant
Third lecture Review Visitor pattern DemeterJ 12/25/2018 SD.
SE-2811 Software Component Design
SE2811 Software Component Design Dr. Rob Hasker
Third lecture Review Visitor pattern DemeterJ 2/17/2019 SD.
7. Decorator SE2811 Software Component Design
8. Observer Pattern SE2811 Software Component Design
Software Design Principles
14. Factory Pattern SE2811 Software Component Design
4: Object-oriented Analysis & Design
SE2811 Software Component Design Dr. Rob Hasker
13. Composite Pattern SE2811 Software Component Design
10. Façade Pattern SE2811 Software Component Design
Week 6, Class 2: Observer Pattern
Informatics 122 Software Design II
13. Composite Pattern SE2811 Software Component Design
11. MVC SE2811 Software Component Design
11. MVC SE2811 Software Component Design
SE2811 Software Component Design Dr. Rob Hasker
16. Visitors SE2811 Software Component Design
Visitor Pattern Intent
Lecture 2 – Abstract Data Type (ADT)
Lecture 3 – Data collection List ADT
Presentation transcript:

16. Visitors SE2811 Software Component Design Dr. Rob Hasker (based on slides by Dr. Mark Hornick)

Recall the Composite example: computer Structure Computer System as a collection of Components (Parts & Composites) Operations Compute total price Compute power consumption Compute total weight computer System Unit monitor HDD Cabinet Chassis CPU keyboard Memory GPU Fan Motherboard

Diagram, code… Duplicated code: loops, accumulator Does not scale class Composite { … public void priceInCents() { int total = basePrice; for(Component c : components) total += c.priceInCents(); return total; } public double weight() { double total = 0.0; total += c.weight(); public double amps() { /*similar*/ } computer System Unit monitor HDD Cabinet Chassis CPU keyboard Memory GPU Fan Motherboard Scaling: suppose we add checks for hazardous materials, checks that no price/weight/amp is negative, no composite has empty component lists Duplicated code: loops, accumulator Does not scale

A problem and a solution Violates DRY: Don’t repeat yourself! Repeated code: traversing the structure Solution: Construct class to visit all the elements of the structure Visitor: “walk through” the structure, recursively visiting sub-elements computer System Unit monitor HDD Cabinet Chassis CPU keyboard Memory GPU Fan Motherboard

A problem and a solution Violates DRY: Don’t repeat yourself! Repeated code: traversing the structure Solution: Construct class to visit all the elements of the structure Visitor: “walk through” the structure, recursively visiting sub-elements Another case of making an action into a class Visitor: apply the action everywhere computer System Unit monitor HDD Cabinet Chassis CPU keyboard Memory GPU Fan Motherboard

The Visitor pattern in UML:

Simple visitor Client creates visitor to acquire information Apply visitor to each element Call accept() Pass element to accept Simple case: single ConcreteElement Element interface modified to allow visitor ConcreteElement: provides public methods that can be invoked by visitor Typically: visitor will have attributes used to keep a running tally of operations Example: running total, count of items meeting a condition

The Visitor pattern in UML: public abstract class ComponentVisitor { public abstract void visit(Component c); } public class WeightVisitor { private double totalGrams = 0.0; public double getTotalGrams() { return totalGrams; public void visit(Component c) { totalGrams += c.unitWeight(); Code for the visitor, using WeightVisitor as an example; note how it has an accumulator. Power visitor would be similar.

The Visitor pattern in UML: abstract class Component { abstract void accept(ComponentVisitor); } public class Part extends Component { public void accept(ComponentVisitor v) { v.visit(this); … public class Composite { for(Component c : components) c.accept(v); The Visitor pattern in UML: public void visit(Component c) { totalGrams += c.unitWeight(); } Visiting component: calls accept, and this processes current component and calls accept for any children; recall visit adds the unit weight to the total grams

Museum Example: see demo folder

Visiting multiple types of objects Each Visitor has specific methods that target each type of ConcreteElement to be visited. Common: object hierarchy implements Composite Not mandatory! Client: capability to traverse the hierarchy Client applies visitor to each element Each ConcreteElement provides a specific public method that the Visitors invoke in order to perform their operation(s).

Collaborations Client ConcreteElementA ConcreteElementB ConcreteVisitorA accept(aVisitor) visitConcreteElementA(this) operationA() visitConcreteElementB(aConcreteElementB) operationB() aVisitor=new ConcreteVisitorA() getResultA() getResultB()

Advantages Visitor makes adding new operations relatively easy If a new operation over the object structure is needed, just create another type of ConcreteVisitor Each Visitor gathers related operations and separates unrelated ones. Unrelated behaviors are implemented in their own Visitors.

Disadvantages A lot of code has to be written to prepare for the Visitor pattern: The Visitor class/interface with one abstract “visit_xxx” method per ConcreteElement class to be visited must be defined. An accept() method has to be added to each ConcreteElement class to be visited Public methods must be provided on each ConcreteElement to provide Visitor access Arguments and the return type of visit_xxx() methods have to be known in order to write abstract visitor ConcreteElements must be modified to accommodate the pattern as well Encapsulation is compromised – public methods must be provide sufficient access to ConcreteElement to let the Visitor perform its function; thereby exposing internal implementation The code is more obscure

Review Visitor: traverse through nested structure, applying an operation to each element Useful when have large number of operations to apply to structure Tracing the visitor call sequence is challenging

So how to design software systems? Answer the following in small groups and share w/ the class: What are the elements of design (for software)? How can we apply those elements to implement requirements? What are the two forms of requirements? Not discussed: ensuring testability! Why start with the domain during design? What is the goal of applying design patterns? How many patterns should we use on a project? Elements of design: identify domain, introduce containers and consider other big-O issues, introduce patterns Real projects: also design the interface