OnJava8-Examples/patterns/observer/ObservedFlower.java

123 lines
3.3 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// patterns/observer/ObservedFlower.java
2016-12-30 17:23:13 -08:00
// (c)2017 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// Demonstration of "Observer" pattern
2016-07-28 12:48:23 -06:00
// {java patterns.observer.ObservedFlower}
2015-06-15 17:47:35 -07:00
package patterns.observer;
import java.util.*;
class Flower {
private boolean isOpen;
2015-11-03 12:00:44 -08:00
private boolean alreadyOpen;
private boolean alreadyClosed;
2015-06-15 17:47:35 -07:00
public Flower() { isOpen = false; }
2015-11-03 12:00:44 -08:00
OpenNotifier opening = new OpenNotifier();
CloseNotifier closing = new CloseNotifier();
2015-06-15 17:47:35 -07:00
public void open() { // Opens its petals
isOpen = true;
2015-11-03 12:00:44 -08:00
opening.notifyObservers();
alreadyClosed = false;
2015-06-15 17:47:35 -07:00
}
public void close() { // Closes its petals
isOpen = false;
2015-11-03 12:00:44 -08:00
closing.notifyObservers();
alreadyOpen = false;
2015-06-15 17:47:35 -07:00
}
2015-11-03 12:00:44 -08:00
class OpenNotifier extends Observable {
2015-06-15 17:47:35 -07:00
@Override
public void notifyObservers() {
if(isOpen && !alreadyOpen) {
setChanged();
super.notifyObservers();
alreadyOpen = true;
}
}
}
2015-11-03 12:00:44 -08:00
class CloseNotifier extends Observable{
2015-06-15 17:47:35 -07:00
@Override
public void notifyObservers() {
if(!isOpen && !alreadyClosed) {
setChanged();
super.notifyObservers();
alreadyClosed = true;
}
}
}
}
class Bee {
private String name;
public Bee(String nm) { name = nm; }
2015-11-03 12:00:44 -08:00
// Observe openings:
2015-06-15 17:47:35 -07:00
public Observer openObserver() {
2015-11-03 12:00:44 -08:00
return (ob, a) -> System.out.println(
"Bee " + name + "'s breakfast time!");
2015-06-15 17:47:35 -07:00
}
2015-11-03 12:00:44 -08:00
// Observe closings:
2015-06-15 17:47:35 -07:00
public Observer closeObserver() {
2015-11-03 12:00:44 -08:00
return (ob, a) -> System.out.println(
"Bee " + name + "'s bed time!");
2015-06-15 17:47:35 -07:00
}
}
class Hummingbird {
private String name;
public Hummingbird(String nm) { name = nm; }
public Observer openObserver() {
2015-11-03 12:00:44 -08:00
return (ob, a) -> System.out.println(
"Hummingbird " + name +
"'s breakfast time!");
2015-06-15 17:47:35 -07:00
}
public Observer closeObserver() {
2015-11-03 12:00:44 -08:00
return (ob, a) -> System.out.println(
"Hummingbird " + name + "'s bed time!");
2015-06-15 17:47:35 -07:00
}
}
public class ObservedFlower {
2016-01-25 18:05:55 -08:00
public static void main(String[] args) {
2015-06-15 17:47:35 -07:00
Flower f = new Flower();
Bee
ba = new Bee("A"),
bb = new Bee("B");
Hummingbird
ha = new Hummingbird("A"),
hb = new Hummingbird("B");
2015-11-03 12:00:44 -08:00
f.opening.addObserver(ha.openObserver());
f.opening.addObserver(hb.openObserver());
f.opening.addObserver(ba.openObserver());
f.opening.addObserver(bb.openObserver());
f.closing.addObserver(ha.closeObserver());
f.closing.addObserver(hb.closeObserver());
f.closing.addObserver(ba.closeObserver());
f.closing.addObserver(bb.closeObserver());
2015-06-15 17:47:35 -07:00
// Hummingbird B decides to sleep in:
2015-11-03 12:00:44 -08:00
f.opening.deleteObserver(hb.openObserver());
2015-06-15 17:47:35 -07:00
// A change that interests observers:
f.open();
f.open(); // It's already open, no change.
// Bee A doesn't want to go to bed:
2015-11-03 12:00:44 -08:00
f.closing.deleteObserver(ba.closeObserver());
2015-06-15 17:47:35 -07:00
f.close();
f.close(); // It's already closed; no change
2015-11-03 12:00:44 -08:00
f.opening.deleteObservers();
2015-06-15 17:47:35 -07:00
f.open();
f.close();
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
Bee B's breakfast time!
Bee A's breakfast time!
2015-11-03 12:00:44 -08:00
Hummingbird B's breakfast time!
2015-06-15 17:47:35 -07:00
Hummingbird A's breakfast time!
Bee B's bed time!
2015-11-03 12:00:44 -08:00
Bee A's bed time!
2015-06-15 17:47:35 -07:00
Hummingbird B's bed time!
Hummingbird A's bed time!
Bee B's bed time!
2015-11-03 12:00:44 -08:00
Bee A's bed time!
2015-06-15 17:47:35 -07:00
Hummingbird B's bed time!
Hummingbird A's bed time!
2015-09-07 11:44:36 -06:00
*/