Design Patterns (II)

Similar documents
COSC 3351 Software Design. Design Patterns Behavioral Patterns (I)

Brief Note on Design Pattern

Dr. Xiaolin Hu. Review of last class

Object-Oriented Oriented Programming Command Pattern. CSIE Department, NTUT Woei-Kae Chen

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

CS 2720 Practical Software Development University of Lethbridge. Design Patterns

Object-Oriented Oriented Programming

Design patterns Behavioral Pattern 2015

SDC Design patterns GoF

COSC 3351 Software Design. Design Patterns Behavioral Patterns (II)

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

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

Design Patterns Reid Holmes

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

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

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

Design for change. You should avoid

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

Appendix-A. A.1 Catalogues of Design Patterns. Below is the definition for each design pattern using the FINDER notation, followed

Software Eningeering. Lecture 9 Design Patterns 2

Design Patterns Lecture 2

Egon Borger (Pisa) Capturing Design Pattern Abstractions by ASMs

CS560. Lecture: Design Patterns II Includes slides by E. Gamma et al., 1995

Object-Oriented Design

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

Object-Oriented Oriented Programming

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

Topics in Object-Oriented Design Patterns

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

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

State Pattern. CSIE Department, NTUT Woei-Kae Chen. State: Intent. Allow an object to alter its behavior when its internal state changes.

» Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request!

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

Information systems modelling UML and service description languages

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

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

Command Pattern. CS356 Object-Oriented Design and Programming November 13, 2014

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

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

6.3 Patterns. Definition: Design Patterns

Interpreter Pattern Behavioural!

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

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

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Design Patterns. GoF design patterns catalog

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

Design Patterns. State. Oliver Haase

Design patterns. Jef De Smedt Beta VZW

Chapter 8, Design Patterns Visitor

Design Patterns Reid Holmes

Object Oriented Paradigm

Introduction to Software Engineering: Object Design I Reuse & Patterns

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

Design patterns generic models

BEHAVIORAL DESIGN PATTERNS

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

EECS 4314 Advanced Software Engineering. Topic 04: Design Pattern Review Zhen Ming (Jack) Jiang

EMBEDDED SYSTEMS PROGRAMMING Design Patterns

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

Simple Patterns for Extensibility

EMBEDDED SYSTEMS PROGRAMMING Design Patterns

DESIGN PATTERN - INTERVIEW QUESTIONS

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

Object Oriented. Analysis and Design

INSTITUTE OF AERONAUTICAL ENGINEERING

UNIT-I. Introduction, Architectural Styles, Shared Information Systems

Using Design Patterns in Java Application Development

The Factory Method Pattern

Design of Software Systems (Ontwerp van SoftwareSystemen) Design Patterns Reference. Roel Wuyts

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

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

Abstract Factory. Consequences section. UML structure. Applicability section Requirement Elaboration of requirement Achieved through

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

CSCI Object Oriented Design: Frameworks and Design Patterns George Blankenship. Frameworks and Design George Blankenship 1

! Statechart diagrams contain. ! States. ! Transitions. ! States : A,B,C, A is initial state. ! Labels : 0,1. ! Transitions:

Design Pattern and Software Architecture: IV. Design Pattern

Design Patterns. CSE870: Advanced Software Engineering (Design Patterns): Cheng

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

DESIGN PATTERNS SURESH BABU M ASST PROFESSOR VJIT

Design Patterns! Acknowledgements!

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

Implementing GUI context-sensitive help... ECE450 Software Engineering II. Implementing GUI context-sensitive help... Context-sensitive help

CSE870: Advanced Software Engineering (Cheng) 1

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

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

An Introduction to Patterns

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

Design Patterns. (and anti-patterns)

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

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

Composite Pattern. IV.4 Structural Pattern

Design Patterns. An introduction

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

Why Design Patterns? OO Design Patterns. Design Patterns, Elements of Reusable Object-Oriented Software by Gamma, Helm, Johnson, & Vlissides

UNIT I Introduction to Design Patterns

Development and Implementation of Workshop Management System Application to Explore Combing Multiple Design Patterns

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

