Package org.xjge.core
Class Observable
java.lang.Object
org.xjge.core.Observable
public class Observable
extends java.lang.Object
A Component object which can be used to relay information about state
changes occurring in the implementing object to one or more observers
located anywhere in the codebase.
Objects which make use of observable should define the properties (often fields) observers should look for in their constructors with the properties collection provided. This usually looks something like this:
int prop1 = 0; boolean prop2 = false; char prop3 = 'someVal'; Observable observable = new Observable(this); MyObject(Object other) { ... observable.properties.put("myProperty1", prop1); observable.properties.put("myProperty2", prop2); observable.properties.put("myProperty3", prop3); ... observable.addObserver(other); ... } //Called every game tick... MyObject.update() { notifyObservers("myProperty1", prop1); notifyObservers("myProperty2", prop2); notifyObservers("myProperty3", prop3); ... }
- Since:
- 2.0.0
-
Field Summary
Fields Modifier and Type Field Description java.util.HashMap<java.lang.String,java.lang.Object>
properties
-
Constructor Summary
Constructors Constructor Description Observable(java.lang.Object object)
Creates a new observable object that will look for state changes in the object provided and supply it to other parts of the codebase. -
Method Summary
Modifier and Type Method Description void
addObserver(java.beans.PropertyChangeListener observer)
Exposes this objects state to another.void
notifyObservers(java.lang.String name, java.lang.Object property)
Notifies all observers of state changes in this object.void
removeObserver(java.beans.PropertyChangeListener observer)
Revokes an observers access to view the state changes of implementing objects.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
properties
public final java.util.HashMap<java.lang.String,java.lang.Object> properties
-
-
Constructor Details
-
Observable
public Observable(java.lang.Object object)Creates a new observable object that will look for state changes in the object provided and supply it to other parts of the codebase.- Parameters:
object
- the implementing object that will expose its state
-
-
Method Details
-
addObserver
public void addObserver(java.beans.PropertyChangeListener observer)Exposes this objects state to another. Observer objects must implementPropertyChangeListener
.- Parameters:
observer
- the object that is interested in the state changes of the one implementing theObservable
component
-
removeObserver
public void removeObserver(java.beans.PropertyChangeListener observer)Revokes an observers access to view the state changes of implementing objects.- Parameters:
observer
- the observer to remove
-
notifyObservers
public void notifyObservers(java.lang.String name, java.lang.Object property)Notifies all observers of state changes in this object.- Parameters:
name
- the name of the property (field) we're observingproperty
- an object representing the value of the property changed
-