0% found this document useful (0 votes)
1 views

Program NO 10 dp 80

The document explains the Observer Pattern, a behavioral design pattern that establishes a one-to-many dependency between objects, allowing observers to be notified of changes in a subject's state. It outlines the main components, including Subject, Observer, ConcreteSubject, and ConcreteObserver, and emphasizes the importance of loose coupling. Additionally, it provides a Java implementation of the pattern through a WeatherStation example that demonstrates adding and removing observers and notifying them of temperature changes.

Uploaded by

Palak Goel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Program NO 10 dp 80

The document explains the Observer Pattern, a behavioral design pattern that establishes a one-to-many dependency between objects, allowing observers to be notified of changes in a subject's state. It outlines the main components, including Subject, Observer, ConcreteSubject, and ConcreteObserver, and emphasizes the importance of loose coupling. Additionally, it provides a Java implementation of the pattern through a WeatherStation example that demonstrates adding and removing observers and notifying them of temperature changes.

Uploaded by

Palak Goel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

KAJAL SAH 11232680 4C3

Program NO:10
AIM: Write program to understand The Observer Pattern.

Theory Of Observer Pattern:


The Observer Pattern is a Behavioral design pattern that defines a one-to-many dependency between objects
so that when one object changes state, all its dependents are notified and updated automatically. This pattern
is commonly used to implement distributed event handling systems.
Components of Observer Design Pattern
Below are the main components of Observer Design 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.

MMEC, MULLANA Page | 35


KAJAL SAH 11232680 4C3

CODE USED:
package myPackage;
import java.util.ArrayList;
import java.util.List;
class WeatherStation {
private List<Observer> observers = new ArrayList<>();
private float temperature;

public void addObserver(Observer observer) {


observers.add(observer);
}
public void removeObserver(Observer observer) {
observers.remove(observer);
}
public void setTemperature(float temperature) {
this.temperature = temperature;
notifyObservers();
}
private void notifyObservers() {
for (Observer observer : observers) {
observer.update(temperature);
}
}
}
interface Observer {
void update(float temperature);
}
class CurrentConditionsDisplay implements Observer {
private float temperature;
@Override
public void update(float temperature) {
this.temperature = temperature;
display();
}
private void display() {
System.out.println("Current Conditions Display: Temperature = " + temperature);
}
}
class StatisticsDisplay implements Observer {

MMEC, MULLANA Page | 36


KAJAL SAH 11232680 4C3
private float temperature;
@Override
public void update(float temperature) {
this.temperature = temperature;
display();
}
private void display() {
System.out.println("Statistics Display: Temperature = " + temperature);
}
}
public class ObserverPattern {
public static void main(String[] args) {
WeatherStation weatherStation = new WeatherStation();
CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay();
StatisticsDisplay statisticsDisplay = new StatisticsDisplay();
weatherStation.addObserver(currentConditionsDisplay);
weatherStation.addObserver(statisticsDisplay);
weatherStation.setTemperature(25.5f);
weatherStation.setTemperature(30.0f);
weatherStation.removeObserver(currentConditionsDisplay);
weatherStation.setTemperature(28.0f);
}
}

OUTPUT:

MMEC, MULLANA Page | 37

You might also like