Observers
Directory - A Price Watching Application Using the Observer Design PatternThe Observers directory demonstrates the application of the Observer Design Pattern within the context of a price-watching application for a mall shopping scenario. This application simulates a customer trying to monitor sales events at multiple stores in order to buy products at the best prices. The Observer pattern allows customers (observers) to receive updates from stores (subjects) whenever a relevant sale event occurs, such as a product going on sale or coming back in stock.
In this directory, the Observer pattern is used to notify customers of various sales events, and customers can react to these notifications by purchasing products or waiting for better deals. This setup not only demonstrates how the Observer pattern works but also provides a real-world example of how this design pattern can be applied in interactive applications.
The Observer Design Pattern is a behavioral pattern where an object (the subject) maintains a list of its dependents (the observers) and notifies them of any state changes, usually by calling one of their methods. This pattern is commonly used for implementing distributed event handling systems.
In this project, stores are the subjects, and customers are the observers. When an event like a sale starts, a product restocking, or an out-of-stock situation occurs, the store notifies all of its registered customers about the event.
StoreEvent
)In this application, there are different types of sale events that customers may want to observe. These events are encapsulated as concrete classes implementing the StoreEvent
interface.
BackInStockEvent
: This event occurs when a product that was previously out of stock is now available for purchase.OutOfStockEvent
: This event occurs when a product is sold out (i.e., no stock left).PurchaseEvent
: This event occurs when a product is purchased from a store.SaleEndEvent
: This event signals that a sale on a particular product has ended.SaleStartEvent
: This event signals the start of a sale on a product.Each event class encapsulates a Product
and a Store
, and implements the StoreEvent
interface. These events are immutable and notify observers (customers) about the changes.
ProductImpl
)The ProductImpl
class represents a product in the store. Each product has a name and a base price. Products may also have additional properties, such as sale prices or availability (whether they’re in stock or not).
StoreImpl
)The StoreImpl
class represents a store in the mall. It maintains a list of products and also tracks the customers who are observing the store for sale events. The store is the subject in the observer pattern and notifies its observers whenever a relevant event occurs.
startSale()
: Starts a sale for a product and notifies customers.endSale()
: Ends a sale for a product and notifies customers.restockProduct()
: Restocks a product and notifies customers if it is back in stock.purchaseProduct()
: Processes a purchase, deducts stock, and notifies customers of the purchase event.CustomerImpl
)The CustomerImpl
class represents a customer who is observing stores for sales events. Customers are observers in the observer pattern and respond to events by purchasing products or simply observing sales announcements.
ReceiptItem
object is created to record each purchase.update()
method handles incoming sale events, such as when a product goes on sale or comes back in stock. It prints messages to the console to inform the customer about the event.Main
)The Main
class is responsible for simulating the shopping experience in the mall. It initializes stores, products, and customers. It also registers customers as observers of the stores, so they can be notified of events.
update()
method, which then processes the event (e.g., prints a message to the console and possibly makes a purchase).BackInStockEvent
, SaleStartEvent
, etc.) encapsulates the product and the store where the event occurred. When an event happens, it is passed to all observers who are interested in that store.update()
method.Observers
DirectoryThe Observer pattern is an essential part of this price-watching application. The design ensures that:
By using the Observer pattern, this directory demonstrates how stores can notify multiple customers about important events without the need for tight coupling between them. Customers can be added or removed from the notification list without affecting the store’s functionality.
This implementation highlights the advantages of the Observer pattern:
By using the Observer pattern, this directory provides a simple, scalable, and modular implementation for a shopping trip simulation that tracks sales and customer behavior.