DOC PREVIEW
CU-Boulder CSCI 5448 - Patterns of Patterns

This preview shows page 1-2-3-27-28-29 out of 29 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Patterns of PatternsKenneth M. AndersonUniversity of Colorado, BoulderCSCI 4448/5448 — Lecture 25 — 11/17/2009© University of Colorado, 20091Lecture Goals• Cover Material from Chapter 12 of the Design Patterns Textbook2Patterns of Patterns• Patterns can be• used together in a single system (we’ve seen this several times)• can be combined to create, in essence, a new pattern• Chapter 12 does a good job of showing both of these situations in use• DuckSimulator Revisited: An example that uses six patterns at once• Model View Controller: A pattern that makes use of multiple patterns• We’ll see many examples as we move through this lecture3But first… what pattern is this?regBar(Bar)remBar(Bar)notBars()Foo«Interface»heyYou()Bar«Interface»bars*getSecret()setSecret()secret_dataRealFooRealBarfooRemember that the names of classes participating in a pattern is unimportant; Its the structure (of the relationships and methods) that’s important!4Duck Simulator Revisited• We’ve been asked to build a new Duck Simulator by a Park Ranger interested in tracking various types of water fowl, ducks in particular.• New Requirements• Ducks are the focus, but other water fowl (e.g. Geese) can join in too• Need to keep track of how many times duck’s quack• Control duck creation such that all other requirements are met• Allow ducks to band together into flocks and subflocks• Generate a notification when a duck quacks• Note: to avoid coding to an implementation, replace all instances of the word “duck” above with the word “Quackable”5Opportunities for Patterns• There are several opportunities for adding patterns to this program• New Requirements•Ducks are the focus, but other water fowl (e.g. Geese) can join in too (ADAPTER)•Need to keep track of how many times duck’s quack (DECORATOR)•Control duck creation such that all other requirements are met (FACTORY)•Allow ducks to band together into flocks and subflocks (COMPOSITE and ITERATOR)•Generate a notification when a duck quacks (OBSERVER)• Lets take a look at this example via a class diagram perspective6Step 1: Need an Interfacequack()Quackable«interface»All simulator participants will implement this interface7Step 2: Need ParticipantsInterloper!quack()Quackable«interface»MallardDuck RedheadDuck DuckCall RubberDuckhonk()Goose8Step 3: Need Adapterquack()Quackable«interface»MallardDuck RedheadDuck DuckCall RubberDuckhonk()GooseGooseAdaptergooseAll participants are now Quackables,allowing us to treat them uniformly9Review: (Object) Adapter StructureClientrequest()Target«interface»request()AdapterspecificRequest()Adapteeadaptee.specificRequest()QuackableGooseGooseAdapterquack() { goose.honk() }10Step 4: Use Decorator to Add Quack Countingquack()Quackable«interface»quack()static getQuacks(): intQuackCounterduckPrevious classes/relationships are all still there… just elided for clarityNote: two relationships between QuackCounter and QuackableWhat do they mean?11Review: Decorator StructuremethodA()methodB()ComponentmethodA()methodB()...att1att2ConcreteComponentmethodA()methodB()DecoratormethodA()methodB()...ConcreteDecoratorAmethodA()methodB()...newatt1newatt2ConcreteDecoratorBcomponentQuackableMallardDuckQuackCounterNo need for abstract Decorator interface in this situation; note that QuackCounter follows ConcreteDecorators, as it adds state and methods on top of the original interface.12Step 5: Add Factory to Control Duck CreationcreateMallardDuck(): QuackablecreateRedheadDuck() : QuackablecreateDuckCall(): QuackablecreateRubberDuck(): QuackableAbstractDuckFactoryDuckFactory CountingDuckFactoryCountingDuckFactory returns ducks that are automatically wrapped by the QuackCounter developed in Step 4This code is used by a method in DuckSimulator (not previously shown) that accepts an instance of AbstractDuckFactory as a parameter. Demonstration.13Review: Abstract Factory StructureClientcreateProductA(): AbstractProductAcreateProductB(): AbstractProductBAbstractFactory«Interface»createProductA(): ProductA1createProductB(): ProductB1ConcreteFactoryAcreateProductA(): ProductA2createProductB(): ProductB2ConcreteFactoryBAbstractProductA«Interface»AbstractProductB«Interface»ProductA1 ProductA2ProductB1 ProductB2factoryQuackableMallardDuckDuckSimulatorAbstractDuckFactoryDuckFactoryCountingDuckFactory14Step 6: Add support for Flocks with Compositequack()Quackable«interface»MallardDuck RedheadDuck DuckCall RubberDuckadd(Quackable)quack()FlockquackersNote: Iterator pattern is hiding inside of Flock.quack(); DemonstrationNote: This is a variation on Composite, in which the Leaf and Composite classes have different interfaces;Only Flock has the “add(Quackable)” method.Client code has to distinguish between Flocks and Quackables as a result. Resulting code is “safer” but less transparent.15Review: Composite StructureClientop1()op2()add(Component)remove(Component)getChild(int): ComponentComponentop1()op2()Leafadd(Component)remove(Component)getchild(int): Componentop1()op2()Compositechildren*DuckSimulatorQuackableMallardDuckFlock16Step 7: Add Quack Notification via ObserverregisterObserver(Observer)notifyObservers()QuackObservable«interface»quack()Quackable«interface»update(QuackObservable)Observer«interface»ObservableobserversQuackologistMallardDuckobservableCool implementation of the Observer pattern. All Quackables are made Subjects by having Quackable inherit from QuackObserver. To avoid duplication of code, an Observable helper class is implemented and composed with each ConcreteQuackable class. Flock does not make use of the Observable helper class directly; instead it delegates those calls down to its leaf nodes. Demonstration.17Review: Observer StructureregisterObserver()removeObserver()notifyObservers()Subject«Interface»update()Observer«Interface»observers*getState()setState()stateConcreteSubjectObserversubjectQuackableviaQuackObservableMallardDuckcomposed withObservableQuackologistObserver18Counting Roles• As you can see, a single class will play multiple roles in a design• Quackable defines the shared interface for five of the patterns• Each Quackable implementation has four roles to play: Leaf, ConcreteSubject, ConcreteComponent, ConcreteProduct• You should now see why names do not matter in patterns• Imagine giving MallardDuck the following name:• MallardDuckLeafConcreteSubjectComponentProduct• !!!• Instead, its the structure of the


View Full Document

CU-Boulder CSCI 5448 - Patterns of Patterns

Documents in this Course
Django

Django

42 pages

ENRS

ENRS

30 pages

PhoneGap

PhoneGap

22 pages

Load more
Download Patterns of 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 Patterns of 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 Patterns of 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?