Package jsl.observers
Class ObservableComponent
- java.lang.Object
-
- jsl.observers.ObservableComponent
-
- All Implemented Interfaces:
ObservableIfc
public class ObservableComponent extends java.lang.Object implements ObservableIfc
The Java observer/observable pattern has a number of flaws. This class provides a base implementation of the observer/observable pattern that mitigates those flaws. This allows observers to be added and called in the order added to the component. The basic usage of this class is to have a class have an instance of ObservableComponent while implementing the ObservableIfc. The notifyObservers() method can be used to notify attached observers whenever necessary.
-
-
Constructor Summary
Constructors Constructor Description ObservableComponent()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddObserver(ObserverIfc observer)Allows the adding (attaching) of an observer to the observablebooleancontains(ObserverIfc observer)Returns true if the observer is already attachedintcountObservers()Returns how many observers are currently observing the observablevoiddeleteObserver(ObserverIfc observer)Allows the deletion (removing) of an observer from the observablevoiddeleteObservers()Deletes all the observers from the observablevoidnotifyObservers(java.lang.Object theObserved, java.lang.Object arg)Notify the observers
-
-
-
Method Detail
-
addObserver
public void addObserver(ObserverIfc observer)
Description copied from interface:ObservableIfcAllows the adding (attaching) of an observer to the observable- Specified by:
addObserverin interfaceObservableIfc- Parameters:
observer- the observer to attach
-
deleteObserver
public void deleteObserver(ObserverIfc observer)
Description copied from interface:ObservableIfcAllows the deletion (removing) of an observer from the observable- Specified by:
deleteObserverin interfaceObservableIfc- Parameters:
observer- the observer to delete
-
deleteObservers
public void deleteObservers()
Description copied from interface:ObservableIfcDeletes all the observers from the observable- Specified by:
deleteObserversin interfaceObservableIfc
-
contains
public boolean contains(ObserverIfc observer)
Description copied from interface:ObservableIfcReturns true if the observer is already attached- Specified by:
containsin interfaceObservableIfc- Parameters:
observer- the observer to check- Returns:
- true if attached
-
countObservers
public int countObservers()
Description copied from interface:ObservableIfcReturns how many observers are currently observing the observable- Specified by:
countObserversin interfaceObservableIfc- Returns:
- number of observers
-
notifyObservers
public void notifyObservers(java.lang.Object theObserved, java.lang.Object arg)Notify the observers- Parameters:
theObserved-arg-
-
-