1 / 12

Recitation 11

Recitation 11. November 11, 2011. Today’s Goals:. Automatic refresh Learn and apply the Observer pattern. Fixing the Refresh Problem. Events / Notifications. Tell Object Editor when changes have occurred Observer pattern:. “Observable” Object. Obervers / Listeners.

jersey
Download Presentation

Recitation 11

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. 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

More Related