CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy Studies investigated economy of...

19
LECTURE 6: OBSERVER PATTERN –OR– LINDSAY LOHAN, CSC GENIUS CSC 313 – Advanced Programming Topics

Transcript of CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy Studies investigated economy of...

Page 1: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

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

CSC 313 – Advanced Programming Topics

Page 2: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

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 3: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

Something about Lindsay

Is it her singing?

Page 4: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

Something about Lindsay

Is it her singing? Acting key to her

success?

Page 5: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

Something about Lindsay

Is it her singing? Acting key to her

success? Her keen fashion

sense?

Page 6: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

Something about Lindsay

Is it her singing? Acting key to her

success? Her keen fashion

sense? Long, slow, very

public total breakdown?

Page 7: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

Something about Lindsay

Is it her singing? Acting key to her

success? Her keen fashion

sense? Long, slow, very

public total breakdown?

No! Really due to her mastery of Observer Pattern!

Page 8: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

How Lindsay Does It

Millions interested in seeing this unfold Each and every mistake, Every outburst that she makes, And, of course, what or whom she is dating

Could follow her to be around for each event But may have job, school, or restraining

orders

Lindsay uses Observer Pattern for attention & $$$

Page 9: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

Observer Pattern

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

updates

ConcreteSubject

stateChanged() : booleansetChanged() : void

Page 10: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

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 11: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

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 12: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

Textbook Observer Pattern

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

master) Observers in this relationship are

Paparazzi

Page 13: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

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 14: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

True Genius

Observer Pattern == $$$ in my bank!

Page 15: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

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 no DUIs, covers with Kate

Gosselin instead

Page 16: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

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 17: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

Replace Cycles With Observer Cycle still in UML class diagram…

Page 18: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

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 19: CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.

For Next Lecture

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

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

world? Is it really this simple and powerful?