lecture 7: observer pattern –or– lindsay lohan, csc genius

22
LECTURE 7: OBSERVER PATTERN –OR– LINDSAY LOHAN, CSC GENIUS CSC 313 – Advanced Programming Topics

Upload: wan

Post on 25-Feb-2016

37 views

Category:

Documents


0 download

DESCRIPTION

CSC 313 – Advanced Programming Topics. Lecture 7: Observer Pattern –or– Lindsay Lohan, CSC Genius. Goals. Understand secret to how celebrities live Consider “talents” & if that keeps all our interest Look at how they live life in public without stalkers - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

LECTURE 7:OBSERVER PATTERN –OR– LINDSAY LOHAN, CSC GENIUS

CSC 313 – Advanced Programming Topics

Page 2: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Goals

Understand secret to how celebrities live Consider “talents” & if that keeps all our

interest Look at how they live life in public without

stalkers Communicate efficiently & see how it

makes money Understand the situations solved by

Observers What is intent of this design pattern? In pattern, what represented by Subject &

Observer?

Page 3: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Lindsay Lohan Economy

Studies investigated economy of celebrities Direct earnings from movies, music, TV,

ads Profits earned by her label, handlers,

etc. Photographers’ income for pictures on

TMZ When placed on cover, increase in

magazine sales $150+ million/year industry for top

celebrity

Page 4: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Something about Lindsay

Is it her singing?

Page 5: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Something about Lindsay

Is it her singing? Acting key to her

success?

Page 6: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Something about Lindsay

Is it her singing? Acting key to her

success? Her keen modeling

skills?

Page 7: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Something about Lindsay

Is it her singing? Acting key to her

success? Her keen modeling

skills? Long, repeated,

obvious public cries for help?

Page 8: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Something about Lindsay

Is it her singing? Acting key to her

success? Her keen modeling

skills? Long, repeated,

obvious public cries for help?

No! Really due to her mastery of Observer Pattern!

Page 9: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

How Lindsay Does It

Millions interested in seeing this unfold Every one of her painful, drug-addled

mistakes Every person she hits with her fists or her car And, of course, what crime she will commit

next Could follow her to be around for each

event But may have job, school, or restraining

orders

Lindsay uses Observer Pattern for attention & $$$

Page 10: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Observer Pattern

Observers want update when subject changes Must manage this 1:many relationship Efficiently communicate all of these

updates

stateChanged() : booleansetChanged() : void

ConcreteSubject

Page 11: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Foundation of an Industry

Lindsay notifies paparazzi before going out With notice, photographers go to her

destination Take incriminating pictures of her doing

her thing With pictures takes, paparazzi notify

press Some published in magazines & used on

Dr. Phil Others will get posted to blogs “Private use” of individuals for some

pictures Entire crisis then unfolds for

enquiring minds

Page 12: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Textbook Observer Pattern

Readers sent People when it is printed Readers are observers Subject is People magazine (in this case) Whenever subject updated, observers

notified Magazines notified of pics paparazzi

took In this relationship, People is an observer Subject is each photographer

Page 13: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Textbook Observer Pattern

Paparazzi alerted by Lindsay before leaving Lindsay is ultimate subject (& pattern

master) Observers in this relationship are

Paparazzi

Page 14: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

True Genius

Very efficient way for Lindsay to get money Also has benefit of limiting people

following her Readers notified of each and every

change In this adventure, readers observers

only Lindsay only a subject in this

implementation Magazines & photographers play

both roles Observer Pattern mastered for this to

work

Page 15: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

True Genius

Observer Pattern == $$$ in my bank!

Page 16: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Observer Is Hollywood

Hollywood Principle:Don’t Call Us, We’ll Call You

If observers were to ask constantly“Are we there yet? Are we there

yet?” Really slow when many observers exist

Principle says to have subject send alerts Articles only after craziness on Lindsay’s

part When off probation, cover Kim Kardashian

instead

Page 17: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Observer Pattern Intent

Page 18: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Observer Pattern Intent

Looks to handle 1-to-many communication Observers track the state of some single

object Performance hit from repeat Subject

checks avoided Common script used to perform this

work Once ready, Observer registers with

Subject Subject notifies each Observer if

something happens If updates not wanted, Observer can

deregister itself Checking for updates not needed with

Observer Limits work to be done, so avoids swamping

system

Page 19: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Co-Dependent Relationships UML class diagrams with cycles are

problem Two classes inseparable no matter what

Classes completely dependent on each other Almost impossible to add or extend the

design Since both classes needed, reusing one

impossible

Page 20: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Replace Cycles With Observer Cycle still in UML class diagram…

Page 21: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

Replace Cycles With Observer Cycle still in UML class diagram… … but now classes depend only on

interfaces Classes can be replaced at any time

Only requires that new class implement interface

Far easier to add or modify classes now

Page 22: Lecture 7: Observer Pattern –or–  Lindsay Lohan, CSC Genius

For Next Lecture

Read pages 56 – 69 in book Great… so how do we code the observer

pattern? Haven’t we seen this pattern before? How is it actually written in the real

world? Is it really this simple and powerful?