我不明白为什么工厂模式在创建接口对象时会起作用。
Shape.java
public interface Shape {
void draw();
}
ShapeFactory.java
public class ShapeFactory {
//use getShape method to get object of type shape
public Shape getShape(String shapeType){
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("CIRCLE")){
return new Circle();
} else if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
return null;
}
}
FactoryPatternDemo.java
public static void main(String[] args) {
Shape shape1 = shapeFactory.getShape("CIRCLE");}
我希望会出现编译错误。 Shape shape1 = shapeFactory.getShape("CIRCLE");
在这种情况下是什么意思?接口的名称(Shape)在此代码行中扮演什么角色,仅仅是为了清楚起见?