Software-Design-Patterns

Observers Directory - A Price Watching Application Using the Observer Design Pattern

The 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.

Overview of the Observer Pattern

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.

Key Concepts:

  1. Subject: The object that holds a list of observers and notifies them about changes.
  2. Observer: The object that receives updates from the subject when an event occurs.
  3. Event: Represents a specific occurrence that the observers are interested in (e.g., a sale starting or an item going out of stock).

Key Components of the Observer Pattern in the Application

1. Event Classes (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.

Types of Events:

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.

2. Product (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).

Key Features:

3. Store (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.

Key Features:

4. Customer (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.

Key Features:

5. Main Class (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.

Key Features:

How the Observer Pattern Works

  1. Store (Subject): A store contains a list of observers (customers) and notifies them whenever a relevant event occurs (e.g., a product going on sale, restocking, or being sold out).
  2. Customer (Observer): A customer listens for store events. When a store announces a sale, the customer receives the notification through the update() method, which then processes the event (e.g., prints a message to the console and possibly makes a purchase).
  3. Event: Each type of event (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.

Example Event Flow:

Summary of the Observers Directory

The 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.