Class MaterialEventPublisher

java.lang.Object
com.university.bookstore.observer.MaterialEventPublisher
All Implemented Interfaces:
MaterialSubject

public class MaterialEventPublisher extends Object implements MaterialSubject
Concrete implementation of MaterialSubject that publishes material events to observers. Provides thread-safe event publishing and observer management.

This class demonstrates the Observer pattern by managing a list of observers and broadcasting events to all registered observers when material events occur.

Since:
2024-09-15
Version:
3.0
Author:
Navid Mohaghegh
  • Constructor Details

    • MaterialEventPublisher

      public MaterialEventPublisher()
      Creates a new material event publisher.
  • Method Details

    • addObserver

      public void addObserver(MaterialObserver observer)
      Description copied from interface: MaterialSubject
      Adds an observer to receive material events.
      Specified by:
      addObserver in interface MaterialSubject
      Parameters:
      observer - the observer to add
    • removeObserver

      public boolean removeObserver(MaterialObserver observer)
      Description copied from interface: MaterialSubject
      Removes an observer from receiving material events.
      Specified by:
      removeObserver in interface MaterialSubject
      Parameters:
      observer - the observer to remove
      Returns:
      true if the observer was removed, false if not found
    • notifyObservers

      public void notifyObservers(MaterialEvent event)
      Description copied from interface: MaterialSubject
      Notifies all observers of a material event.
      Specified by:
      notifyObservers in interface MaterialSubject
      Parameters:
      event - the event to broadcast
    • getObserverCount

      public int getObserverCount()
      Description copied from interface: MaterialSubject
      Gets the number of registered observers.
      Specified by:
      getObserverCount in interface MaterialSubject
      Returns:
      the observer count
    • hasNoObservers

      public boolean hasNoObservers()
      Description copied from interface: MaterialSubject
      Checks if any observers are registered.
      Specified by:
      hasNoObservers in interface MaterialSubject
      Returns:
      true if no observers are registered
    • clearObservers

      public void clearObservers()
      Description copied from interface: MaterialSubject
      Clears all observers.
      Specified by:
      clearObservers in interface MaterialSubject
    • publishMaterialAdded

      public void publishMaterialAdded(Material material)
      Publishes a material added event.
      Parameters:
      material - the material that was added
    • publishPriceChanged

      public void publishPriceChanged(Material material, double oldPrice, double newPrice)
      Publishes a price changed event.
      Parameters:
      material - the material whose price changed
      oldPrice - the previous price
      newPrice - the new price
    • publishEvent

      public void publishEvent(MaterialEvent event)
      Publishes a custom material event.
      Parameters:
      event - the event to publish
    • getObservers

      public List<MaterialObserver> getObservers()
      Gets a list of all registered observers.
      Returns:
      list of observers (defensive copy)
    • hasObserver

      public boolean hasObserver(MaterialObserver observer)
      Checks if a specific observer is registered.
      Parameters:
      observer - the observer to check
      Returns:
      true if the observer is registered
    • getObserversOfType

      public <T extends MaterialObserver> List<T> getObserversOfType(Class<T> observerType)
      Gets observers of a specific type.
      Parameters:
      observerType - the type of observers to find
      Returns:
      list of observers of the specified type
    • toString

      public String toString()
      Overrides:
      toString in class Object