Skip to content

Instantly share code, notes, and snippets.

@chathurangat
Created March 5, 2018 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chathurangat/0dbcde71250301d27c7222fa9a293cab to your computer and use it in GitHub Desktop.
Save chathurangat/0dbcde71250301d27c7222fa9a293cab to your computer and use it in GitHub Desktop.
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