UNIT I Introduction to Design Patterns

LECTURE NOTES ON DESIGN PATTERNS MCA III YEAR, V SEMESTER (JNTUA-R09)

Object-oriented Software Design Patterns

Transcription:

Design Patterns (II) Design Pattern Space Defer object creation to another class Scope Class Object Defer object creation to another object Creational Factory Method Abstract Factory Builder Prototype Singleton Describe ways to assemble objects Purpose Structural Adapter (class) Adapter (object) Bridge Composite Decorator Facade Flyweight Proxy Describe algorithms and flow control Behavioral Interpreter Template Method Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor

Behavioral Patterns Concerned with algorithms and the assignment of responsibilities between objects Describe communication flows among objects Behavioral class patterns Use inheritance to distribute behavior among classes Behavioral object patterns Use object composition rather than inheritance Describe how groups of peer objects cooperate for a task Patterns on how peer objects know each other Intent: CHAIN OF RESPONSIBILITY (Object Behavioral) Avoid coupling the sender of a request to its receiver Give more than one object a chance to handle a request Chain the receiving objects Pass the request along until an Object handles it : Example: Context-sensitive help facility for a GUI Users can obtain help info on any widget Help provided depends on the chosen widget and its context Object that provides help is not directly known to object (e.g. button) that initiates the request Decouple senders and receivers of requests

CHAIN OF RESPONSIBILITY asavedialog aprintbutton handler handler aprintdialog anapplication handler anokbutton handler handler specific general CHAIN OF RESPONSIBILITY Object in the chain receives the request aprintbutton aprintdialog anapplicaton Handles it or forwards it HandleHelp() Requestor has implicit receiver for the request Final receiver handles or ignores the request HandleHelp()

CHAIN OF RESPONSIBILITY handler HelpHandler HandleHelp() handler - >HandleHelp() HelpHandler forwards requests by default Subclasses can override operation Application Dialog Widget Button HandleHelp() ShowHelp() if can handle { ShowHelp() } else { Handler :: HandleHelp() } Requests are fulfilled in the subclass, or handled by the default implementation CHAIN OF RESPONSIBILITY Structure Client Handler HandleRequest() successor ConcreteHandler1 HandleRequest() ConcreteHandler2 HandleRequest() aclient ahandler aconcretehandler1 successor aconcretehandler2 successor

CHAIN OF RESPONSIBILITY Participants Handler (HelpHandler) Defines an interface for handling requests (optional) implements the successor link ConcreteHandler (PrintButton, PrintDialog) Handles requests it is responsible for Either handles requests or forwards it to its successor, usually through the Handler Client Initiates the request to a ConcreteHandler object on the chain CHAIN OF RESPONSIBILITY Applicability / Benefits Use Chain of Responsibility when: More than one object may handle a request The handler is not known a priori The handler should be identified automatically You don t want specify the receiver explicitly The handler objects are specified dynamically Benefits: Reduced coupling Sender and receiver have no explicit knowledge of each other Single reference to successor Flexible assignment of object responsibilities

COMMAND (Object Behavioral) Intent: Encapsulate a request as an object Parameterize clients with different requests (queue or log requests) Support undoable operations (Transactions) Decouple requesting object from performing object : Decouple GUI toolkit request from Operation being requested Receiver of the request Abstract command class interface for executing operations Requestor does not know which Command subclass is used Concrete Command subclass specifies receiver-action pair Final receiver as instance variable COMMAND Application Add(Document) Menu Add(MenuItem) MenuItem Clicked() command Command Execute() Document Open() Close() Cut() Copy() Paste() command ->Execute() Instance variable stores receiver of an event Each possible choice in a Menu is an instance of a MenuItem class Application creates menus and their menu items

COMMAND Command Execute() Document Open() Close() Cut() Copy() Paste() PasteCommand Execute() document >Paste() PasteCommand supports pasting text from the clipboard into a document. PasteCommand s receiver is the Document object given at instantiation. The Execute operation invokes Paste() on the receiving document. COMMAND Command Execute() Application Add(Document) application OpenCommand Execute() AskUser() OpenCommand s operation Execute prompts the user for a name creates the corresponding document object adds document to the receiving app opens the document name = AskUser() doc = new Document(name) application -> Add(doc) doc -> Open()

