资讯专栏INFORMATION COLUMN

[LintCode] Shape Factory

zebrayoung / 960人阅读

摘要:这道题考了,具体概念如下除此之外,还需要注意正则表达式的写法。

Problem

Factory is design pattern in common usage. Implement a ShapeFactory that can generate correct shape.

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

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

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

这道题考了interface & implementation & override,具体概念如下:

Interface: A Java interface is a bit like a class, except that it can only contain method signatures and fields, which is saying that it cannot contain any implementation of the methods. You can use interface to achieve polymorphism.

Implementation: To declare a class that implements an interface, you have to include an implements clause in the class definition. Your class can implement more than one interface.

Overriding: If subclass provides the specific/close implementation of the method that has been provided by one of its parent class, it is known as method overriding.

除此之外,还需要注意正则表达式的写法。

Solution
interface Shape {
    void draw();
}

class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println(" ----"); 
        System.out.println("|    |");
        System.out.println(" ----");    
    }
}

class Square implements Shape {
    @Override
    public void draw() {
        System.out.println(" ----"); 
        System.out.println("|    |");
        System.out.println("|    |");
        System.out.println(" ----");
    }
}

class Triangle implements Shape {
    @Override
    public void draw() {
        System.out.println("  /"); 
        System.out.println(" /  ");
        System.out.println("/____");
    }
}

public class ShapeFactory {
    public Shape getShape(String shapeType) {
        if (shapeType.equalsIgnoreCase("Rectangle")) return new Rectangle();
        else if (shapeType.equalsIgnoreCase("Square")) return new Square();
        else if (shapeType.equalsIgnoreCase("Triangle")) return new Triangle();
        return null;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65803.html

相关文章

  • [LintCode] Toy Factory

    摘要:系统设计基础题,用和继承,然后在里按照生成需要的类就可以了。 Problem Factory is a design pattern in common usage. Please implement a ToyFactory which can generate proper toy based on the given type. Example ToyFactory tf = ...

    BlackHole1 评论0 收藏0
  • 设计模式:工厂三姐妹一网打尽

    摘要:假设需要创建一个矩形,我们可以这样做需要一个圆形也简单实际上,我们通常是在界面上,一般是工具栏上,放置两个按钮,让用户选择哪个按钮,然后创建此形状。 作为创建型设计模式,带有工厂名字的设计模式共有三个,分别是 Simple Factory Factory Method Abstract Factory 其中的 Simple Factory并不是GoF一书中的模式,但是它是最基础最常...

    diabloneo 评论0 收藏0
  • Design Pattern - Factory Pattern(译)

    摘要:原文链接译者个人翻译,水平有限,如有错误欢迎指出,谢谢设计模式工厂模式工厂模式是中最常用的设计模式之一。这种类型的设计模式属于创建型模式下,创建一个对象最好的方式之一。调用圆的方法获得矩形的一个对象并调用它的方法。 原文链接译者:smallclover个人翻译,水平有限,如有错误欢迎指出,谢谢! 设计模式-工厂模式 工厂模式是Java中最常用的设计模式之一。这种类型的设计模式属于创建型...

    zhangwang 评论0 收藏0
  • Design Pattern - Abstract Factory Pattern(译)

    摘要:原文链接译者个人翻译,水平有限,如有错误欢迎指出,谢谢设计模式抽象工厂模式抽象工厂的核心是一个超级工厂,而这个工厂能创建其他的工厂。在抽象工厂模式中,一个接口负责创建抽象与一个工厂相关的对象,不需要显示的指定它们的类。 原文链接译者:smallclover个人翻译,水平有限,如有错误欢迎指出,谢谢! 设计模式-抽象工厂模式 抽象工厂的核心是一个超级工厂,而这个工厂能创建其他的工厂。所以...

    王笑朝 评论0 收藏0
  • 深入理解工厂模式

    摘要:工厂模式的分类简单工厂模式,又称静态工厂方法模式。工厂方法模式,又称多态性工厂模式或虚拟构造子模式抽象工厂模式,又称工具箱或模式。具体产品角色抽象工厂模式所创建的任何产品对象都是某一个具体产品类的实例。 Java面试通关手册(Java学习指南,欢迎Star,会一直完善下去,欢迎建议和指导):https://github.com/Snailclimb/Java_Guide 历史回顾: 深...

    zhou_you 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<