2015-09-07 11:44:36 -06:00
|
|
|
// patterns/observer/ObservedFlower.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 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.
|
2021-03-04 16:15:04 -07:00
|
|
|
// Demonstration of the 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.*;
|
2021-03-04 16:15:04 -07:00
|
|
|
import java.util.function.*;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
2020-10-07 17:06:42 -06:00
|
|
|
@SuppressWarnings("deprecation")
|
2015-06-15 17:47:35 -07:00
|
|
|
class Flower {
|
2021-03-04 16:15:04 -07:00
|
|
|
private boolean isOpen = false;
|
|
|
|
private boolean alreadyOpen = false;
|
|
|
|
private boolean alreadyClosed = false;
|
|
|
|
Observable opening = new Observable() {
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public void notifyObservers() {
|
2015-06-15 17:47:35 -07:00
|
|
|
if(isOpen && !alreadyOpen) {
|
|
|
|
setChanged();
|
|
|
|
super.notifyObservers();
|
|
|
|
alreadyOpen = true;
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 16:15:04 -07:00
|
|
|
};
|
|
|
|
Observable closing = new Observable() {
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public void notifyObservers() {
|
2015-06-15 17:47:35 -07:00
|
|
|
if(!isOpen && !alreadyClosed) {
|
|
|
|
setChanged();
|
|
|
|
super.notifyObservers();
|
|
|
|
alreadyClosed = true;
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 16:15:04 -07:00
|
|
|
};
|
|
|
|
public void open() { // Opens its petals
|
|
|
|
isOpen = true;
|
|
|
|
opening.notifyObservers();
|
|
|
|
alreadyClosed = false;
|
|
|
|
}
|
|
|
|
public void close() { // Closes its petals
|
|
|
|
isOpen = false;
|
|
|
|
closing.notifyObservers();
|
|
|
|
alreadyOpen = false;
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 17:06:42 -06:00
|
|
|
@SuppressWarnings("deprecation")
|
2015-06-15 17:47:35 -07:00
|
|
|
class Bee {
|
2021-03-04 16:15:04 -07:00
|
|
|
private String id;
|
|
|
|
Bee(String name) { id = name; }
|
2015-11-03 12:00:44 -08:00
|
|
|
// Observe openings:
|
2021-03-04 16:15:04 -07:00
|
|
|
public final Observer whenOpened = (ob, a) ->
|
|
|
|
System.out.println(
|
|
|
|
"Bee " + id + "'s breakfast time!");
|
2015-11-03 12:00:44 -08:00
|
|
|
// Observe closings:
|
2021-03-04 16:15:04 -07:00
|
|
|
public final Observer whenClosed = (ob, a) ->
|
|
|
|
System.out.println(
|
|
|
|
"Bee " + id + "'s bed time!");
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:06:42 -06:00
|
|
|
@SuppressWarnings("deprecation")
|
2015-06-15 17:47:35 -07:00
|
|
|
class Hummingbird {
|
2021-03-04 16:15:04 -07:00
|
|
|
private String id;
|
|
|
|
Hummingbird(String name) { id = name; }
|
|
|
|
public final Observer whenOpened = (ob, a) ->
|
|
|
|
System.out.println("Hummingbird " +
|
|
|
|
id + "'s breakfast time!");
|
|
|
|
public final Observer whenClosed = (ob, a) ->
|
|
|
|
System.out.println("Hummingbird " +
|
|
|
|
id + "'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");
|
2021-03-04 16:15:04 -07:00
|
|
|
f.opening.addObserver(ha.whenOpened);
|
|
|
|
f.opening.addObserver(hb.whenOpened);
|
|
|
|
f.opening.addObserver(ba.whenOpened);
|
|
|
|
f.opening.addObserver(bb.whenOpened);
|
|
|
|
f.closing.addObserver(ha.whenClosed);
|
|
|
|
f.closing.addObserver(hb.whenClosed);
|
|
|
|
f.closing.addObserver(ba.whenClosed);
|
|
|
|
f.closing.addObserver(bb.whenClosed);
|
|
|
|
// Hummingbird B decides to sleep in.
|
|
|
|
// Removing whenOpened stops open updates.
|
|
|
|
f.opening.deleteObserver(hb.whenOpened);
|
2015-06-15 17:47:35 -07:00
|
|
|
// A change that interests observers:
|
|
|
|
f.open();
|
2021-03-04 16:15:04 -07:00
|
|
|
f.open(); // No effect: it's already open.
|
|
|
|
System.out.println("---------------");
|
|
|
|
// Bee A doesn't want to go to bed.
|
|
|
|
// Removing whenClosed stops close updates.
|
|
|
|
f.closing.deleteObserver(ba.whenClosed);
|
2015-06-15 17:47:35 -07:00
|
|
|
f.close();
|
2021-03-04 16:15:04 -07:00
|
|
|
System.out.println("+++++++++++++++");
|
|
|
|
f.close(); // No effect: it's already closed.
|
|
|
|
System.out.println("===============");
|
2015-11-03 12:00:44 -08:00
|
|
|
f.opening.deleteObservers();
|
2021-03-04 16:15:04 -07:00
|
|
|
f.open(); // No observers to update.
|
|
|
|
System.out.println("###############");
|
|
|
|
f.close(); // Close observers are still there.
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
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!
|
|
|
|
Hummingbird A's breakfast time!
|
2021-03-04 16:15:04 -07:00
|
|
|
---------------
|
2015-06-15 17:47:35 -07:00
|
|
|
Bee B's bed time!
|
|
|
|
Hummingbird B's bed time!
|
|
|
|
Hummingbird A's bed time!
|
2021-03-04 16:15:04 -07:00
|
|
|
+++++++++++++++
|
|
|
|
===============
|
|
|
|
###############
|
2015-06-15 17:47:35 -07:00
|
|
|
Bee B's bed time!
|
|
|
|
Hummingbird B's bed time!
|
|
|
|
Hummingbird A's bed time!
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|