csc 313 advanced programming topics
Download
Skip this Video
Download Presentation
Lecture 8: Implementing Observer Pattern

Loading in 2 Seconds...

play fullscreen
1 / 21

Lecture 8: Implementing Observer Pattern - PowerPoint PPT Presentation


  • 108 Views
  • Uploaded on

CSC 313 – Advanced Programming Topics. Lecture 8: Implementing Observer Pattern. Today’s Goal. Review design patterns (tools) purpose What is needed so that a tool becomes useful? Why can tools suck and how do we avoid this? What does this mean for design patterns?

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Lecture 8: Implementing Observer Pattern' - lerato


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
today s goal
Today’s Goal
  • Review design patterns (tools) purpose
    • What is needed so that a tool becomes useful?
    • Why can tools suck and how do we avoid this?
    • What does this mean for design patterns?
  • Observer pattern implementation discussed
    • How do we handle Observer addition & removal?
    • What do update methods need to be useful?
    • Why do we have to talk about pushing & pulling?
  • Coding “gotchas” discussed & diagnosed
design pattern intent
Design Pattern Intent
  • Design patterns are tools
    • Like all tools, have reason for being
design pattern intent1
Design Pattern Intent
  • Design patterns are tools
    • Like all tools, have reason for being
design pattern intent2
Design Pattern Intent
  • Design patterns are tools
    • Like allmost tools, have reason for being
design pattern intent3
Design Pattern Intent
  • Design patterns are tools
    • Like allmost tools, have reason for being
    • Explains when & where to use pattern
  • When used improperly, tool is sad
    • Bieberalways depressed; Seacrest not much better
  • To be useful, need & pattern MUSTmatch
    • Coding is simple when used properly
    • More importantly, life just sucks when you don’t
strategy pattern details
Strategy Pattern Details
  • Make family of algorithms interchangable
    • Avoid duplicating code across classes & projects
    • Allows to change algorithm dynamically
  • Implementation easy when used correctly
    • Everything relies on interface defining behaviors
    • Each strategy defines method to perform some action
    • Write into the context class has field of interface type
    • Call field’s method (delegate) to perform action
observer pattern intent1
Observer Pattern Intent
  • Efficiently perform 1-to-many communication
    • Easy to respond dynamically when event(s) occurs
    • Can create many, many possible actions to occur
    • Update which actions used throughout execution
  • Can also use to break mutual dependency
    • UML class diagram cycles split and managed
    • Dirty & ugly hack that also is incredibly useful
observer pattern examples
Observer Pattern Examples
  • Where have you seen Observer Pattern?
registering an observer
Registering an Observer
  • Subjectadds & removes Observers as needed
    • Adding is simple
    • Events also simple, call Observers’ update method
    • Removing not so simple, but not very hard either
  • Requires some means of holding Observers
    • Array
    • ArrayList or LinkedList
    • HashMap
registering an observer1
Registering an Observer
  • Subjectadds & removes Observers as needed
    • Adding is simple
    • Events also simple, call Observers’ update method
    • Removing not so simple, but not very hard either
  • Requires some means of holding Observers
    • Array*
    • ArrayList or LinkedList
    • HashMap

* Anyone who did not object to this, fails.

getting the word out
Getting the Word Out
  • Calling Observers’ update methods is simple
    • But for call Observerneeds associated information
    • Not always clear what is best way to share this data
    • Must understand strengths & weaknesses of each
observer s push model
Observer’s Push Model
  • Easiest approach is pattern’s push model
    • Subject pushes data onto Observers
    • Parameters used in call provides all data needed
  • Writing & using is simple with this model, but…
    • Once this is defined, stuck with data to be shared
    • Adding data hard, since must rewrite all Observers
    • Removal easier, but users guessing why param exists
observer s pull model
Observer’s Pull Model
  • Another approach using pattern’s pull model
    • Subjectprovides instance to Observers in the call
    • To get data, Observer calls instance’s methods
    • Extend instance’s interface to make more available
    • But Observers using original methods still work
  • Making instances simple part of this process
    • Will need to have family of algorithms
    • Used interchangeably, but only 1 used at a time
observer s pull model1
Observer’s Pull Model
  • Another approach using pattern’s pull model
    • Subjectprovides instance to Observers in the call
    • To get data, Observer calls instance’s methods
    • Extend instance’s interface to make more available
    • But Observers using original methods still work
  • Making instances simple part of this process
    • Just need to use Strategy Pattern
pull model s pros and cons
Pull Model’s Pros and Cons
  • Communicating using Strategy pattern means:
  • Observers & Subjectindependent of each other
    • Subject provides the strategy to Observer
  • Subject& strategy semi-independent of other
    • Subject source of event that strategy communicates
  • Observer needs strategy & must change together
problems with pull model
Problems with Pull Model
  • Pull model also has its problems
    • May need multiple calls to get all the data
    • Need to consider multi-threaded access issues
    • Interfere with other design patterns, also possible
  • At the same time, problems easier & harder
    • If not multi-threaded, that issue can be ignored
    • As we will see, method calls can cost nothing
    • Just be careful & be certain of your decision
other observer gotchas
Other Observer Gotchas
  • Observer only needs some of the events
    • Intent is to perform 1-to-many communication
    • Subjectcannot filter events for each Observer
    • Better to create addition Observer to do filtering
  • Subject has multiple sets of events to observe
    • Could choose to rely on single Observer interface
      • Java AWT/Swing libraries use this approach
    • Multiple Observer interfaces each with 1 event
      • Simpler, but less efficient if multiple events wanted
for next lecture
For Next Lecture
  • Read pages 70 – 74 in book
    • Is this part of Java; what does Observable do?
    • How would one use it?
    • Was there anyone with IQ over 80 to correct it?
ad