LintCode:玩具工厂
来源:互联网 发布:网络挣钱 编辑:程序博客网 时间:2024/04/29 07:05
工厂模式是一种常见的设计模式。请实现一个玩具工厂 ToyFactory
用来产生不同的玩具类。可以假设只有猫和狗两种玩具。
ToyFactory tf = ToyFactory();Toy toy = tf.getToy('Dog');toy.talk(); >> Wowtoy = tf.getToy('Cat');toy.talk();
>> Meow
代码:
/** * Your object will be instantiated and called as such: * ToyFactory* tf = new ToyFactory(); * Toy* toy = tf->getToy(type); * toy->talk(); */class Toy {public: virtual void talk() const=0;};class Dog: public Toy { // Write your code herepublic: void talk()const { printf("Wow"); }};class Cat: public Toy { // Write your code herepublic: void talk()const { printf("Meow"); }};class ToyFactory {public: /** * @param type a string * @return Get object of the type */ Toy* getToy(string& type) { // Write your code here Toy* toy; if(type == "Dog") toy = new Dog(); else toy = new Cat(); return toy; }};
- LintCode:玩具工厂
- lintcode-->玩具工厂
- LintCode之玩具工厂
- 玩具工厂-LintCode
- LintCode-[容易] 496. 玩具工厂
- LintCode-玩具工厂(Java工厂模式)
- 玩具工厂
- 玩具工厂
- LintCode算法题解——移动零、玩具工厂、左填充、丑数
- 形状工厂-lintcode
- lintcode-->形状工厂
- Lintcode: 形状工厂
- 496.Toy Factory-玩具工厂(容易题)
- LintCode
- lintcode--
- lintcode
- LintCode
- Lintcode
- python搭配环境变量服务端管理所有客户端的houdini资产及工具
- jQuery部分知识点汇总
- 敏捷开发初学体会
- Shell—简介
- 如何修改WAMP中mysql默认空密码
- LintCode:玩具工厂
- 文件系统遇到的各种问题
- 爬虫可以做哪些有趣的事情,欢迎讨论
- Class.forName加载自定义类(带参数构造方法)
- 多项式计算
- Android主题设置为@android:style/Theme.Dialog报错解决办法
- Js中的原型继承和原型链
- Android 启动第三方应用
- android面试题汇总