简单工厂模式
Shape.java
public interface Shape {
void draw();
}
Circle.java
public class Circle implements Shape{
@Override
public void draw() {
System.out.println("i'm a circle");
}
}
Rectangle.java
public class Rectangle implements Shape{
@Override
public void draw() {
System.out.println("i'm a rectangle");
}
}
工厂类ShapeFactory.java
public class ShapeFactory {
public static Shape getShape(String name) {
if ("circle".equals(name)) {
return new Circle();
}
if ("rectangle".equals(name)) {
return new Rectangle();
}
return null;
}
}