Create a simple Observable class

Difficulty: EASYCategory: LLDFrequency: 1Asked On 2025-02-27

Problem Statement

Designing a simple Observable system that implements the Observer design pattern. This system should allow multiple observers to subscribe to an observable source and get notified when the state of the source changes.


Requirements:

1.Implement an Observable class that:
•Maintains a list of Observer instances.
•Allows observers to register and unregister.
•Notifies all registered observers when its internal state changes.
2.Implement an Observer interface (or abstract class) that:
•Defines a notify(data) method which is called when the observable changes.
3.Demonstrate the pattern by:
•Creating a WeatherStation class (as the Observable) which updates temperature.
•Creating two observers, MobileDisplay and WebDashboard, which print the updated temperature.


Solution

Comments