public class FactoryTest {
public static void main(String[] args) {
// The ComputerFactory.create method declares
// that it throws an Exception. So the caller
// must be prepared to catch it.
String mac = null;
try {
mac = ComputerFactory.create(ComputerType.Mac);
} catch(Exception e) {
System.out.println(e.getClass().getName() + ": " +
e.getMessage());
}
System.out.println("mac = " + mac);
}
}
final class ComputerFactory {
public static String create(ComputerType type) throws Exception {
switch(type) {
case PC: return "new CompPC";
case Mac: return "new CompMac";
case Sparc: return "new CompSparc";
case Itanium: return "new CompItanium";
}
throw new Exception("No such computer: " + type );
}
}
enum ComputerType { PC, Mac, Sparc, Itanium }