forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeeAndFlowers.java
More file actions
115 lines (106 loc) · 2.71 KB
/
BeeAndFlowers.java
File metadata and controls
115 lines (106 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// patterns/visitor/BeeAndFlowers.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Demonstration of the Visitor pattern.
// {java patterns.visitor.BeeAndFlowers}
package patterns.visitor;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
interface Visitor {
void visit(Gladiolus g);
void visit(Ranunculus r);
void visit(Chrysanthemum c);
}
// The Flower hierarchy cannot be changed:
interface Flower {
void accept(Visitor v);
}
class Gladiolus implements Flower {
@Override
public void accept(Visitor v) { v.visit(this);}
}
class Ranunculus implements Flower {
@Override
public void accept(Visitor v) { v.visit(this);}
}
class Chrysanthemum implements Flower {
@Override
public void accept(Visitor v) { v.visit(this);}
}
// Add the ability to produce a String:
class StringVal implements Visitor {
private String s;
@Override public String toString() { return s; }
@Override public void visit(Gladiolus g) {
s = "Gladiolus";
}
@Override public void visit(Ranunculus r) {
s = "Ranunculus";
}
@Override public void visit(Chrysanthemum c) {
s = "Chrysanthemum";
}
}
// Add the ability to do "Bee" activities:
class Bee implements Visitor {
@Override public void visit(Gladiolus g) {
System.out.println("Bee and Gladiolus");
}
@Override public void visit(Ranunculus r) {
System.out.println("Bee and Ranunculus");
}
@Override public void visit(Chrysanthemum c) {
System.out.println("Bee and Chrysanthemum");
}
}
class FlowerFactory {
static List<Supplier<Flower>> flowers =
Arrays.asList(Gladiolus::new,
Ranunculus::new, Chrysanthemum::new);
static final int SZ = flowers.size();
private static SplittableRandom rand =
new SplittableRandom(47);
public static Flower newFlower() {
return flowers.get(rand.nextInt(SZ)).get();
}
}
public class BeeAndFlowers {
public static void main(String[] args) {
List<Flower> flowers =
Stream.generate(FlowerFactory::newFlower)
.limit(10)
.collect(Collectors.toList());
StringVal sval = new StringVal();
flowers.forEach(f -> {
f.accept(sval);
System.out.println(sval);
});
// Perform "Bee" operation on all Flowers:
Bee bee = new Bee();
flowers.forEach(f -> f.accept(bee));
}
}
/* Output:
Gladiolus
Chrysanthemum
Gladiolus
Ranunculus
Chrysanthemum
Ranunculus
Chrysanthemum
Chrysanthemum
Chrysanthemum
Ranunculus
Bee and Gladiolus
Bee and Chrysanthemum
Bee and Gladiolus
Bee and Ranunculus
Bee and Chrysanthemum
Bee and Ranunculus
Bee and Chrysanthemum
Bee and Chrysanthemum
Bee and Chrysanthemum
Bee and Ranunculus
*/