Class MaterialEventPublisher
java.lang.Object
com.university.bookstore.observer.MaterialEventPublisher
- All Implemented Interfaces:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddObserver(MaterialObserver observer) Adds an observer to receive material events.voidClears all observers.intGets the number of registered observers.Gets a list of all registered observers.<T extends MaterialObserver>
List<T> getObserversOfType(Class<T> observerType) Gets observers of a specific type.booleanChecks if any observers are registered.booleanhasObserver(MaterialObserver observer) Checks if a specific observer is registered.voidnotifyObservers(MaterialEvent event) Notifies all observers of a material event.voidpublishEvent(MaterialEvent event) Publishes a custom material event.voidpublishMaterialAdded(Material material) Publishes a material added event.voidpublishPriceChanged(Material material, double oldPrice, double newPrice) Publishes a price changed event.booleanremoveObserver(MaterialObserver observer) Removes an observer from receiving material events.toString()
-
Constructor Details
-
MaterialEventPublisher
public MaterialEventPublisher()Creates a new material event publisher.
-
-
Method Details
-
addObserver
Description copied from interface:MaterialSubjectAdds an observer to receive material events.- Specified by:
addObserverin interfaceMaterialSubject- Parameters:
observer- the observer to add
-
removeObserver
Description copied from interface:MaterialSubjectRemoves an observer from receiving material events.- Specified by:
removeObserverin interfaceMaterialSubject- Parameters:
observer- the observer to remove- Returns:
- true if the observer was removed, false if not found
-
notifyObservers
Description copied from interface:MaterialSubjectNotifies all observers of a material event.- Specified by:
notifyObserversin interfaceMaterialSubject- Parameters:
event- the event to broadcast
-
getObserverCount
public int getObserverCount()Description copied from interface:MaterialSubjectGets the number of registered observers.- Specified by:
getObserverCountin interfaceMaterialSubject- Returns:
- the observer count
-
hasNoObservers
public boolean hasNoObservers()Description copied from interface:MaterialSubjectChecks if any observers are registered.- Specified by:
hasNoObserversin interfaceMaterialSubject- Returns:
- true if no observers are registered
-
clearObservers
public void clearObservers()Description copied from interface:MaterialSubjectClears all observers.- Specified by:
clearObserversin interfaceMaterialSubject
-
publishMaterialAdded
Publishes a material added event.- Parameters:
material- the material that was added
-
publishPriceChanged
Publishes a price changed event.- Parameters:
material- the material whose price changedoldPrice- the previous pricenewPrice- the new price
-
publishEvent
Publishes a custom material event.- Parameters:
event- the event to publish
-
getObservers
Gets a list of all registered observers.- Returns:
- list of observers (defensive copy)
-
hasObserver
Checks if a specific observer is registered.- Parameters:
observer- the observer to check- Returns:
- true if the observer is registered
-
getObserversOfType
Gets observers of a specific type.- Parameters:
observerType- the type of observers to find- Returns:
- list of observers of the specified type
-
toString
-