Chapter 8, Design Patterns Visitor

Similar documents
Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

The Visitor Pattern. Design Patterns In Java Bob Tarr

Object-Oriented Oriented Programming

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

Visitor Pattern CS356 Object-Oriented Design and Programming November 5, 2014 Yu Sun, Ph.D.

Introduction to Software Engineering: Object Design I Reuse & Patterns

Visitor Pattern.» Represent an operation to be performed on all of the components of an object structure

Design Patterns Reid Holmes

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Design Patterns. Software Engineering. Sergio Feo-Arenis slides by: Matthias Keil

Design Patterns. Softwaretechnik. Matthias Keil. Albert-Ludwigs-Universität Freiburg

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

SDC Design patterns GoF

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

UNIT I Introduction to Design Patterns

Using Design Patterns in Java Application Development

Softwaretechnik. Design Patterns. Matthias Keil. Albert-Ludwigs-Universität Freiburg

Object-Oriented Oriented Programming

The Strategy Pattern Design Principle: Design Principle: Design Principle:

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Softwaretechnik. Design Patterns. Stephan Arlt SS University of Freiburg. Stephan Arlt (University of Freiburg) Softwaretechnik SS / 47

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

INSTITUTE OF AERONAUTICAL ENGINEERING

Laboratorio di Sistemi Software Design Patterns 2

Material and some slide content from: - GoF Design Patterns Book. Design Patterns #1. Reid Holmes. Lecture 11 - Tuesday October

Summary of the course lectures

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Functional Design Patterns. Rumours. Agenda. Command. Solution 10/03/10. If you only remember one thing. Let it be this:

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Design Patterns #3. Reid Holmes. Material and some slide content from: - GoF Design Patterns Book - Head First Design Patterns

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

UNIT I Introduction to Design Patterns

Topics. Software Process. Agile. Requirements. Basic Design. Modular Design. Design Patterns. Testing. Quality. Refactoring.

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

CS251 Software Engineering Lectures 18: Intro to DP

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Iterator Pattern George Blankenship

Design Patterns. An introduction

Pattern Examples Behavioural

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

Design Patterns V Structural Design Patterns, 2

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

What is a Pattern? Lecture 40: Design Patterns. Elements of Design Patterns. What are design patterns?

TDDB84: Lecture 6. Adapter, Bridge, Observer, Chain of Responsibility, Memento, Command. fredag 4 oktober 13

Design Patterns. GoF design patterns catalog

Applying the Observer Design Pattern

Design Pattern and Software Architecture: IV. Design Pattern

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

Software Eningeering. Lecture 9 Design Patterns 2

Design Patterns Lecture 2

A few important patterns and their connections

Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

Topics in Object-Oriented Design Patterns

Singleton Pattern Creational

The GoF Design Patterns Reference

A Reconnaissance on Design Patterns

Tuesday, October 4. Announcements

SYLLABUS CHAPTER - 1 [SOFTWARE REUSE SUCCESS FACTORS] Reuse Driven Software Engineering is a Business

Object-Oriented Design

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

Chapter 4. Abstract Syntax

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Object Design II: Design Patterns

Singleton Pattern Creational. » Ensure a class has only one instance» Provide a global point of access

Lecture 4: Observer Pattern, Event Library and Componentization

Design Patterns. Gunnar Gotshalks A4-1

CS/CE 2336 Computer Science II

C++ for System Developers with Design Pattern

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

Applying Design Patterns to SCA Implementations

Design Patterns Reid Holmes

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

Design Patterns. Hausi A. Müller University of Victoria. Software Architecture Course Spring 2000

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Winter /22/ Hal Perkins & UW CSE H-1

Overview CS Kinds of Patterns. Design Pattern. Factory Pattern Rationale. Kinds of Factory Patterns

Software Design COSC 4353/6353 D R. R A J S I N G H

COSC 3351 Software Design. Design Patterns Structural Patterns (I)

CS 520/620 Advanced Software Engineering Fall September 27, 2016

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

