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

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

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

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

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

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

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

Object-Oriented Oriented Programming

Design Pattern: Composite

Information systems modelling UML and service description languages

SDC Design patterns GoF

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

Topics in Object-Oriented Design Patterns

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

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

COSC 3351 Software Design. Design Patterns Structural Patterns (II) Edgar Gabriel. Spring Decorator

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

Design Patterns IV. Alexei Khorev. 1 Structural Patterns. Structural Patterns. 2 Adapter Design Patterns IV. Alexei Khorev. Structural Patterns

Design Patterns IV Structural Design Patterns, 1

» Access elements of a container sequentially without exposing the underlying representation

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

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

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

The 'Gang of Four' Companion

Composite Pattern. IV.4 Structural Pattern

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

Design Patterns Reid Holmes

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

Using Design Patterns in Java Application Development

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

CS342: Software Design. November 21, 2017

SOFTWARE PATTERNS. Joseph Bonello

Design for change. You should avoid

Laboratorio di Sistemi Software Design Patterns 2

LOG6306 : Études empiriques sur les patrons logiciels

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

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

Design Patterns. Decorator. Oliver Haase

Design Patterns. Lecture 10: OOP, autumn 2003

What are patterns? Design Patterns. Design patterns. Creational patterns. The factory pattern. Factory pattern structure. Lecture 10: OOP, autumn 2003

MORE DESIGN PATTERNS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 24 11/10/2011

David Talby March 21, 2006

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

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

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

Design Patterns. GoF design patterns catalog

An Introduction to Patterns

Dr. Xiaolin Hu. Review of last class

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

CS 2340 Objects and Design

Behavioral patterns. Command Iterator Observer Strategy Template method

Behavioral Design Patterns Used in Data Structures Implementation

Software Eningeering. Lecture 9 Design Patterns 2

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

Design Patterns Lecture 2

DESIGN PATTERNS Course 4

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

Design Patterns. Vasileios Theodorou

A DESIGN PATTERNS PERSPECTIVE ON DATA STRUCTURES. Virginia Niculescu

The Composite Design Pattern

More on Inheritance. Interfaces & Abstract Classes

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

Tuesday, October 4. Announcements

CS 2720 Practical Software Development University of Lethbridge. Design Patterns

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

Brief Note on Design Pattern

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

CS 349 / SE 382 Design Patterns. Professor Michael Terry January 21, 2009

Lecture 13: Design Patterns

Design patterns generic models

Design Patterns. David Talby

Design Patterns in C++

Design Patterns Design patterns advantages:

Design Patterns. State. Oliver Haase

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

Introduction to Software Engineering (2+1 SWS) Winter Term 2009 / 2010 Dr. Michael Eichberg Vertretungsprofessur Software Engineering Department of


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

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

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

Tree Structures. A hierarchical data structure whose point of entry is the root node

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

DESIGN PATTERN - INTERVIEW QUESTIONS

Adapter pattern. Acknowledgement: Freeman & Freeman

The Factory Method Pattern

2.1 Design Patterns and Architecture (continued)

COURSE 4 DESIGN PATTERNS

2.1 Design Patterns and Architecture (continued)

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

EMBEDDED SYSTEMS PROGRAMMING Design Patterns

Object-Oriented Oriented Programming

The Design Patterns Matrix From Analysis to Implementation

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

Object- Oriented Design with UML and Java Part I: Fundamentals

Object-Oriented Design

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

Object-oriented Software Design Patterns

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

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

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

Principles of Software Construction: Objects, Design, and Concurrency. Assigning Responsibilities to Objects. toad. Jonathan Aldrich Charlie Garrod

Transcription:

Lecture 26: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 6448 - Spring Semester, 2003 Last Lecture Design Patterns Background and Core Concepts Examples Singleton, Factory Method, and Adapter April 10, 2003 Univeristy of Colorado, 2003 2 Goals of Lecture Cover Additional Design Patterns State Iterator Flyweight Decorator Observer Composite State Allow an object to alter its behavior when its internal state changes TCPConnection example A TCPConnection class must respond to an open operation differently based on its current state: established, closed, listening, etc. April 10, 2003 Univeristy of Colorado, 2003 3 April 10, 2003 Univeristy of Colorado, 2003 4

