Created
March 5, 2018 19:28
-
-
Save chathurangat/0dbcde71250301d27c7222fa9a293cab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Subject { | |
private String status; | |
private List<Observer> observers = new ArrayList<Observer>(); | |
public String getStatus() { | |
return status; | |
} | |
public void setStatus(String status) { | |
System.out.println("changing the status to [" + status + "] "); | |
this.status = status; | |
this.notifyObservers(); | |
} | |
public void subscribe(Observer observer) { | |
observers.add(observer); | |
} | |
public void unsubscribe(Observer observer) { | |
observers.remove(observer); | |
} | |
private void notifyObservers() { | |
System.out.println("notifying observers"); | |
observers.stream().forEach((observer) -> { | |
observer.receiveUpdate(this); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment