DOC PREVIEW
CU-Boulder CSCI 5448 - MORE DESIGN PATTERNS

This preview shows page 1-2-3-4-24-25-26-50-51-52-53 out of 53 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 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 53 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 53 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 53 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 53 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 53 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 53 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 53 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 53 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 53 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 53 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

© Kenneth M. Anderson, 2011MORE DESIGN PATTERNSCSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGNLECTURE 24 — 04/07/20111Monday, April 11, 2011© Kenneth M. Anderson, 2011Goals of the LectureCover the material in Chapters 18 & 19 of our textbookObserverTe m p l a t e M e t h o dAnd, include two bonus patternsStateFlyweight2Monday, April 11, 2011Observer Pattern• Don’t miss out when something interesting (in your system) happens! • The observer pattern allows objects to keep other objects informed about events occurring within a software system (or across multiple systems)• It’s dynamic in that an object can choose to receive or not receive notifications at run-time• Observer happens to be one of the most heavily used patterns in the Java Development Kit• and indeed is present in many frameworks3Monday, April 11, 2011Weather MonitoringWeatherStationTempSensorHumiditySensorPressureSensorWeatherDataObjectTabTabTabDocument Windowpulldatadisplaydataprovided what we implementWe need to pull information from a station and then generate “current conditions, weather stats, and a weather forecast”. 4Monday, April 11, 2011WeatherData SkeletongetTemperature()getHumidity()getPressure()measurementsChanged()WeatherDataWe receive a partial implementation of the WeatherData class from our client.They provide three getter methods for the sensor values and an empty measurementsChanged() method that is guaranteed to be called whenever a sensor provides a new valueWe need to pass these values to our three displays… simple!5Monday, April 11, 2011First pass at measurementsChanged...12public void measurementsChanged() {34 float temp = getTemperature();5 float humidity = getHumidity();6 float pressure = getPressure();7 8 currentConditionsDisplay.update(temp, humidity, pressure);9 statisticsDisplay.update(temp, humidity, pressure);10 forecastDisplay.update(temp, humidity, pressure);1112}1314...1516Problems?1. The number and type of displays may vary. These three displays are hard coded with no easy way to update them.2. Coding to implementations, not an interface! Each implementation has adopted the same interface, so this will make translation easy!6Monday, April 11, 2011Observer Pattern• This situation can benefit from use of the observer pattern• This pattern is similar to subscribing to a hard copy newspaper• A newspaper comes into existence and starts publishing editions• You become interested in the newspaper and subscribe to it• Any time an edition becomes available, you are notified (by the fact that it is delivered to you)• When you don’t want the paper anymore, you unsubscribe• The newspaper’s current set of subscribers can change at any time• Observer is just like this but we call the publisher the “subject” and we refer to subscribers as “observers”7Monday, April 11, 2011Observer in Action (I)ObserversSubjectObserver 1Observer2Observer3Subject maintains a list of observers8Monday, April 11, 2011Observer in Action (II)ObserversSubjectObserver 1Observer2Observer3If the Subject changes, it notifies its observers9Monday, April 11, 2011Observer in Action (III)ObserversSubjectObserver 1Observer2Observer3If needed, an observer may query its subject for more information10Monday, April 11, 2011Observer In Action (IV)ObserversSubjectObserver 1Observer2Observer3At any point, an observer may join or leave the set of observers Observer411Monday, April 11, 2011Observer Definition and Structure• The Observer Pattern defines a one-to-many dependency between a set of objects, such that when one object (the subject) changes all of its dependents (observers) are notified and updated automaticallyregisterObserver()removeObserver()notifyObservers()Subject«Interface»update()Observer«Interface»observers*getState()setState()stateConcreteSubjectObserversubject12Monday, April 11, 2011Observer Benefits• Observer affords a loosely coupled interaction between subject and observer• This means they can interact with very little knowledge about each other• Consider• The subject only knows that observers implement the Observer interface• We can add/remove observers of any type at any time• We never have to modify subject to add a new type of observer• We can reuse subjects and observers in other contexts• The interfaces plug-and-play anywhere observer is used• Observers may have to know about the ConcreteSubject class if it provides many different state-related methods• Otherwise, data can be passed to observers via the update() method13Monday, April 11, 2011Demonstration• Roll Your Own Observer• Using java.util.Observable and java.util.Observer• Observable is a CLASS, a subject has to subclass it to manage observers• Observer is an interface with one defined method: update(subject, data)• To notify observers: call setChanged(), then notifyObservers(data)• Observer in Swing• Listener framework is just another name for the Observer pattern• Observer in Cocoa• Notifications (system defined as well as application defined)14Monday, April 11, 2011Template Method: Definition• The Template Method Pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure• Template Method defines the steps of an algorithm and allows subclasses to provide the implementation for one or more steps• Makes the algorithm abstract• Each step of the algorithm is represented by a method• Encapsulates the details of most steps• Steps (methods) handled by subclasses are declared abstract• Shared steps (concrete methods) are placed in the same class that has the template method, allowing for code re-use among the various subclasses15Monday, April 11, 2011Template Method: StructuretemplateMethod()primitiveOperation1()primitiveOperation2()AbstractClassprimitiveOperation1()primitiveOperation2()ConcreteClassprimitiveOperation1();primitiveOperation2()Very simple pattern…...but also very powerfulUsed typically in application frameworks, e.g. Cocoa and .NetprimitiveOperation1() and primitiveOperation2() are sometimes referred to as hook methods as they allow subclasses to hook their behavior into the service provided by AbstractClass16Monday, April 11, 2011Example: Tea and Coffee• Consider another Starbuzz example in which we consider the recipes for making coffee and tea in a barista’s


View Full Document

CU-Boulder CSCI 5448 - MORE DESIGN PATTERNS

Documents in this Course
Django

Django

42 pages

ENRS

ENRS

30 pages

PhoneGap

PhoneGap

22 pages

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