Recitation 11 - PowerPoint PPT Presentation

recitation 11 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Recitation 11 PowerPoint Presentation
Download Presentation
Recitation 11

play fullscreen
1 / 12
Recitation 11
152 Views
Download Presentation
jersey
Download Presentation

Recitation 11

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Recitation 11 November 11, 2011

  2. Today’s Goals: • Automatic refresh • Learn and apply the Observer pattern

  3. Fixing the Refresh Problem Events / Notifications Tell Object Editor when changes have occurred Observer pattern: “Observable” Object Obervers / Listeners

  4. Analogy: AlertCarolina • You register / listen for alerts • When the state changes, you receive a notification indicating: • State has changed • Information relevant to the change

  5. Two Steps in Observer Pattern 1) Observers register with observable objects to listen for events (one time only) 2) When observable object undergoes an appropriate change, it sends an informative notification to all registered observers

  6. Step 1: Registration (Properties) Observable class must implement: public void addPropertyChangeListener( PropertyChangeListener listener) This method adds the provided listener to a collection of listeners (as long as the collection doesn’t already contain this listener – avoid double notifications) If ObjectEditor.edit() is called on a class that implements this method, ObjectEditor will automatically register itself for notifications from this class

  7. Step 2: Notification (Properties) • When the observed object changes state, it calls propertyChange( PropertyChangeEvent event) on each of its observers • Construct a PropertyChangeEvent with: • this (i.e. a pointer to the observed object) • The name of the changed property as a String • “width”, “x”, “knight”, etc. • The old value of the property • The new value of the property

  8. Step 2 Example:setWidth(intnewWidth) intoldWidth = width; width = newWidth; PropertyChangeEvent event = new PropertyChangeEvent(this, “width”, oldWidth, newWidth); /* call propertyChange(event) on all observers */

  9. Collection Observers Very similar to property observers Must implement public void addVectorListener(VectorListener listener); When collection changes, call on all registered listeners: updateVector(VectorChangeEventevent)

  10. Vector Change Event packageutil.models; publicclassVectorChangeEvent { // constants to be used for event type publicstaticfinalintAddComponentEvent = 1, DeleteComponentEvent = 2, ChangeComponentEvent = 3, InsertComponentEvent = 4, CompletedComponentsEvent = 5, ClearEvent = 6, UndefEvent = 1000; // constructor, oldObject can be null when no value is replaced publicVectorChangeEvent(Object theSource, int type, intposn, Object oldObject, Object newObject, intnewSize) { … } … }

  11. Vector Change Event • To create a VectorChangeEvent within an Observer, pass the following into the VectorChangeEvent constructor (in order): • this • The int corresponding to the change that occurred, e.g. VectorChangeEvent.AddComponentEvent • The index of the affected element • The Object that was previously at that location (null for push()) • The Object that is now at that location (null for pop()) • The new size of the collection

  12. Recitation Specification • Download Recitation11.zip from the Recitations page • Implement ObservableAlignedShapeStack: • Create an ArrayList instance variable that stores all objects listening to / observing this stack (VectorChangeListenerobjects) • public void addVectorListener(VectorListenerlistener) /* adds the provided listener to the list of listeners, but only if the listener is not already in the list */ • Hint: Use the add() and contains() methods from ArrayList • Create: void notifyAllListeners(VectorChangeEventevent) • For each listener in the ArrayList, call propertyChange on that listener with the provided event • Override push() and pop() so that they notify all listeners of the change made to the collection (but only IF a change was made!) • If done correctly, you won’t need to manually refresh