Thursday, May 5, 2016

[LintCode] 497 Shape Factory 解题报告

Description
Factory is a design pattern in common usage. Implement a ShapeFactory that can generate correct shape.
You can assume that we have only tree different shapes: Triangle, Square and Rectangle.


Example
ShapeFactory sf = new ShapeFactory();
Shape shape = sf.getShape("Square");
shape.draw();
>>   ----
>>  |       |
>>  |       |
>>   ----

shape = sf.getShape("Triangle");
shape.draw();
>>     / \
>>    /    \
>>  /____\

shape = sf.getShape("Rectangle");
shape.draw();
>>  ----
>> |     |
>>  ----


Code
/**
 * 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 {
    // Write your code here
    public void draw() {
        System.out.println(" ----");
        System.out.println("|    |");
        System.out.println(" ----");
    }
}

class Square implements Shape {
    // Write your code here
    public void draw() {
        System.out.println(" ----");
        System.out.println("|    |");
        System.out.println("|    |");
        System.out.println(" ----");
    }
}

class Triangle implements Shape {
    // Write your code here
    public void draw() {
        System.out.println("  /\\");
        System.out.println(" /  \\");
        System.out.println("/____\\");
    }
}

public class ShapeFactory {
    /**
     * @param shapeType a string
     * @return Get object of type Shape
     */
    public Shape getShape(String shapeType) {
        // Write your code here
        switch (shapeType) {
            case "Rectangle":
                return new Rectangle();
            case "Square":
                return new Square();
            case "Triangle":
                return new Triangle();
            default:
                return null;
        }
    }
}