1 / 27

Lecture 8: Java’s Observable & Observer

CSC 313 – Advanced Programming Topics. Lecture 8: Java’s Observable & Observer. Observer Pattern in Java. Java ♥ Observer Pattern & uses everywhere Find pattern in JButton & ActionListener s Tracking mouse in Swing using Observer Pattern Much of the multi-threaded API uses this

yetty
Télécharger la présentation

Lecture 8: Java’s Observable & Observer

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 313 – Advanced Programming Topics Lecture 8:Java’s Observable & Observer

  2. Observer Pattern in Java • Java ♥ Observer Pattern & uses everywhere • Find pattern in JButton & ActionListeners • Tracking mouse in Swing using Observer Pattern • Much of the multi-threaded API uses this • Event-based code needs Observers to work • Not limited to Java; true for most OO languages

  3. java.util.Observable • Among original classes included in Java • Works with java.util.Observer interface • Observable manages Observersin code void addObserver(Observer o)void deleteObserver(Observer o)void deleteObservers() intcountObservers()

  4. Handling Updates • Observable will support push model… void notifyObserver(Object arg) … and pull model can also be used void notifyObserver() booleanhasChanged() void clearChanged() void setChanged() • Notifications sent to Observer’s only method void update(Observable o, Object arg)

  5. Using Observable Problem public class CSCClass {private Observable obs;// Lots of unimportant code herepublic void cancelClass() {obs.notifyObservers(“Go ski”);} }

  6. Using Observable Problem public class CSCClass {private Observable obs;// Lots of unimportant code herepublic void cancelClass() {obs.notifyObservers(“Go ski”);} }

  7. Using Observable Problem public class CSCClass {private Observable obs;// Lots of unimportant code herepublic void cancelClass() {obs.notifyObservers(“Go ski”);} }

  8. Bad & Worse Design Decisions • No update was sent

  9. Bad & Worse Design Decisions • No update was sent

  10. Bad & Worse Design Decisions • No update was sent; everyone went to class

  11. Bad & Worse Design Decisions • No update was sent; everyone went to class • notifyObservers misnamed horribly • Should be notifyObserversIfChanged • Must call setChangedbefore notifying • Use of notifyObservers inconsistent, too • Calls clearChangedautomatically once updates sent

  12. Updated Code public class CSCClass {private Observable obs;// Lots of unimportant code herepublic void cancelClass() {obs.setChanged();obs.notifyObservers(“Go ski”);} }

  13. Next Attempt public class CSCClass {private Observable obs;// Lots of unimportant code herepublic void cancelClass() {obs.setChanged();obs.notifyObservers(“Go ski”);} }

  14. Next Attempt public class CSCClass {private Observable obs;// Lots of unimportant code herepublic void cancelClass() {obs.setChanged();obs.notifyObservers(“Go ski”);} }

  15. Next Attempt public class CSCClass {private Observable obs;// Lots of unimportant code herepublic void cancelClass() {obs.setChanged();obs.notifyObservers(“Go ski”);} }

  16. Next Attempt public class CSCClass {private Observable obs;// Lots of unimportant code herepublic void cancelClass() {obs.setChanged();obs.notifyObservers(“Go ski”);} } setChanged has protected access

  17. %&#*@ Awful Design

  18. %&#*@ Awful Design • setChanged & clearChangedprotected access • java.util package is sealed; cannot write class for it • Can only be called & used from within subclass • Must write subclass of Observable to use it • As this is Java, that class cannot extend others • Updates received in method of Observer: void update(Observable o, Object arg) • This really means that can onlypush 1 object • Kills point of push model since cannot push much

  19. %&#*@ Awful Design • setChanged & clearChangedprotected access • java.util package is sealed; cannot write class for it • Can only be called & used from within subclass • Must write subclass of Observable to use it • As this is Java, that class cannot extend others • Updates received in method of Observer: void update(Observable o, Object arg) • This really means that can onlypush 1 object • Kills point of push model since cannot push much Useless

  20. Final Attempt public class CSCClassextends Observable {public void cancelClass() {setChanged(); notifyObservers(“Go ski”);} } public class Student implements Observer {public void update(Observable o, Object arg) { String s = (String)arg; if (s.equals(“Get here NOW!!!”)) // Go skiing else // Go to class; think about skiing} }

  21. Final Attempt

  22. Final Attempt

  23. Final Attempt

  24. Final Attempt

  25. Observable Pros & Cons • Cons: • Pros:

  26. Observable Pros & Cons • Cons: • Need a subclass of Observable to use • Must call setChange before notifyObservers • Can push only 1 object to Observers • Pros: • Already has code to add & remove Observers • Easy to rewrite and improve upon this design

  27. For Next Lecture • Two (short) readings available on web • Will start looking into how code is optimized • How does Java run & make programs faster? • What can we do to make our code faster? • Using final can be huge. Why would it matter? • Lab #2 due in 1 week & available on Angel • Make sure that you also check the grading rubric • Help available in WTC206 during the lab time

More Related