OnJava8-Examples/patterns/visitor/BeeAndFlowers.java

116 lines
2.7 KiB
Java
Raw Permalink Normal View History

2015-09-07 11:44:36 -06:00
// patterns/visitor/BeeAndFlowers.java
// (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.
// Demonstration of the Visitor pattern.
2016-07-28 12:48:23 -06:00
// {java patterns.visitor.BeeAndFlowers}
2015-06-15 17:47:35 -07:00
package patterns.visitor;
import java.util.*;
2015-11-03 12:00:44 -08:00
import java.util.function.*;
import java.util.stream.*;
2015-06-15 17:47:35 -07:00
interface Visitor {
void visit(Gladiolus g);
void visit(Ranunculus r);
2015-06-15 17:47:35 -07:00
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 {
2015-06-15 17:47:35 -07:00
@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:
2015-06-15 17:47:35 -07:00
class StringVal implements Visitor {
private String s;
@Override public String toString() { return s; }
@Override public void visit(Gladiolus g) {
2015-06-15 17:47:35 -07:00
s = "Gladiolus";
}
@Override public void visit(Ranunculus r) {
s = "Ranunculus";
2015-06-15 17:47:35 -07:00
}
@Override public void visit(Chrysanthemum c) {
2015-06-15 17:47:35 -07:00
s = "Chrysanthemum";
}
}
// Add the ability to do "Bee" activities:
class Bee implements Visitor {
@Override public void visit(Gladiolus g) {
2015-06-15 17:47:35 -07:00
System.out.println("Bee and Gladiolus");
}
@Override public void visit(Ranunculus r) {
System.out.println("Bee and Ranunculus");
2015-06-15 17:47:35 -07:00
}
@Override public void visit(Chrysanthemum c) {
2015-06-15 17:47:35 -07:00
System.out.println("Bee and Chrysanthemum");
}
}
class FlowerFactory {
2015-11-03 12:00:44 -08:00
static List<Supplier<Flower>> flowers =
Arrays.asList(Gladiolus::new,
Ranunculus::new, Chrysanthemum::new);
static final int SZ = flowers.size();
2017-01-09 14:26:12 -08:00
private static SplittableRandom rand =
new SplittableRandom(47);
2015-06-15 17:47:35 -07:00
public static Flower newFlower() {
return flowers.get(rand.nextInt(SZ)).get();
2015-06-15 17:47:35 -07:00
}
}
public class BeeAndFlowers {
2016-01-25 18:05:55 -08:00
public static void main(String[] args) {
2015-11-03 12:00:44 -08:00
List<Flower> flowers =
Stream.generate(FlowerFactory::newFlower)
.limit(10)
.collect(Collectors.toList());
2015-06-15 17:47:35 -07:00
StringVal sval = new StringVal();
2015-11-03 12:00:44 -08:00
flowers.forEach(f -> {
f.accept(sval);
2015-06-15 17:47:35 -07:00
System.out.println(sval);
2015-11-03 12:00:44 -08:00
});
2015-06-15 17:47:35 -07:00
// Perform "Bee" operation on all Flowers:
Bee bee = new Bee();
2015-11-03 12:00:44 -08:00
flowers.forEach(f -> f.accept(bee));
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2016-07-22 14:45:35 -06:00
Gladiolus
2015-06-15 17:47:35 -07:00
Chrysanthemum
2016-07-22 14:45:35 -06:00
Gladiolus
Ranunculus
2015-06-15 17:47:35 -07:00
Chrysanthemum
Ranunculus
2015-11-03 12:00:44 -08:00
Chrysanthemum
Chrysanthemum
2016-07-22 14:45:35 -06:00
Chrysanthemum
Ranunculus
2016-07-22 14:45:35 -06:00
Bee and Gladiolus
2015-06-15 17:47:35 -07:00
Bee and Chrysanthemum
2016-07-22 14:45:35 -06:00
Bee and Gladiolus
Bee and Ranunculus
2015-06-15 17:47:35 -07:00
Bee and Chrysanthemum
Bee and Ranunculus
2015-11-03 12:00:44 -08:00
Bee and Chrysanthemum
Bee and Chrysanthemum
2016-07-22 14:45:35 -06:00
Bee and Chrysanthemum
Bee and Ranunculus
2015-09-07 11:44:36 -06:00
*/