COMMAND Command Execute() MacroCommand command Execute() for all c in commands c -> Execute() MacroCommand is a concrete Command subclass Executes a sequence of commands MacroCommand has no explicit receiver Command objects in the sequence define their own receivers. COMMAND Structure Client Invoker Command Execute() Receiver Action() receiver ConcreteCommand Execute() receiver -> Action(); state

COMMAND Participants Command Declares interface for operation execution ConcreteCommand (PasteCommand, OpenCommand) Defines binding between Receiver and an action Implements operation execution by invoking Receiver Client (Application) Creates a ConcreteCommand object and sets the Receiver Invoker (MenuItem) Asks the Command to carry out the request (stores ConcreteCommand) Receiver (Document, Application) Knows how to perform the operation(s) for a request COMMAND Interaction Between Objects areceiver aclient acommand aninvoker new Command(aReceiver) StoreCommand(aCommand) Action() Execute()

COMMAND Applicability Use the Command pattern when you want to: Parameterize objects by an action to perform Commands = OO replacement for callback function registration Decouple request specification and execution Command object s lifetime is independent from original request Command object might be transferred to another process Implement undo operation Command object maintains state information for reversing its effects Additional Unexecute() operation Saving / loading operations for state allows crash fault tolerance Model transactional behavior Encapsulation of set of data changes Intent: INTERPRETER (Class Behavioral) Define representation of language through its grammar Build something that uses the representation to interpret sentences : Interpreter for problems represented through language sentences Example: Regular expressions Implementation of search algorithms uses given pattern language Example grammar expression ::= literal alternation sequence repetition ( expression ) alternation ::= expression expression sequence ::= expression & expression Repetition ::= expression *

INTERPRETER RegularExpression Interpret() LiteralExpression Interpret() SequenceExpression Interpret() expression 1 expression 2 literal repetition RegularExpression Interpret() AlternationExpression Interpret() alternative 1 alternative 2 INTERPRETER asequenceexpression expression1 expression 2 arepetitionexpression aliteralexpression raining repeat analternationexpression alternation 1 alternation 2 aliteralexpression aliteralexpression dogs cats Raining & (dogs cats) *

INTERPRETER Structure Context Client AbstractExpression Interpret(Context) TerminalExpression NoterminalExpression Interpret(Context) Interpret(Context) INTERPRETER Participants AbstractExpression (RegularExpression) Declares abstract Interpret operation TerminalExpression (LiteralExpression) Implements Interpret operation according to symbol NonterminalExpression (AlternationExpression, ) One class for each rule in the grammar Holds instances of AbstractExpression for each symbol in it Implements Interpret operation, mostly through recursion on the AbstractExpression instances Context Global interpreter information, manipulated by Interpret implementations Initialized by Client Client Builds representation of sentence through NonterminalExpression and TerminalExpression classes Invokes Interpret operation of root symbol

INTERPRETER Applicability Use the Interpreter pattern when: You have abstract syntax trees there is a language to interpret statements are representable as AST The grammar is simple Large and unmanageable class hierarchy in complex cases (use parser generators) Efficiency is not a critical concern most efficient interpreters first translating parse trees into another form Example: Regular expressions! state machines Translator itself could be an interpreter Intent: ITERATOR (Object Behavioral) Access elements of an aggregate object sequentially Don t expose underlying representation : Traversal of aggregate list object Allow multiple pending traversals Separate traversal operations from list interface Solution: Take responsibility for access and traversal out of the list interface Iterator class for list element access Current element is managed in the Iterator implementation Decouple aggregate class from client

ITERATOR List list ListIterator Count() Append(Element) Remove(Element)... First() Next() IsDone() CurrentItem() index ITERATOR Polymorphic Iteration AbstractList client Iterator CreateIterator() Count() Append(Item) Remove(Item... First() Next() IsDone() CurrentItem() List ListIterator SkipList SkipListIterator Iteration mechanism independent of concrete aggregate class

ITERATOR Structure Aggregate client Iterator CreateIterator() First() Next() IsDone() CurrentItem() ConcreteAggregate ConcreteIterator CreateIterator() return new ConcreteIterator (this) ITERATOR Applicability / Benefits Use the Iterator pattern to access aggregate content No exposing of internal representation Support for multiple traversals Uniform traversal interface for different aggregates Benefits: Support for variation in the traversal of an aggregate e.g. parse order New traversals through Iterator sublasses Simplification of Aggregate interface Each iterator keeps track of it s own traversal state

Intent: MEDIATOR (Object Behavioral) Define object which encapsulates interaction of objects Keep objects from referring to each other, allow variation of interaction : OO-design might lead to structure with many connections Example: Implementation of dialog box Window with widgets Most widgets depend on each other New dialogs with same widgets have different behavior Define control and coordination intermediary! director Hub of communication for widgets Every widget only need to know the director object MEDIATOR The quick brown fox... Family New cetury schoolbook Avant garde chicago courier helvetica palatino times roman zapf dingbats Weight Slant medium bold demibold roma italic oblique Size 34pt condensed

MEDIATOR aclient director alistbox director afontdialogdirector abutton director aentryfield director MEDIATOR aclient Mediator afontdialogdirector alistbox Colleagues anentryfield ShowDialog() WidgetChanged() GetSelection() SetText()

MEDIATOR DialogDirector director Widget ShowDialo() CreateWidgets() WidgetChanged(Widget) Changed() director ->WidgetChanged(this) ListBox EntryField FontDialogDirector list GetSelection() SetText() CreateWidgets() WidgetChanged(Widget) field MEDIATOR Structure Mediator mediator Colleague ConcreteMediator ConcreteColleague1 ConcreteColleague2

MEDIATOR Typical Object Structure acolleague acolleague mediator mediator acolleague aconcretemediator mediator acolleague mediator acolleague mediator MEDIATOR Applicability / Benefits Use the Mediator pattern when: Multiple objects communicate in a complex way have unstructured / difficult dependencies prevent reuse of single objects through the tight interdependencies should be easily configurable with another behavior Benefits: Limits subclassing in case of behavior change Decouples colleague objects Simplifies object protocols (one-to-many vs. many-to-many) Abstraction of object cooperation Provides centralized control

Intent: MEMENTO (Object Behavioral) Capture and externalize an object s internal state Provide capability to restore object later Keep encapsulation principle : Save state information for later restore Checkpointing mechanisms Undo mechanisms Memento object Storage for state snapshot of another (originator) object Read / written on request by originator object Opaque to other objects MEMENTO Structure Originator SetMemento(Memento m) CreateMemento() state Memento GetState() SetState() state memento Caretaker return new Memento (state) state = m -> GetState() Caretaker requests Memento from Originator holds it for a time passes it (eventually) back to the originator Only the Originator of a Memento can assign / retrieve its state

MEMENTO Participants Memento Stores internal state of the Originator object Protects against access by objects other than the Originator Narrow interface for Caretaker Wide interface for Originator In best case, only one Originator has access to the state data Originator Creates Memento containing current state snapshot Uses Memento to restore internal state Caretaker Responsible for Memento s safekeeping Never operates / examines content of a Memento MEMENTO Applicability Use the Memento pattern when: Snapshot of object state is needed Later restore Direct interface would break encapsulation Exposing of implementation details Benefits: Preserves encapsulation boundaries Shields other objects from potentially complex Originator internals Simplifies Originator Storage management handled externally

Intent: OBSERVER (Object Behavioral) Define one-to-many dependency between objects Notification of dependent objects about state change : Need to maintain consistency between related objects Example: GUI toolkit Separate presentation aspects from application data Different visualization of same data No dependency between visualization objects, but all update on data change Subject and it s dependent Observers Publish-subscribe interaction OBSERVER observers a b c x 60 30 10 y 50 30 20 a b c z 80 10 10 a b c a = 50% b = 30% c = 20% change notification state request subject

OBSERVER Structure Subject observers Observers Attach(Observer) Detach(Observer) Notify() for all o in observers{ o -> Update() } Update() ConcreteObserver ConcreteSubject GetState() SetState() return subjectstate subject Update() observerstate observerstate= subject -> GetState() subjectstate Subject: OBSERVER Participants Knows its Observers Provides interface for attaching/detaching Observer objects Observer: Defines an Update interface, for changes in the subject ConcreteSubject: Stores state, which is of interest to ConcreteObserver objects Sends a notification to its observers when its state changes ConcreteObserver: Maintains a reference to a ConcreteSubject object Own state, consistent with Subject state through Update interface

OBSERVER Collaborations ConcreteSubject notifies its observers whenever the observer s state becomes invalid ConcreteObserver object may query the Subject for information in case of notification Notification might be triggered by another Observer OBSERVER Applicability Use the Observer pattern when: Abstraction has two dependent aspects Dependencies on object state change are unclear Need notification without knowledge about Observers Benefits: Abstract coupling between Subject and Observers Support for broadcast communication

Intent: STATE (Object Behavioral) Allow object to alter its behavior, depending on internal state change Object appears to change its class : Example: Class TCPConnection Represent possible states of network connection as objects Abstract base class TCPState Subclasses implement state-specific behavior TCPConnection maintains a state object State-specific requests are handled directly by the according state object STATE TCPConnection state TCPState Open() Close() Achnowledge() Open() Close() Achnowledge() state -> Open() TCPEstablished TCPListen TCPClosed Open() Close() Achnowledge() Open() Close() Achnowledge() Open() Close() Achnowledge()

STATE Structure Context state State Request() Handle() state -> Handle() ConcreteStateA ConcreteStateB Handle() Handle() STATE Collaborations Context delegates state-specific requests to current ConcreteState object Context may pass itself as an argument to the State object Context is the primary interface for clients Clients can configure a Context with State objects Once configured, Clients don t have to deal any longer directly with State objects

STATE Applicability Use the State pattern when: Object s behavior depends on its state Object must change behavior at runtime, reasoned by state information Benefits: Localize state-specific behavior Makes state transitions explicit State objects can be shared Intent: STRATEGY (Object Behavioral) Define family of algorithms Encapsulate each one, make them interchangeable Vary algorithm independent from clients : Example: Break composed text stream into lines Simple strategy (AsciiParser) Paragraph optimization (TexParser) Array composition (fixed number of columns) Don t integrate different algorithms directly in client Solution: Definition of encapsulating classes, algorithm is a Strategy

STRATEGY Composition compositor Composition Traverse() Repair() Compose() Compositor -> Compose() SimpleCompositor TeXCompositor ArrayCompositor Compose() Compose() Compose() STRATEGY Structure Context ContextInterface() strategy Strategy AlgorithmInterface() ConcreteStrategyA ConcreteStrategyB ConcreteStrategyC AlgorighmInterface() AlgorighmInterface() AlgorighmInterface()

STRATEGY Applicability Use the Strategy pattern when Related classes differ only in their behaviour You need different variants of an algorithm - Should be implementable as hierarchy To avoid exposing algorithm-specific data structures To replace conditional statements for behaviour - Move conditional branches to according Strategy classes TEMPLATE METHOD (Class Behavioral) Intent: Define algorithm skeleton in an operation Subclass might redefine steps of the algorithm : Example: Framework with Application / Document classes Applications can subclass for specific needs Common algorithm for opening a document Check of the document can be opened " app.-specific Create app-specific Document object " app.-specific Add new Document to the set of documents Read document data from a file " doc.-specific Solution: Define some of the algorithm steps using abstract operations

TEMPLATE METHOD Document Save() Open() Close() DoRead() docs Application AddDocument() OpenDocumenr() DoCreateDocument() CanOpenDocument() AboutToOpenDocument() MyDocument DoRead() MyApplication DoCreateDocument() CanOpenDocument() AboutToOpenDocument() return new MyDocument TEMPLATE METHOD Structure AbstractClass TemplateMethod() PrimitiveOperation1() PrimitiveOperation2()... PrimitiveOperation1()... PrimitiveOperation2()... ConcreteClass PrimitiveOperation1() PrimitiveOperation () Hollywood Principle : Don t call us, we ll call you

TEMPLATE METHOD Applicability The Template Method pattern should be used To implement the invariant parts of an algorithm once To leave it up to subclasses to implement varying behaviour To avoid code duplication - Centralize common behaviour of subclasses - First identify the differences in the existing code - Then separate the differences into new operation - Replace the differing code with a template method To control subclasses extensions - Template method that calls hook operations - permitting extensions only at those points. Intent: VISITOR (Object Behavioral) Represent operation for object structure elements Define new operations without changing the element classes : Example: Compiler with internal AST code representation Operations on AST (type checking, code optimization, ) Different AST node types (assignment node, variable node, ) Providing operations on each node type is hard New operations Solution: Package related operations in a separate object! Visitor Pass visitor to element objects Node classes become independent of operations applied to them

VISITOR Node TypeCheck() GenerateCode() PrettyPrint() VariableRefNode TypeCheck() GenerateCode() PrettyPrint() AssignmentNode TypeCheck() GenerateCode() PrettyPrint() VISITOR NodeVisitor VisitAssignment(AssignmentNode) VisitVariableRef(VariableRefNode) TypeChecingVisitor CodeGeneratingVisitor VisitAssignment(AssignmentNode) VisitVariableRef(VariableRefNode) VisitAssignment(AssignmentNode) VisitVariableRef(VariableRefNode)

VISITOR Program Node Accept(NodeVisitor) Two class hierarchies: Elements being operated on (Node hierarchy) Visitors with node operations (NodeVisitor hierarchy) Acceptance of visitor through operation call AssignmentNode Accept(NodeVisitor v) VariableRefNode Accept(NodeVisitor v) v -> VisitAssignment(this) v -> VisitAssignment(this) VISITOR Structure (I) client Visitor VisitConcreteElementA(ConcreteElementA) VisitConcreteElementB(ConcreteElementB) ContcreteVisitor1 VisitConcreteElementA(ConcreteElementA) VisitConcreteElementB(ConcreteElementB) ContcreteVisitor2 VisitConcreteElementA(ConcreteElementA) VisitConcreteElementB(ConcreteElementB)

VISITOR Structure (II) ObjectStructure Element Accept(Visitor) ConcreteElementA Accept(Visitor v) OperationA() ConcreteElementB Accept(Visitor v) OperationB() V -> VisitConcreteElementA(this) V -> VisitConcreteElementB(this) VISITOR Collaborations Client must create a ConcreteVisitor object Client traverses the object structure, visiting each element Visited Element calls class corresponding Visitor operation Element supplies itself as an argument to operation Visitor can access state information

VISITOR Applicability Use the Visitor pattern when Need to perform operations on differing objects, depending on concrete classes Many distinct and unrelated operations need to be performed on objects Avoid pollution of object interface Visitor pattern keeps related operations together Only needed application operations with shared object structures Rarely change of element object structure, frequent change of operation set - Changes of object structure classes might require costly visitor changes Design Pattern Space Defer object creation to another class Creational Purpose Structural Behavioral Scope Class Object Defer object creation to another object Factory Method Abstract Factory Builder Prototype Singleton Describe ways to assemble objects Adapter (class) Adapter (object) Bridge Composite Decorator Facade Flyweight Proxy Describe algorithms and flow control Interpreter Template Method Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor

Design Patterns in Smalltalk MVC View character-based View View GUI, Document 1 GUI, Document 2 Controller Model Implements algorithms (business logic) Independent of environment View: Communicates with environment Implements I/O interface for model Controller: Controls data exchange (notification protocol) between model and view Model US $ -> EUR Model EUR -> US $ Model/View/Controller (contd.) MVC decouples views from models more general: Decoupling objects so that changes to one can affect any number of others without requiring the object to know details of the others Observer pattern solves the more general problem MVC allows view to be nested: CompositeView objects act just as View objects Composite pattern describes the more general problem of grouping primitive and composite objects into new objects with identical interfaces MVC controls appearance of view by controller: Example of the more general Strategy pattern MVC uses Factory and Decorator patterns as well