2015-09-07 11:44:36 -06:00
|
|
|
// patterns/ShapeFactory2.java
|
2015-12-15 11:47:04 -08:00
|
|
|
// (c)2016 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
|
|
|
// Polymorphic factory methods
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.function.*;
|
2015-11-03 12:00:44 -08:00
|
|
|
import java.util.stream.*;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
2015-11-03 12:00:44 -08:00
|
|
|
|
|
|
|
class BadShapeCreation extends RuntimeException {
|
2015-06-15 17:47:35 -07:00
|
|
|
BadShapeCreation(String msg) {
|
|
|
|
super(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Shape {
|
|
|
|
void draw();
|
|
|
|
void erase();
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class ShapeFactory {
|
|
|
|
static Map<String, Supplier<Shape>> factories =
|
|
|
|
new HashMap<>();
|
|
|
|
static Shape createShape(String id)
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
return factories.get(id).get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Circle implements Shape {
|
|
|
|
private Circle() {}
|
2015-11-03 12:00:44 -08:00
|
|
|
public void draw() {
|
|
|
|
System.out.println("Circle.draw");
|
|
|
|
}
|
|
|
|
public void erase() {
|
|
|
|
System.out.println("Circle.erase");
|
|
|
|
}
|
2015-06-15 17:47:35 -07:00
|
|
|
static {
|
|
|
|
ShapeFactory.factories.put("Circle", Circle::new);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Square implements Shape {
|
|
|
|
private Square() {}
|
2015-11-03 12:00:44 -08:00
|
|
|
public void draw() {
|
|
|
|
System.out.println("Square.draw");
|
|
|
|
}
|
|
|
|
public void erase() {
|
|
|
|
System.out.println("Square.erase");
|
|
|
|
}
|
2015-06-15 17:47:35 -07:00
|
|
|
static {
|
|
|
|
ShapeFactory.factories.put("Square", Square::new);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ShapeFactory2 {
|
2016-01-25 18:05:55 -08:00
|
|
|
public static void main(String[] args) {
|
2015-11-03 12:00:44 -08:00
|
|
|
List<Shape> shapes = Stream.of("Circle", "Square",
|
|
|
|
"Square", "Circle", "Circle", "Square")
|
|
|
|
.map(ShapeFactory::createShape)
|
|
|
|
.collect(Collectors.toList());
|
2015-06-15 17:47:35 -07:00
|
|
|
shapes.forEach(Shape::draw);
|
|
|
|
shapes.forEach(Shape::erase);
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
Circle.draw
|
|
|
|
Square.draw
|
|
|
|
Square.draw
|
|
|
|
Circle.draw
|
|
|
|
Circle.draw
|
|
|
|
Square.draw
|
|
|
|
Circle.erase
|
|
|
|
Square.erase
|
|
|
|
Square.erase
|
|
|
|
Circle.erase
|
|
|
|
Circle.erase
|
|
|
|
Square.erase
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|