Class AbstractSubject<T,O extends Observer<T>>
java.lang.Object
io.github.astrapi69.design.pattern.observer.AbstractSubject<T,O>
- Type Parameters:
T
- the generic type of the observable objectO
- the generic type of the observer
- All Implemented Interfaces:
Subject<T,
O>
- Direct Known Subclasses:
ChatRoom
public abstract class AbstractSubject<T,O extends Observer<T>>
extends Object
implements Subject<T,O>
The class
AbstractSubject
is an implementation of the Subject
interface This
class encapsulates the observable object and notifies all registered observers when the
observable changes-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor for a new subject with no initial observableAbstractSubject
(T observable) Constructor for a new subject with an initial observable -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds the given observer to the list of observersvoid
addAll
(Collection<O> observers) Adds the given observers to the list of observersvoid
Removes the given observer from the list of observersvoid
removeAll
(Collection<O> observers) Removes the given observers from the list of observersvoid
setObservable
(T observable) Sets the observable object, which is the object being observed for state changesvoid
Notifies all observers of a change in the state of the observable object Each observer'supdate
method is called with the current state of the observableMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.github.astrapi69.design.pattern.observer.api.Subject
getObservable, getObservers
-
Constructor Details
-
AbstractSubject
public AbstractSubject()Default constructor for a new subject with no initial observable -
AbstractSubject
Constructor for a new subject with an initial observable- Parameters:
observable
- the initial observable object
-
-
Method Details
-
add
Adds the given observer to the list of observers -
addAll
Adds the given observers to the list of observers -
remove
Removes the given observer from the list of observers -
removeAll
Removes the given observers from the list of observers -
setObservable
Sets the observable object, which is the object being observed for state changes -
updateObservers
public void updateObservers()Notifies all observers of a change in the state of the observable object Each observer'supdate
method is called with the current state of the observable
-