java.lang.Object
io.github.astrapi69.design.pattern.observer.AbstractSubject<T,O>
Type Parameters:
T - the generic type of the observable object
O - 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

    Constructors
    Constructor
    Description
    Default constructor for a new subject with no initial observable
    AbstractSubject(T observable)
    Constructor for a new subject with an initial observable
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(O observer)
    Adds the given observer to the list of observers
    void
    addAll(Collection<O> observers)
    Adds the given observers to the list of observers
    void
    remove(O observer)
    Removes the given observer from the list of observers
    void
    removeAll(Collection<O> observers)
    Removes the given observers from the list of observers
    void
    setObservable(T observable)
    Sets the observable object, which is the object being observed for state changes
    void
    Notifies all observers of a change in the state of the observable object Each observer's update method is called with the current state of the observable

    Methods 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

      public AbstractSubject(T observable)
      Constructor for a new subject with an initial observable
      Parameters:
      observable - the initial observable object
  • Method Details

    • add

      public void add(O observer)
      Adds the given observer to the list of observers
      Specified by:
      add in interface Subject<T,O extends Observer<T>>
      Parameters:
      observer - the observer to be added
    • addAll

      public void addAll(Collection<O> observers)
      Adds the given observers to the list of observers
      Specified by:
      addAll in interface Subject<T,O extends Observer<T>>
      Parameters:
      observers - the observers to be added
    • remove

      public void remove(O observer)
      Removes the given observer from the list of observers
      Specified by:
      remove in interface Subject<T,O extends Observer<T>>
      Parameters:
      observer - the observer to be removed
    • removeAll

      public void removeAll(Collection<O> observers)
      Removes the given observers from the list of observers
      Specified by:
      removeAll in interface Subject<T,O extends Observer<T>>
      Parameters:
      observers - the observers to be removed
    • setObservable

      public void setObservable(T observable)
      Sets the observable object, which is the object being observed for state changes
      Specified by:
      setObservable in interface Subject<T,O extends Observer<T>>
      Parameters:
      observable - the new observable object
    • updateObservers

      public void updateObservers()
      Notifies all observers of a change in the state of the observable object Each observer's update method is called with the current state of the observable
      Specified by:
      updateObservers in interface Subject<T,O extends Observer<T>>