- Type Parameters:
TMsg
- The type of message to send to all the observers.
- All Known Implementing Classes:
ADispatcher
,ParallelDispatcher
,SequentialDispatcher
public interface IDispatcher<TMsg>
Top-level abstraction of a Dispatcher which is the Observable in the
Observer-Observable Design Pattern without the requirement to set the state
before sending a message to the observers.
A Dispatcher sends IMsg-type messages to all its observers which therefore must
be capable of receiving and processing those messages, i.e. are IObserver<TMSg>.
To maximally decouple an application from the randomizer's implementation any variable representing a randomizer
should be typed to this interface, not to any concrete implementation. For example:
IDispatcher<Graphics> myDispatcher = new SequentialDispatcher<Graphics>()
- Author:
- swong
-
Method Summary
Modifier and TypeMethodDescriptionboolean
addObserver(IObserver<TMsg> obs)
Add an observer to the dispatcher.Get a COPY of the set of all the observers currently in the dispatcher.Removes all the observers currently in the dispatcherremoveObserver(IObserver<TMsg> obs)
Remove an observer from the dispatcher.void
Send the given message to all the observers in the dispatcher
-
Method Details
-
addObserver
Add an observer to the dispatcher. If the observer is already in the dispatcher, as determined by the comparison (equals()) process, the dispatcher is NOT mutated and false is returned.- Parameters:
obs
- The IObserver to add- Returns:
- true if the given observer was not already in the dispatcher, false otherwise.
- Throws:
ClassCastException
- If the observer cannot be properly compared against the existing observersNullPointerException
- If the supplied value is null
-
removeObserver
Remove an observer from the dispatcher. The dispatcher does not make any assumptions that the observer being removed is identically the same object as that it was requested to remove via the input parameter. The returned object is the object that was internally held by the dispatcher.- Parameters:
obs
- The IObserver to add- Returns:
- The observer that was removed or null if it was not found.
-
getAllObservers
Get a COPY of the set of all the observers currently in the dispatcher. This is a shallow copy, so the observers themselves are not copied.- Returns:
- A set of IObservers
-
removeAllObservers
Removes all the observers currently in the dispatcher- Returns:
- A COPY of the set of IObservers in the dispatcher before they were all removed.
-
updateAll
Send the given message to all the observers in the dispatcher- Parameters:
msg
- The IMsg to send to all the observers
-