Presentation is loading. Please wait.

Presentation is loading. Please wait.

Properties and Collections

Similar presentations


Presentation on theme: "Properties and Collections"— Presentation transcript:

1 Properties and Collections

2 This exercise is about how to let one object react to the behavior of another object, and how to refresh ObjectEditor without using refresh(). To achieve this, you need to have observable classes and observer classes. Observers should register at observables by calling addPropertyChangeListener. And they also need to implement PropertyChangeListener, otherwise they cannot register. Observables must have code to notify observers. Notice that method for notification (notifyAllListeners) is already in class APropertyListenerSupport, so you just need to set it as a property of the observable and call that method. Correspondingly, observers should contain methods that react to notifications. The name of the method is propertyChange(PropertyChangeEvent arg0). (The implementaton of notification is actually invoke this method)

3 Lecture Slides: Properties and Collections
MvcPropertiesCollections.pptx

4 Program Explanations The driver class simply moves thief left (and no refresh() here). Each time the thief moves, it sends a notification to AScene, which run a test to see whether the thief is in house or not. If the thief is in house, then AScene change alarm and state. Notice the code in constructor of AScene, it register at thief as a propertyChangeListener, which is required for every class you write, if you want to let them responds to something. But you do not need to register ObjectEditor as propertyChangeListener to let it refresh automatically. This is done by itself when you let it edit some object

5 PropertyListenerSupport
This class is like a encapsulation of Observable. It contains a list which stores the observers. Basically it works like the collection (array list, linked list), and it contains a special method which notifies all observers. So it makes life much simpler, just let those observer class contains an instance of this class as a property. And to add observer and notify observers is actually call the methods of PropertyListenerSupport.

6 APropertyChangeMonitor is another class
APropertyChangeMonitor is another class. It takes an instance of AScene as argument when created. And it register as observers at the four components of AScene to monitor the changes of them. If any notification is sent to the monitor, it prints out the name of the object, name of changed property, old value and the new value in the console. These information are contained in the notification. It will be help if you need to debug with observable / observer.

7 Frequent Reasons for Failure
Observers do not register, or observables do not have register method. Observable does not notify observers propertyChange method implemented incorrectly For the last reason, notice that the propertyChange method does has an argument, which contains the necessary information of the change event and it is sent from the observer. So it is possible for observers to respond dependently.


Download ppt "Properties and Collections"

Similar presentations


Ads by Google