Page 1. Chapter 8, Object Design: Design Patterns II. Recall: Why reusable Designs? Definitions. A Taxonomy of Design Patterns

EINDHOVEN UNIVERSITY OF TECHNOLOGY

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Autumn /20/ Hal Perkins & UW CSE H-1

Requirements and Design Overview

Applying the Decorator Design Pattern

Lecture 20: Design Patterns II

Behavioral patterns. Command Iterator Observer Strategy Template method

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

Design Pattern- Creational pattern 2015

administrivia today UML start design patterns Tuesday, September 28, 2010

Design Patterns: Structural and Behavioural

Design Patterns 2. Page 1. Software Requirements and Design CITS 4401 Lecture 10. Proxy Pattern: Motivation. Proxy Pattern.

Object-oriented Software Design Patterns

Overview of Patterns: Introduction

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

Design Patterns. Observations. Electrical Engineering Patterns. Mechanical Engineering Patterns

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

UP Requirements. Software Design - Dr Eitan Hadar (c) Activities of greater emphasis in this book. UP Workflows. Business Modeling.

Design Patterns Revisited

Transcription:

Chapter 8, Design Patterns Visitor Using UML, Patterns, and Java Object-Oriented Software Engineering

Pattern A Pattern Taxonomy Structural Pattern Behavioral Pattern Creational Pattern Composite Decorator Adapter Bridge Façade Proxy Iterator Visitor Command Observer Template Strategy Singleton Abstract Factory Builder Factory Prototype Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 2

the basics 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 elements on which it operates aka: none! These slides were borrowed from lecture notes on Design Patterns by Brian Malloy at Clemson University Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 3

Motivation Consider a compiler that represents programs as ASTs need to perform operations on ASTs type checking code generation printing One option: place all of these operations in the nodes of the AST Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 4

Node class hierarchy Node typecheck() generatecode(); prettyprint(); VariableRefNode typecheck() generatecode(); prettyprint(); AssignmentNode typecheck() generatecode(); prettyprint(); Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 5

Problem with Node class hierarchy Distributing operations across Node classes leads to a system that s hard to understand, maintain and change. it s confusing to have type-checking code in the same class with code to perform pretty printing. also, adding a new operation, e.g. data flow analysis, requires recompiling all of the classes! Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 6

Visitor allows us to separate Node classes from operations on Node classes allows each new operation to be added separately, without changing Node class! Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 7

Nodes accept visitors Node accept(nodevisitor) program v.visitassignment(this) VariableRefNode accept(nodevisitor) AssignmentNode accept(nodevisitor v) Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 8

Visitor for Nodes NodeVisitor visitassignment(node); visitvariableref(node); VariableRefNode visitassignment(node); visitvariableref(node); AssignmentNode visitassignment(node) visitvariableref(node) Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 9

Visitor Pattern Define two class hierarchies one for elements being operated on (nodes) one for visitors that define operations on the elements create new operations by adding a new subclass to Visitor class hierarchy Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 10

Structure of Visitor Pattern Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 11

Collaborations Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 12

Circularity inherent in Visitor pattern: Visitor needs elements & elements need Visitor emanates from double dispatch In Java, it s much easier, don t have to worry about forward references, or quick compiles C++ strives for efficiency, at a cost in programmer expertise Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 13

Single Dispatching Two criteria determine the operation: name of the request type of the receiver Example: calling node->generatecode() on an assignment Node will dynamically bind to the generatecode() function in AssignmentNode Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 14

Double Dispatch operation that gets executed depends on kind of request types of two receivers example: accept() is double dispatched operation. Its meaning depends on two types: Visitor s and Element s Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 15

More implementation issues A visitor must visit each element of the structure Who is responsible for traversing the structure? object structure -- most often an iterator the visitor -- problem: you will likely dupe traversal code in each concrete visitor Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 16

Accumulating State Visitors can accumulate state as they traverse without a visitor, this state information might have to be passed during traversal Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 17

Consequences Easy to add new operations Visitor gathers related operations and separates unrelated operations Adding new ConcreteElement classes takes extra work -- need new abstract operation on Visitor Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 18