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.抽象工厂模式是 ...
随机推荐
- Linux内核分析第三周学习笔记
linux内核分析第三周学习笔记 标签(空格分隔): 20135328陈都 陈都 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.co ...
- 20135337朱荟潼 Linux第一周学习总结——计算机是如何工作的
朱荟潼 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课http://mooc.study.163.com/course/USTC-1000029000 1.冯诺依曼体系结 ...
- Hadoop 4 MapReduce
对单词个数统计的MapReduce的案例 Mapper类: package main.java.worldClient; import java.io.IOException; import org. ...
- jquery实现点击复制到剪切板
1.必须有先引入 jquery库 <script type="text/javascript" src="js/jquery.js"></sc ...
- Cloudstack 的搭建
Note: 关闭了NFS Storage 的防火墙 service iptables stop 1. 新创建的Linux没有获取IP; vi /etc/sysconfig/network-script ...
- [Asp.net MVC]Asp.net MVC5系列——布局视图
目录 系列文章 概述 布局视图 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列——添加视图 [Asp.net M ...
- 解决sublime text3下中文无法输入的问题(Ubuntu)
sublime-text-imfix,非常无脑.就喜欢这样的.
- Git push -u orign master 提示hint: not have locally. This is usually caused by another repository push
一.情景 1.在GitHub上创建一个仓库A,并且初始化了readme.md这个文档. 2.在本地用Git Bash初始化仓库A(一开始没有从GitHub上拉下来). git init /* 初始化一 ...
- 对象内存空间 在创建对象后 运行时 会把对象的方法放到jvm的方法区中 调用时 将方法拿到栈中 执行完后 这个方法会出栈 然后新的方法方法进栈
- MySQL relay_log_purge=0 时的风险
转自: http://xiezhenye.com/2015/12/mysql-relay_log_purge0-%E6%97%B6%E7%9A%84%E9%A3%8E%E9%99%A9.html 有时 ...