package shapeFactory;
/**
* Your object will be instantiated and called as such: ShapeFactory sf = new
* ShapeFactory(); Shape shape = sf.getShape(shapeType); shape.draw();
*/
interface Shape {
void draw();
}
class Rectangle implements Shape {
@Override
public void draw() {
System.out.println(" ----" + "\n" + " | |" + "\n" + " ----" + "\n");
}
// Write your code here
}
class Square implements Shape {
@Override
public void draw() {
System.out.println(" ----" + "\n" + " | |" + "\n" + " | |" + "\n" + " ----" + "\n");
}
}
class Triangle implements Shape {
@Override
public void draw() {
System.out.println(" /\\" + "\n" + " / \\" + "\n" + " /____\\" + "\n");
}
}
public class ShapeFactory {
/**
* @param shapeType
* a string
* @return Get object of type Shape
*/
public Shape getShape(String shapeType) {
if (shapeType.equals("Triangle")) {
return new Triangle();
}
if (shapeType.equals("Square")) {
return new Square();
}
if (shapeType.equals("Rectangle")) {
return new Rectangle();
}
return new Rectangle();
// Write your code here
}
public static void main(String[] args) {
ShapeFactory sf = new ShapeFactory();
Shape shape = sf.getShape("Square");
shape.draw();
}
}
lintcode-->形状工厂
最新推荐文章于 2021-08-09 04:27:21 发布