|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
E
- Type of event data that this ISubject
notifies with.public interface ISubject<E>
Observer Design Pattern: Define a one-to-many dependency between
objects so that when one object changes state, all its dependents are
notified and updated automatically. (Gamma et al, Design Patterns)
Responsibility: Abstract generic definition of the "Subject".
pattern role:
IObserver
objects may
observe a ISubject
.IObserver
objects.IObserver
instances are not
coupled to the ISubject
interface i.e. there is no need for the
IObserver
to query ISubject
's state. State
change or Message content is pushed to the notifyObservers(E)
callback method.IObserver
to downcast the parameter as is the case for
e.g. java.util.Observer
implementation.
//
// Create Observer Pattern elements
//
ISubject<NullEventData> mySubject = new ConcreteSubject();
IObserver<NullEventData> myFirstObserver = new ConcreteObserver();
IObserver<NullEventData> mySecondObserver = new ConcreteObserver();
//
// Subscribe multiple Observer instances to the Subject
//
mySubject.attach(myFirstObserver, mySecondObserver);
//
// Notify all subscribed Observers, optionally pass some Event
// data associated with the Event.
//
mySubject.notifyObservers(NullEventData.getInstance());
//
// Detach Observer instances
//
mySubject.detach(myFirstObserver);
Method Summary | |
---|---|
void |
attach(IObserver<E>... anObservers)
Attaches a variable list of IObserver instances to the
ISubject instance. |
void |
clear()
Detaches all registered IObserver instances from this
ISubject |
void |
detach(IObserver<E>... anObservers)
Detaches a variable list of IObserver instances from the
ISubject instance. |
void |
notifyObservers(E anEventData)
Notifies the attached IObserver instances that a change
has occurred. |
int |
size()
Returns the number of attached IObserver instances |
Method Detail |
---|
void attach(IObserver<E>... anObservers)
IObserver
instances to the
ISubject
instance.
anObservers
- IObserver
instances to be attached.
IllegalArgumentException
- 'anObservers' must not be null.void clear()
IObserver
instances from this
ISubject
void detach(IObserver<E>... anObservers)
IObserver
instances from the
ISubject
instance.
anObservers
- IObserver
instances to be detached.
IllegalArgumentException
- 'anObservers' must not be null.void notifyObservers(E anEventData)
IObserver
instances that a change
has occurred. It is compulsory to provide a non null parameter
anEventData
. For situations where passing information is
not required use instead NullEventData
singleton instance.
anEventData
- Event data pushed to IObserver
instances.
IllegalArgumentException
- 'anEventData' must not be null.int size()
IObserver
instances
IObserver
instances.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |