Skip to content

Instantly share code, notes, and snippets.

@DavidMellul
Created July 8, 2018 11:39
Show Gist options
  • Save DavidMellul/1652bd65947a38f1792d00e7000312c8 to your computer and use it in GitHub Desktop.
Save DavidMellul/1652bd65947a38f1792d00e7000312c8 to your computer and use it in GitHub Desktop.
import java.util.Observer;
import java.util.Observable;
class Margherita extends Observable {
private boolean isOutOfDate;
Margherita() {
this.isOutOfDate = false;
}
public void setOutOfDate(boolean outOfDate) {
this.isOutOfDate = outOfDate;
// Tell Java : I have changed
this.setChanged();
// Tell Java : Notify my observer(s)
this.notifyObservers("Pizza out of date");
}
}
class Chef implements Observer {
Chef() {
System.out.println("Hello, I'm Mario from JavaPizza");
}
// Called when a Chef's Observable has changed
public void update(Observable pizza, Object details) {
System.out.println("Mamamia! Something happened");
System.out.println("Details: "+details);
}
}
public class PizzaBusiness {
public static void main(String[] args) {
// Mario is the Chef running the Pizza Business
Chef mario = new Chef();
// Margherita is one of the pizzas Mario sells
Margherita pizza = new Margherita();
// Mario will always check that his pizza isn't out of date
// Mario will observe it
pizza.addObserver(mario);
// Imagine a logic based on time
// .....
pizza.setOutOfDate(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment