OnJava8-Examples/patterns/visitor/BeeAndFlowers.java

122 lines
2.7 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// patterns/visitor/BeeAndFlowers.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 "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(Renuculus 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 Renuculus 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:
2015-06-15 17:47:35 -07:00
class StringVal implements Visitor {
String s;
@Override
public String toString() { return s; }
@Override
public void visit(Gladiolus g) {
s = "Gladiolus";
}
@Override
public void visit(Renuculus r) {
s = "Renuculus";
}
@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(Renuculus r) {
System.out.println("Bee and Renuculus");
}
@Override
public void visit(Chrysanthemum c) {
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,
Renuculus::new, Chrysanthemum::new);
static final int SZ = flowers.size();
2016-01-25 18:05:55 -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
2015-06-15 17:47:35 -07:00
Renuculus
Chrysanthemum
Renuculus
2015-11-03 12:00:44 -08:00
Chrysanthemum
Chrysanthemum
2016-07-22 14:45:35 -06:00
Chrysanthemum
2015-06-15 17:47:35 -07:00
Renuculus
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
2015-06-15 17:47:35 -07:00
Bee and Renuculus
Bee and Chrysanthemum
Bee and Renuculus
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
2015-06-15 17:47:35 -07:00
Bee and Renuculus
2015-09-07 11:44:36 -06:00
*/