State, continued Use State when an object s behavior depends on its state operations have large, multipart conditional statements that depend on the object s state Context defines the interface of interest to clients maintains an instance of a ConcreteState subclass State defines an interface for encapsulating the behavior associated with a particular state of the Context ConcreteState each subclass of State implements a different behavior that implements the correct behavior for a particular state April 10, 2003 Univeristy of Colorado, 2003 5 State, continued Page 306 of Design Patterns Context delegates state-specific requests to the current ConcreteState object A context may pass itself as an argument to the State object handling the request Context is the primary interface of clients Either Context or ConcreteState subclasses can decide which state succeeds another and under what circumstances April 10, 2003 Univeristy of Colorado, 2003 6 State, continued State localizes state-specific behavior and partitions behavior for different states State makes state transitions explicit State objects can be shared Example We saw an example of the state pattern back in Lecture 20 April 10, 2003 Univeristy of Colorado, 2003 7 Iterator Provide a way to access the elements of an aggregate object (e.g. a collection class) sequentially without exposing its underlying representation Also Known As Cursor A collection may have multiple ways of being traversed ; Iterator lets you keep traversal operations out of the core collection interface April 10, 2003 Univeristy of Colorado, 2003 8

Iterator, continued Use the Iterator pattern to access an aggregate object s contents without exposing its internal representation to support multiple traversals of aggregate objects to provide a uniform interface for traversing different aggregate structures (that is, to support polymorphic iteration) Iterator defines an interface for accessing and traversing elements ConcreteIterator implements Iterator interface and keeps track of current position within collection Aggregate defines an interface for creating an Iterator (factory method) ConcreteAggregate implements the factory method Iterator, continued page 259 of Design Patterns A ConcreteIterator keeps track of the current object in the aggregate and can compute the next object in the traversal The Iterator pattern supports multiple traversals for each collection (e.g. inorder, preorder, postorder for trees) Iterators simplify Aggregate interface More than one traversal can occur on a single collection at once; as long as the traversal is read-only April 10, 2003 Univeristy of Colorado, 2003 9 April 10, 2003 Univeristy of Colorado, 2003 10 Iterator, continued Implementation The Iterator interface in the Java Collection classes java.util.iterator (interface) java.util.list (interface) java.util.linkedlist (class) java.util.listiterator (interface) implementing subclass is private within List class Flyweight Use sharing to support large numbers of finegrained objects efficiently Imagine a text editor that creates one object per character in a document For large documents, that is a lot of objects! but for simple text documents, there are only 26 letters, 10 digits, and a handful of punctuation marks being referenced by all of the individual character objects April 10, 2003 Univeristy of Colorado, 2003 11 April 10, 2003 Univeristy of Colorado, 2003 12

Flyweight, continued Use flyweight when all of the following are true An application uses a large number of objects Storage costs are high because of the sheer quantity of objects Most object state can be made extrinsic Many groups of objects may be replaced by relatively few shared objects once extrinsic state is removed The application does not depend on object identity. Since flyweight objects may be shared, identity tests will return true for conceptually distinct objects April 10, 2003 Univeristy of Colorado, 2003 13 Flyweight, continued Flyweight declares an interface through which flyweights can receive and act on extrinsic state ConcreteFlyweight implements Flyweight interface and adds storage for intrinsic state UnsharedConcreteFlyweight not all flyweights need to be shared; unshared flyweights typically have children which are flyweights FlyweightFactory creates and manages flyweight objects Client maintains extrinsic state and stores references to flyweights April 10, 2003 Univeristy of Colorado, 2003 14 Flyweight, continued Data that a flyweight needs to process must be classified as intrinsic or extrinsic Intrinsic is stored with client; Extrinsic is stored with client Clients should not instantiate ConcreteFlyweights directly Storage savings is a tradeoff between total reduction in number of objects verses the amount of intrinsic state per flyweight and whether or not extrinsic state is computed or stored greatest savings occur when extrinsic state is computed April 10, 2003 Univeristy of Colorado, 2003 15 Flyweight, continued See code example (available from class website) Simple implementation of flyweight pattern Focus is on factory and flyweight rather than on client Demonstrates how to do simple sharing of characters April 10, 2003 Univeristy of Colorado, 2003 16

