recitation 11

12
Recitation 11 November 11, 2011

Upload: jersey

Post on 05-Jan-2016

36 views

Category:

Documents


3 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Recitation 11

Recitation 11November 11, 2011

Page 2: Recitation 11

Today’s Goals:

Automatic refresh

Learn and apply the Observer pattern

Page 3: Recitation 11

Fixing the Refresh Problem

Tell Object Editor when changes have occurred

Observer pattern:

“Observable” Object Obervers / Listeners

Events / Notifications

Page 4: Recitation 11

Analogy: AlertCarolina

You register / listen for alerts When the state changes, you receive a

notification indicating: State has changed Information relevant to the change

Page 5: Recitation 11

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

Page 6: Recitation 11

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

Page 7: Recitation 11

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

Page 8: Recitation 11

Step 2 Example:setWidth(int newWidth)

int oldWidth = width;

width = newWidth;

PropertyChangeEvent event = new PropertyChangeEvent(this, “width”, oldWidth, newWidth);

/* call propertyChange(event) on all observers */

Page 9: Recitation 11

Collection Observers

Very similar to property observers Must implementpublic void addVectorListener(VectorListener listener);

When collection changes, call on all registered listeners:

updateVector(VectorChangeEvent event)

Page 10: Recitation 11

Vector Change Eventpackage util.models;public class VectorChangeEvent {

// constants to be used for event typepublic static final int AddComponentEvent = 1,

DeleteComponentEvent = 2, ChangeComponentEvent = 3, InsertComponentEvent = 4, CompletedComponentsEvent = 5, ClearEvent = 6, UndefEvent = 1000;

// constructor, oldObject can be null when no value is replaced

public VectorChangeEvent(Object theSource, int type, int posn, Object oldObject, Object newObject, int newSize) {

…}…

}

Page 11: Recitation 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

Page 12: Recitation 11

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 (VectorChangeListener objects)

public void addVectorListener(VectorListener listener) /* 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(VectorChangeEvent event) 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