[LintCode] 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.
Example
ShapeFactory sf = new ShapeFactory();
Shape shape = sf.getShape("Square");
shape.draw();
>> ----
>> | |
>> | |
>> ----
shape = sf.getShape("Triangle");
shape.draw();
>> /\
>> / \
>> /____\
shape = sf.getShape("Rectangle");
shape.draw();
这道题让我们求形状工厂,实际上就是Factory pattern的一个典型应用,说得是有一个基类Shape,然后派生出矩形,正方形,和三角形类,每个派生类都有一个draw,重写基类中的draw,然后分别画出派生类中的各自的形状,然后在格式工厂类中提供一个派生类的字符串,然后可以新建对应的派生类的实例,没啥难度,就是考察基本的知识。
class Shape {
public:
    virtual void draw() const=;
};
class Rectangle: public Shape {
public:
    void draw() const {
        cout << " ---- " << endl;
        cout << "|    |" << endl;
        cout << " ---- " << endl;
    }
};
class Square: public Shape {
public:
    void draw() const {
        cout << " ---- " << endl;
        cout << "|    |" << endl;
        cout << "|    |" << endl;
        cout << " ---- " << endl;
    }
};
class Triangle: public Shape {
public:
    void draw() const {
        cout << "  /\\ " << endl;
        cout << " /  \\ " << endl;
        cout << "/____\\ " << endl;
    }
};
class ShapeFactory {
public:
    /**
     * @param shapeType a string
     * @return Get object of type Shape
     */
    Shape* getShape(string& shapeType) {
        if (shapeType == "Square") return new Square();
        else if (shapeType == "Triangle") return new Triangle();
        else if (shapeType == "Rectangle") return new Rectangle();
        else return NULL;
    }
};
[LintCode] Shape Factory 形状工厂的更多相关文章
- [LintCode] Toy Factory 玩具工厂
		
Factory is a design pattern in common usage. Please implement a ToyFactory which can generate proper ...
 - lintcode:形状工厂
		
题目 工厂模式是一种常见的设计模式.实现一个形状工厂 ShapeFactory 来创建不同的形状类.这里我们假设只有三角形,正方形和矩形三种形状. 样例 ShapeFactory sf = new S ...
 - Factory Method 工厂方法模式
		
Factory method工厂方法模式是一种实现了“工厂”概念的面向对象设计模式.就像其他创建型模式一样,它也是处理在不指定对象具体类型的情况下创建对象的问题.工厂方法模式的实质是“定义一个创建对象 ...
 - 1740: [Usaco2005 mar]Yogurt factory 奶酪工厂
		
1740: [Usaco2005 mar]Yogurt factory 奶酪工厂 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 119 Solved: ...
 - Factory,工厂设计模式,C++描述
		
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
 - 设计模式04: Factory Methord 工厂方法模式(创建型模式)
		
Factory Methord 工厂方法模式(创建型模式) 从耦合关系谈起耦合关系直接决定着软件面对变化时的行为 -模块与模块之间的紧耦合使得软件面对变化时,相关的模块都要随之变更 -模块与模块之间的 ...
 - 设计模式02: Abstract Factory 抽象工厂(创建型模式)
		
Abstract Factory 抽象工厂(创建型模式) 常见的对象创建方法: //创建一个Road对象 Road road=new Road(); new的问题: -实现依赖 ...
 - Android shape自定义形状,设置渐变色
		
<?xml version="1.0" encoding="utf-8"?> <!-- android:shape=["rect ...
 - bzoj1680[Usaco2005 Mar]Yogurt factory*&&bzoj1740[Usaco2005 mar]Yogurt factory 奶酪工厂*
		
bzoj1680[Usaco2005 Mar]Yogurt factory bzoj1740[Usaco2005 mar]Yogurt factory 奶酪工厂 题意: n个月,每月有一个酸奶需求量( ...
 
随机推荐
- android:imeOptions属性(转)
			
默认情况下软键盘右下角的按钮为“下一个”,点击会到下一个输入框,保持软键盘 设置 android:imeOptions="actionDone" ,软键盘下方变成“完成”,点击后光 ...
 - 字符串截取函数--C语言(转)
			
#include<stdio.h> #include<stdlib.h> char* substring(char* ch,int pos,int length) { char ...
 - ios 时间和毫秒数转换
			
01-时间和毫秒数的相互转换 //获取毫秒数的时间戳 long inter = [[NSDate date] timeIntervalSince1970]*1000; NSLog(@"%ld ...
 - Html 块级元素和行级元素
			
转载:http://blog.csdn.net/yuyanqiao/article/details/8558118 内联元素(inline element)* a - 锚点* abbr - 缩写* a ...
 - 前端学PHP之文件操作(认真读读)
			
前面的话 在程序运行时,程序本身和数据一般都存在内存中,当程序运行结束后,存放在内存中的数据被释放.如果需要长期保存程序运行所需的原始数据,或程序运行产生的结果,就需要把数据存储在文件或数据库.一般地 ...
 - Git 使用命令
			
$ git add . $ git commit -m "html files created 20160308 16:08" $ git push origin master 设 ...
 - Graphics 导出图片使用【这个主要是画图类图的使用,记录一下】
			
/// <summary> /// 导出信令流程矢量图 /// </summary> /// <param name="signalFlowInfos" ...
 - jquery事件重复绑定解决办法
			
一$.fn.live 重复绑定 解决:使用die()方法,在live()方法绑定前,将此元素上的前面被绑定的事件统统解除,然后再通过live()方法绑定新的事件. //先通过die()方法解除,再通过 ...
 - CF#335 Intergalaxy Trips
			
Intergalaxy Trips time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
 - void和void*
			
void的含义 void即“无类型”,void *则为“无类型指针”,可以指向任何数据类型. void指针使用规范①void指针可以指向任意类型的数据,亦即可用任意数据类型的指针对void指针赋值.例 ...