Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observer design pattern implementation #193

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Observer/Observer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Observer;

public interface Observer {
void update(String weather);
}
15 changes: 15 additions & 0 deletions Observer/PhoneDisplay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package Observer;

public class PhoneDisplay implements Observer {
private String weather;

@Override
public void update(String weather) {
this.weather = weather;
display();
}

private void display() {
System.out.println("Phone Display: Weather updated - " + weather);
}
}
7 changes: 7 additions & 0 deletions Observer/Subject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Observer;

public interface Subject {
void addObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObservers();
}
16 changes: 16 additions & 0 deletions Observer/TVDisplay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package Observer;

class TVDisplay implements Observer {
private String weather;

@Override
public void update(String weather) {
this.weather = weather;
display();
}

private void display() {
System.out.println("TV Display: Weather updated - " + weather);
}
}

18 changes: 18 additions & 0 deletions Observer/WeatherApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package Observer;

public class WeatherApp {
public static void main(String[] args) {
WeatherStation weatherStation = new WeatherStation();

Observer phoneDisplay = new PhoneDisplay();
Observer tvDisplay = new TVDisplay();

weatherStation.addObserver(phoneDisplay);
weatherStation.addObserver(tvDisplay);


weatherStation.setWeather("Sunny");


}
}
31 changes: 31 additions & 0 deletions Observer/WeatherStation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package Observer;

import java.util.ArrayList;
import java.util.List;

public class WeatherStation implements Subject {
private List<Observer> observers = new ArrayList<>();
private String weather;

@Override
public void addObserver(Observer observer) {
observers.add(observer);
}

@Override
public void removeObserver(Observer observer) {
observers.remove(observer);
}

@Override
public void notifyObservers() {
for (Observer observer : observers) {
observer.update(weather);
}
}

public void setWeather(String newWeather) {
this.weather = newWeather;
notifyObservers();
}
}
73 changes: 73 additions & 0 deletions Observer/WeatherStationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package Observer;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class WeatherStationTest {
private WeatherStation weatherStation;
private TestObserver phoneDisplay;
private TestObserver tvDisplay;

@BeforeEach
void setUp() {
weatherStation = new WeatherStation();
phoneDisplay = new TestObserver();
tvDisplay = new TestObserver();
}

@Test
void testAddObserverAndNotifyObservers() {
// Add observers
weatherStation.addObserver(phoneDisplay);
weatherStation.addObserver(tvDisplay);

// Simulate weather change
weatherStation.setWeather("Rainy");

// Verify that all observers received the update
assertEquals("Rainy", phoneDisplay.getWeather());
assertEquals("Rainy", tvDisplay.getWeather());
}

@Test
void testRemoveObserver() {
// Add observers
weatherStation.addObserver(phoneDisplay);
weatherStation.addObserver(tvDisplay);

// Remove one observer
weatherStation.removeObserver(phoneDisplay);

// Simulate weather change
weatherStation.setWeather("Cloudy");

// Verify only the remaining observer received the update
assertEquals(null, phoneDisplay.getWeather());
assertEquals("Cloudy", tvDisplay.getWeather());
}

@Test
void testNoObservers() {
// Simulate weather change without any observers
weatherStation.setWeather("Windy");

// No exceptions should occur, nothing to verify
}

// Custom TestObserver class
private static class TestObserver implements Observer {
private String weather;

@Override
public void update(String weather) {
this.weather = weather;
}

public String getWeather() {
return weather;
}
}
}

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ It is very easy to contribute, you may follow these steps -
99.[RotateLinkedList](https://github.com/PrajaktaSathe/Java/blob/main/Programs/RotateLinkedList.java)-Program to demo rotating a linked list
100. [ReverseString](https://github.com/PrajaktaSathe/Java/blob/main/ReverseString.java) -Program to reverse a String using the java method substring.
101.[Overriding](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Overriding.java)-Program to demo overriding in java
102. [Observer-pattern](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Observer/WeatherApp.java)- Observer design pattern in java


# Contributors -
## A big thank you to all our contributors!!!
Expand Down