Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 313 – Advanced Programming Topics. Observer Pattern Intent  Efficiently perform 1-to-many communication  Easy to respond dynamically when event(s)

Similar presentations


Presentation on theme: "CSC 313 – Advanced Programming Topics. Observer Pattern Intent  Efficiently perform 1-to-many communication  Easy to respond dynamically when event(s)"— Presentation transcript:

1 CSC 313 – Advanced Programming Topics

2 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

3 Registering an Observer  Subject Observer  Subject adds & removes Observers as needed  Adding is simple  Events also simple, call Observers’ update method  Removing not as simple, but still not very hard  Requires some means of holding Observers  Array  ArrayList or LinkedList  HashMap

4 Registering an Observer

5 Getting the Word Out Observer  Calling Observers’ update methods is simple Observer  But for call Observer needs associated information  Not always clear what is best way to share this data  Must understand strengths & weaknesses of each  Push model is easiest of two approaches  ObserverSubject  Observers get data directly from Subject  Method’s parameters have values to be used  Another approach using pattern’s pull model  Subject Observer  Subject provides strategy to Observers via a param Observer  To get data, Observer uses strategies methods

6 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  Event-based code needs Observers to work  Not limited to Java; true for most OO languages

7 java.util.Observable  Among original classes included in Java  Works with java.util.Observer interface  ObservableObserver  Observable manages Observer s in code Observer Observer void addObserver(Observer o) void deleteObserver(Observer o) void deleteObservers() int countObservers()

8 Handling Updates  Observable  Observable approach supports push… void notifyObserver(Object arg) … & pull model available as well void notifyObserver() boolean hasChanged() void clearChanged() void setChanged() Observer ’s  Notifications sent to Observer ’s only method Observable void update(Observable o, Object arg)

9 Observable Using Observable Problem public class CSCClass { private Observable obs; // Lots of unimportant code here public void cancelClass() { obs.notifyObservers(“Go drinking!”); } }

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

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

12 Bad & Worse Design Decisions  No update was sent

13 Bad & Worse Design Decisions  No update was sent; sobriety ruled the day

14 Bad & Worse Design Decisions  No update was sent; sobriety ruled the day

15 Bad & Worse Design Decisions

16 Updated Code

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

18 Next Attempt

19

20 setChanged has protected access

21 %&#*@ Awful Design

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

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

24 Final Attempt public class CSCClass extends Observable { public void cancelClass() { setChanged(); notifyObservers(“Go drinking”); } } public class Student implements Observer { public void update(Observable o, Object arg) { String s = (String)arg; if (!s.equals(“Get here NOW!!!”)) // Go drinking else // Go to class } }

25 Final Attempt Observable public class CSCClass extends Observable { public void cancelClass() { setChanged(); notifyObservers(“Go drinking”); } } Observer public class Student implements Observer { public void update(Observable o, Object arg) { String s = (String)arg; if (!s.equals(“Get here NOW!!!”)) // Go drinking else // Go to class with a drink } }

26 Final Attempt

27

28 Observable Pros & Cons  Cons:  Pros:

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

30 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 at week’s end & still on Angel  Make sure that you also check the grading rubric  Use Assignment Submitter when turning it in


Download ppt "CSC 313 – Advanced Programming Topics. Observer Pattern Intent  Efficiently perform 1-to-many communication  Easy to respond dynamically when event(s)"

Similar presentations


Ads by Google