Shape Factory
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.
ShapeFactory sf = new ShapeFactory();
Shape shape = sf.getShape("Square");
shape.draw();
>> ----
>> | |
>> | |
>> ----
shape = sf.getShape("Triangle");
shape.draw();
>> /\
>> / \
>> /____\
shape = sf.getShape("Rectangle");
shape.draw();
>> ----
>> | |
>> ----
/**
* 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 {
public void draw() {
System.out.println(" ----");
System.out.println("| |");
System.out.println(" ----");
}
} class Square implements Shape {
public void draw() {
System.out.println(" ----");
System.out.println("| |");
System.out.println("| |");
System.out.println(" ----");
}
} class Triangle implements Shape {
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) {
if (shapeType.toLowerCase().equals("square")) {
return new Square();
} else if (shapeType.toLowerCase().equals("triangle")) {
return new Triangle();
} else if (shapeType.toLowerCase().equals("rectangle")) {
return new Rectangle();
} else {
return null;
}
}
}
Shape Factory的更多相关文章
- [LintCode] Shape Factory 形状工厂
Factory is a design pattern in common usage. Implement a ShapeFactory that can generate correct shap ...
- [LintCode] Toy Factory 玩具工厂
Factory is a design pattern in common usage. Please implement a ToyFactory which can generate proper ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 详细分析Orchard的Content、Drivers, Shapes and Placement 类型
本文原文来自:http://skywalkersoftwaredevelopment.net/blog/a-closer-look-at-content-types-drivers-shapes-an ...
- Java设计模式——抽象工厂模式
抽象工厂模式也是创建模式,可以把它理解成创建工厂的工厂,这种模式也是我们经常使用的.在抽象工厂中的接口是用来创建工厂的,每个生成的工厂又都可以按照工厂模式创建其他对象. 举例说明: 创建Shape接口 ...
- JAVA用geotools读写shape格式文件
转自:http://toplchx.iteye.com/blog/1335007 JAVA用geotools读写shape格式文件 (对应geotools版本:2.7.2) (后面添加对应geotoo ...
- [Design Pattern] Factory Pattern 简单案例
Factory Pattern , 即工厂模式,用于创建对象的场景,属于创建类的设计模式 . 下面是一个工厂模式案例. Shape 作为接口, Circle, Square, Rectangle 作为 ...
- Factory Pattern(工厂模式)
1.工厂模式简介 工厂模式,专门负责将大量有共同接口的类实例化(用来生产对象).其定义为定义一个用于创建对象的接口,让子类决定实例化那一个类.工厂方法使一个类的实例化延迟到其子类. 工厂模式拥有以下几 ...
- 抽象工厂模式(Abstract Factory)
(二)抽象工厂模式(Abstract Factory) 1.抽象工厂模式(Abstract Factory),提供了一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 2.抽象工厂模式是 ...
随机推荐
- 第二阶段站立会议alpha版总结
一.会议过程 在完成第二次冲刺后,Alpha版本最终发布,我们对校园二手交易平台开发过程及产品存在的问题进行了激烈讨论.进行了我们的团队总结会议,会议中每个人先发表了个人对Alpha版开发过程中存在的 ...
- PAT 甲级 1078 Hashing
https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 The task of this probl ...
- HDU 2020 绝对值排序
http://acm.hdu.edu.cn/showproblem.php?pid=2020 Problem Description 输入n(n<=100)个整数,按照绝对值从大到小排序后输出. ...
- 在PHPStorm中快速插入当前日期
在EditPlus中使用快捷键Ctrl+D即可插入当前日期,但在PHPStorm中似乎没有这样的快捷键,那如何实现快速插入当前日期呢?其实很简单,跟我做一遍你就会了: 目标 为PHPStorm定义一个 ...
- [转帖] Kubernetes如何使用ReplicationController、Replica Set、Deployment管理Pod ----文章很好 但是还没具体操作实践 也还没记住.
Kubernetes如何使用ReplicationController.Replica Set.Deployment管理Pod https://blog.csdn.net/yjk13703623757 ...
- jdk动态代理 要把目标对象 和自己都传进去;以便自己对目标对象的代理
- poj2082 Terrible Sets(单调栈)
Description Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all re ...
- 【Revit API】创建相机视角
在Revit中有一个相机功能可以以相机视角产生一个视图.一开始我在Revit2016的API文档中找关键词Camera,但是没什么收获. 其实这个相机功能的真正核心是创建透视视图:View3D.Cre ...
- suoi44 核能显示屏 (cdq分治)
首先二维树状数组肯定开不下 仿照二维树状数组的做法,如果有差分数组$d[i][j]=a[i][j]-a[i-1][j]-a[i][j-1]+a[i-1][j-1]$,那么就有: $$sum[x][y] ...
- debian9部署ownCloud
ownCloud是一个开源的私有云存储,支持外接存储,具有良好的扩展性.ownCloud是传统的C/S架构,支持目前各大流行平台.服务端客户端实时同步,使用体验非常好. ownCloud is Wed ...