Decorator Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality Also Known As Wrapper Sometimes we want to add responsibilities to individual objects, not to an entire class (like adding scrollbars to windows in GUI toolkits) April 10, 2003 Univeristy of Colorado, 2003 17 Decorator, continued Use Decorator to add responsibilities to individual objects dynamically for responsibilities that can be withdrawn when extension by subclassing is impractical Component defines interface of objects to decorate ConcreteComponent defines an object to decorate Decorator and ConcreteDecorator Decorator maintains a reference to component and defines an interface that conforms to Component s interface; ConcreteDecorator adds responsibilities to the component April 10, 2003 Univeristy of Colorado, 2003 18 Decorator, continued Page 177 of Design Patterns Decorator forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request More flexibility than static inheritance Avoids feature-laden classes high up in the hierarchy A decorator and its component are not identical Lots of little objects April 10, 2003 Univeristy of Colorado, 2003 19 Observer Define a one-to-many dependency between objects so that when one object changes states, all its dependents are notified and updated automatically Also Known As Dependants, Publish-Subscribe Need a way to update dependant objects while avoiding tight coupling User Interface Example April 10, 2003 Univeristy of Colorado, 2003 20

Observer, continued Use Observer when an abstraction has two aspects, one dependent on the other when a change to one object requires changing others and you don t know in advance who needs to change when an object should notify objects but should not make assumptions about which objects need to be notified Subject provides interface to add and delete observers Observer defines an updating interface for dependants ConcreteSubject stores the state being observed ConcreteObserver stores state that must be consistent with observed state April 10, 2003 Univeristy of Colorado, 2003 21 Observer, continued page 294 of Design Patterns ConcreteSubject notifies observers whenever it changes its observed state After receiving a notification, ConcreteObserver gets state from ConcreteSubject see sequence diagram on page 295 of Design Patterns April 10, 2003 Univeristy of Colorado, 2003 22 Observer, continued Abstract coupling between Subject and Observer Subjects do not know the concrete subclasses of their observers Support for broadcast communication Subject does not know who is listening Unexpected updates Change in state may update an unintended object, one we didn t suspect was an observer, or should only be observing at well-defined times Composite Compose objects into tree structures to represent part-whole hierarchies Composite lets clients treat individual objects and compositions of objects uniformly Image programs that allow graphic primitives to be grouped into collections of objects Many operations are shared, such as move(), copy(), paste(), draw(), etc. April 10, 2003 Univeristy of Colorado, 2003 23 April 10, 2003 Univeristy of Colorado, 2003 24

Composite, continued Use Composite when you want to represent part-whole hierarchies you want clients to be able to ignore the difference between compositions of objects and individual objects page 164 of Design Patterns April 10, 2003 Univeristy of Colorado, 2003 25 Composite, continued Component declares the shared interface declares child management operations empty methods for leaves defines an interface to retrieve parent Leaf implements shared interface Composite stores children implements shared interface by delegating to children implements child management operations Client Manipulates objects using the Component interface April 10, 2003 Univeristy of Colorado, 2003 26 Composite, continued Client uses the Component interface to interact with all objects If the recipient is a leaf, then the request is handled directly If the recipient is a composite, then the request is delegated to its children Composite, continued Composite allows primitive objects and composite objects to be treated transparently especially since the child management functions are defined in the Component interface Composite simplifies code in the client It makes it easy to add new types of leaves nothing needs to change to add a new type of component (not even the client) Disadvantage: Difficult to create composites that have only certain types of leaves; you need to subclass the Composite class and use run-time checks to make sure that only legal children are added to it April 10, 2003 Univeristy of Colorado, 2003 27 April 10, 2003 Univeristy of Colorado, 2003 28