玩具工厂-LintCode
来源:互联网 发布:淘宝网汽车用品配件 编辑:程序博客网 时间:2024/06/09 20:05
工厂模式是一种常见的设计模式。请实现一个玩具工厂 ToyFactory 用来产生不同的玩具类。可以假设只有猫和狗两种玩具。
样例:
ToyFactory tf = ToyFactory();Toy toy = tf.getToy('Dog');toy.talk(); >> Wowtoy = tf.getToy('Cat');toy.talk();>> Meow
#ifndef C496_H#define C496_H#include<iostream>using namespace std;/*** 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 here void talk() const { cout << "Wow" << endl; }};class Cat : public Toy { // Write your code herepublic: void talk() const { cout << "Meow" << endl; }};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 == "Cat") toy = new Cat(); else toy = new Dog(); return toy; }};#endif
阅读全文
0 0
- LintCode:玩具工厂
- lintcode-->玩具工厂
- LintCode之玩具工厂
- 玩具工厂-LintCode
- LintCode-[容易] 496. 玩具工厂
- LintCode-玩具工厂(Java工厂模式)
- 玩具工厂
- 玩具工厂
- LintCode算法题解——移动零、玩具工厂、左填充、丑数
- 形状工厂-lintcode
- lintcode-->形状工厂
- Lintcode: 形状工厂
- 496.Toy Factory-玩具工厂(容易题)
- LintCode
- lintcode--
- lintcode
- LintCode
- Lintcode
- KNN算法学习总结
- Android笔记
- 学习笔记14
- Spring认知
- js框架封装(一)——框架介绍
- 玩具工厂-LintCode
- Toolbar+DrawerLayout+Fragment结合使用
- Android动态权限管理及封装
- codeforces 835B The number on the board
- 一种网络数据传输加密算法的设想
- C#冒泡排序
- ubuntu 16.04 下安装 VNC viewer
- 二叉堆的实现及其基本存储结构
- 连成人网站都用AI和ML做视频分类加标签了…