OnJava8-Examples/patterns/visitor/BeeAndFlowers.java

98 lines
2.2 KiB
Java
Raw Normal View History

2015-05-05 11:20:13 -07:00
//: patterns/visitor/BeeAndFlowers.java
// Demonstration of "visitor" pattern.
package patterns.visitor;
import java.util.*;
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:
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 {
public static Flower newFlower() {
switch((int)(Math.random() * 3)) {
default:
case 0: return new Gladiolus();
case 1: return new Renuculus();
case 2: return new Chrysanthemum();
}
}
}
public class BeeAndFlowers {
public static void main(String args[]) {
ArrayList flowers = new ArrayList();
for(int i = 0; i < 10; i++)
flowers.add(FlowerFactory.newFlower());
// It's almost as if I had added a function
// to produce a Flower string representation:
StringVal sval = new StringVal();
Iterator it = flowers.iterator();
while(it.hasNext()) {
((Flower)it.next()).accept(sval);
System.out.println(sval);
}
// Perform "Bee" operation on all Flowers:
Bee bee = new Bee();
it = flowers.iterator();
while(it.hasNext())
((Flower)it.next()).accept(bee);
}
} ///:~