2015-05-05 11:20:13 -07:00
|
|
|
//: patterns/ShapeFactory2.java
|
|
|
|
// Polymorphic factory methods.
|
|
|
|
import java.util.*;
|
2015-05-18 23:05:20 -07:00
|
|
|
import static net.mindview.util.Print.*;
|
2015-05-05 11:20:13 -07:00
|
|
|
|
|
|
|
class BadShapeCreation extends Exception {
|
|
|
|
BadShapeCreation(String msg) {
|
|
|
|
super(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Shape {
|
|
|
|
void draw();
|
|
|
|
void erase();
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class ShapeFactory {
|
|
|
|
protected abstract Shape create();
|
2015-05-06 15:14:33 -07:00
|
|
|
static Map<String, ShapeFactory> factories =
|
|
|
|
new HashMap<>();
|
|
|
|
static Shape createShape(String id)
|
2015-05-05 11:20:13 -07:00
|
|
|
throws BadShapeCreation {
|
|
|
|
if(!factories.containsKey(id)) {
|
|
|
|
try {
|
|
|
|
Class.forName(id); // Load dynamically
|
|
|
|
} catch(ClassNotFoundException e) {
|
|
|
|
throw new BadShapeCreation(id);
|
|
|
|
}
|
|
|
|
// See if it was put in:
|
|
|
|
if(!factories.containsKey(id))
|
|
|
|
throw new BadShapeCreation(id);
|
|
|
|
}
|
2015-05-06 15:14:33 -07:00
|
|
|
return factories.get(id).create();
|
2015-05-05 11:20:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Circle implements Shape {
|
|
|
|
private Circle() {}
|
2015-05-18 23:05:20 -07:00
|
|
|
public void draw() { print("Circle.draw"); }
|
|
|
|
public void erase() { print("Circle.erase"); }
|
2015-05-05 11:20:13 -07:00
|
|
|
static class Factory extends ShapeFactory {
|
|
|
|
@Override
|
2015-05-06 15:14:33 -07:00
|
|
|
protected Shape create() {
|
|
|
|
return new Circle();
|
2015-05-05 11:20:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
static {
|
|
|
|
ShapeFactory.factories.put(
|
|
|
|
"Circle", new Circle.Factory());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Square implements Shape {
|
2015-05-06 15:14:33 -07:00
|
|
|
private Square() {}
|
2015-05-18 23:05:20 -07:00
|
|
|
public void draw() { print("Square.draw"); }
|
|
|
|
public void erase() { print("Square.erase"); }
|
2015-05-05 11:20:13 -07:00
|
|
|
static class Factory extends ShapeFactory {
|
|
|
|
@Override
|
2015-05-06 15:14:33 -07:00
|
|
|
protected Shape create() {
|
|
|
|
return new Square();
|
2015-05-05 11:20:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
static {
|
|
|
|
ShapeFactory.factories.put(
|
|
|
|
"Square", new Square.Factory());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ShapeFactory2 {
|
|
|
|
public static void main(String args[]) {
|
2015-05-06 15:14:33 -07:00
|
|
|
String shlist[] = { "Circle", "Square",
|
2015-05-05 11:20:13 -07:00
|
|
|
"Square", "Circle", "Circle", "Square" };
|
2015-05-06 15:14:33 -07:00
|
|
|
ArrayList<Shape> shapes = new ArrayList<>();
|
2015-05-05 11:20:13 -07:00
|
|
|
try {
|
2015-05-18 23:05:20 -07:00
|
|
|
for(String shlist1 : shlist) {
|
|
|
|
shapes.add(
|
|
|
|
ShapeFactory.createShape(shlist1));
|
2015-05-05 14:05:39 -07:00
|
|
|
}
|
2015-05-05 11:20:13 -07:00
|
|
|
} catch(BadShapeCreation e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return;
|
|
|
|
}
|
2015-05-06 15:14:33 -07:00
|
|
|
Iterator<Shape> i = shapes.iterator();
|
2015-05-05 11:20:13 -07:00
|
|
|
while(i.hasNext()) {
|
2015-05-06 15:14:33 -07:00
|
|
|
Shape s = i.next();
|
2015-05-05 11:20:13 -07:00
|
|
|
s.draw();
|
|
|
|
s.erase();
|
|
|
|
}
|
2015-05-06 15:14:33 -07:00
|
|
|
}
|
2015-05-05 11:20:13 -07:00
|
|
|
} ///:~
|