86 lines
2.5 KiB
Java
Raw Normal View History

2015-05-05 11:20:13 -07:00
//: patterns/trash/Trash.java
// Base class for Trash recycling examples.
package patterns.trash;
import java.util.*;
import java.lang.reflect.*;
public abstract class Trash {
private double weight;
Trash(double wt) { weight = wt; }
Trash() {}
public abstract double value();
public double weight() { return weight; }
// Sums the value of Trash in a bin:
public static void sumValue(ArrayList bin) {
Iterator e = bin.iterator();
double val = 0.0f;
while(e.hasNext()) {
// One kind of RTTI:
// A dynamically-checked cast
Trash t = (Trash)e.next();
val += t.weight() * t.value();
System.out.println(
"weight of " +
// Using RTTI to get type
// information about the class:
t.getClass().getName() +
" = " + t.weight());
}
System.out.println("Total value = " + val);
}
// Remainder of class provides support for
// prototyping:
public static class PrototypeNotFoundException
extends Exception {}
public static class CannotCreateTrashException
extends Exception {}
private static ArrayList trashTypes =
new ArrayList();
public static Trash factory(Info info)
throws PrototypeNotFoundException,
CannotCreateTrashException {
2015-05-05 14:05:39 -07:00
for (Object trashType : trashTypes) {
2015-05-05 11:20:13 -07:00
// Somehow determine the new type
// to create, and create one:
2015-05-05 14:05:39 -07:00
Class tc = (Class) trashType;
if (tc.getName().contains(info.id)) {
2015-05-05 11:20:13 -07:00
try {
// Get the dynamic constructor method
// that takes a double argument:
Constructor ctor =
2015-05-05 14:05:39 -07:00
tc.getConstructor(
new Class[] {double.class});
2015-05-05 11:20:13 -07:00
// Call the constructor to create a
// new object:
return (Trash)ctor.newInstance(
2015-05-05 14:05:39 -07:00
new Object[]{info.data});
2015-05-05 11:20:13 -07:00
} catch(Exception ex) {
ex.printStackTrace();
throw new CannotCreateTrashException();
}
}
}
// Class was not in the list. Try to load it,
// but it must be in your class path!
try {
System.out.println("Loading " + info.id);
trashTypes.add(
Class.forName(info.id));
} catch(Exception e) {
e.printStackTrace();
throw new PrototypeNotFoundException();
}
// Loaded successfully. Recursive call
// should work this time:
return factory(info);
}
public static class Info {
public String id;
public double data;
public Info(String name, double data) {
id = name;
this.data = data;
}
}
} ///:~