Unformatted text preview:

Lecture 26: Design Patterns(part 2)Kenneth M. AndersonObject-Oriented Analysis and DesignCSCI 6448 - Spring Semester, 2003April 10, 2003 © Univeristy of Colorado, 2003 2Last Lecture Design Patterns Background and Core Concepts Examples Singleton, Factory Method, and AdapterApril 10, 2003 © Univeristy of Colorado, 2003 3Goals of Lecture Cover Additional Design Patterns State Iterator Flyweight Decorator Observer CompositeApril 10, 2003 © Univeristy of Colorado, 2003 4State Intent Allow an object to alter its behavior whenits internal state changes Motivation TCPConnection example A TCPConnection class must respond toan open operation differently based on itscurrent state: established, closed, listening,etc.April 10, 2003 © Univeristy of Colorado, 2003 5State, continued Applicability Use State when an object’s behavior depends on its state operations have large, multipart conditional statements that depend onthe object’s state Participants 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 aparticular state of the Context ConcreteState each subclass of State implements a different behavior that implementsthe correct behavior for a particular stateApril 10, 2003 © Univeristy of Colorado, 2003 6State, continued Structure Page 306 of Design Patterns Collaborations Context delegates state-specific requests to thecurrent ConcreteState object A context may pass itself as an argument to theState object handling the request Context is the primary interface of clients Either Context or ConcreteState subclasses candecide which state succeeds another and underwhat circumstancesApril 10, 2003 © Univeristy of Colorado, 2003 7State, continued Consequences State localizes state-specific behavior andpartitions behavior for different states State makes state transitions explicit State objects can be shared Example We saw an example of the state patternback in Lecture 20April 10, 2003 © Univeristy of Colorado, 2003 8Iterator Intent Provide a way to access the elements of anaggregate object (e.g. a collection class)sequentially without exposing its underlyingrepresentation Also Known As Cursor Motivation A collection may have multiple ways of being“traversed”; Iterator lets you keep traversaloperations out of the core collection interfaceApril 10, 2003 © Univeristy of Colorado, 2003 9Iterator, continued Applicability Use the Iterator pattern to access an aggregate object’s contents without exposing its internalrepresentation to support multiple traversals of aggregate objects to provide a uniform interface for traversing different aggregate structures (that is,to support polymorphic iteration) Participants 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 methodApril 10, 2003 © Univeristy of Colorado, 2003 10Iterator, continued Structure page 259 of Design Patterns Collaborations A ConcreteIterator keeps track of the current object in theaggregate and can compute the next object in the traversal Consequences The Iterator pattern supports multiple traversals for eachcollection (e.g. inorder, preorder, postorder for trees) Iterators simplify Aggregate interface More than one traversal can occur on a single collection atonce; as long as the traversal is read-onlyApril 10, 2003 © Univeristy of Colorado, 2003 11Iterator, continued Implementation The Iterator interface in the Java Collectionclasses java.util.Iterator (interface) java.util.List (interface) java.util.LinkedList (class) java.util.ListIterator (interface) implementing subclass is private within List classApril 10, 2003 © Univeristy of Colorado, 2003 12Flyweight Intent Use sharing to support large numbers of fine-grained objects efficiently Motivation Imagine a text editor that creates one object percharacter 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 beingreferenced by all of the individual character objectsApril 10, 2003 © Univeristy of Colorado, 2003 13Flyweight, continued Applicability 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 ofobjects Most object state can be made extrinsic Many groups of objects may be replaced by relatively fewshared objects once extrinsic state is removed The application does not depend on object identity. Sinceflyweight objects may be shared, identity tests will returntrue for conceptually distinct objectsApril 10, 2003 © Univeristy of Colorado, 2003 14Flyweight, continued Participants Flyweight declares an interface through which flyweights can receive andact on extrinsic state ConcreteFlyweight implements Flyweight interface and adds storage for intrinsicstate UnsharedConcreteFlyweight not all flyweights need to be shared; unshared flyweightstypically have children which are flyweights FlyweightFactory creates and manages flyweight objects Client maintains extrinsic state and stores references to flyweightsApril 10, 2003 © Univeristy of Colorado, 2003 15Flyweight, continued Collaborations Data that a flyweight needs to process must beclassified as intrinsic or extrinsic Intrinsic is stored with client; Extrinsic is stored with client Clients should not instantiate ConcreteFlyweightsdirectly Consequences Storage savings is a tradeoff between totalreduction in number of objects verses the amountof intrinsic state per flyweight and whether or notextrinsic state is computed or stored greatest savings occur when extrinsic state is computedApril 10, 2003 © Univeristy of Colorado, 2003 16Flyweight, continued See code example (available from classwebsite) Simple implementation of flyweightpattern Focus is on factory and flyweight ratherthan on client Demonstrates how to do simple sharing


View Full Document

CU-Boulder CSCI 6448 - Design Patterns

Documents in this Course
Struts

Struts

12 pages

Adapter

Adapter

23 pages

Prototype

Prototype

16 pages

Weka

Weka

15 pages

qooxdoo

qooxdoo

16 pages

Django

Django

12 pages

Overview

Overview

22 pages

XNA

XNA

5 pages

Load more
Download Design Patterns
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Design Patterns and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Design Patterns 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?