Program NO 10 dp 80
Program NO 10 dp 80
Program NO:10
AIM: Write program to understand The Observer Pattern.
• Subject: The subject is the object that maintains a list of its dependents, also known as observers.It
provides an interface for attaching, detaching, and notifying observers.
• Observer: The observer is the interface that defines the update method, which is called by the
subject to notify the observer of a change in the subject's state.Observers register themselves with
the subject to receive updates.
• ConcreteSubject: The concrete subject is a concrete implementation of the subject interface. It
maintains the state of interest and notifies observers when a change occurs.
• ConcreteObserver: The concrete observer is a concrete implementation of the observer interface. It
registers itself with a subject to receive updates and implements the update method to respond to
changes in the subject's state.
• Observer List:The subject maintains a list (or collection) of observers, allowing multiple observers
to be notified of changes.
• Loose Coupling: The Observer Pattern promotes loose coupling between the subject and its
observers. The subject doesn't need to know the details of its observers, and observers can be added
or removed without affecting the subject.
CODE USED:
package myPackage;
import java.util.ArrayList;
import java.util.List;
class WeatherStation {
private List<Observer> observers = new ArrayList<>();
private float temperature;
OUTPUT: