Headfirst设计模式的C++实现——简单工厂模式(Simple Factory)
Pizza.h
#ifndef _PIZZA_H
#define _PIZZA_H
#include <iostream>
#include <string> class Pizza
{
public:
Pizza(const std::string &type) : m_type(type) {}
virtual ~Pizza() {}
virtual void prepare() { std::cout << "prepare " << m_type << std::endl; }
virtual void bake() { std::cout << "bake " << m_type << std::endl; }
virtual void cut() { std::cout << "cut " << m_type << std::endl; }
virtual void box() { std::cout << "box " << m_type << std::endl; }
private:
std::string m_type;
};
#endif
CheesePizza.h
#ifndef _CHEESE_PIZZA_H
#define _CHEESE_PIZZA_H
#include <iostream>
#include "Pizza.h" class CheesePizza : public Pizza
{
public:
CheesePizza() : Pizza("cheese") {}
~CheesePizza() {}
};
#endif
GreekPizza.h
#ifndef _GREEK_PIZZA_H
#define _GREEK_PIZZA_H
#include <iostream>
#include "Pizza.h" class GreekPizza : public Pizza
{
public:
GreekPizza() : Pizza("greek") {}
~GreekPizza() {}
}; #endif
SimplePizzaFactory.h
#ifndef _SIMPLE_PIZZA_FACTORY
#define _SIMPLE_PIZZA_FACTORY #include "CheesePizza.h"
#include "GreekPizza.h" class SimplePizzaFactory
{
public:
Pizza *CreatePizza(const std::string & type)
{
if ( "cheese" == type )
{
return new CheesePizza();
}
if ( "greek" == type )
{
return new GreekPizza();
}
return NULL;
}
};
#endif
PizzaStore.h
#ifndef _PIZZA_STORE_H
#define _PIZZA_STORE_H
#include "SimplePizzaFactory.h" class PizzaStore
{
private:
SimplePizzaFactory pizza_factory;
public:
Pizza* OrderPizza(const std::string &type)
{
Pizza *p_pizza = pizza_factory.CreatePizza(type);
if (p_pizza)
{
p_pizza->prepare();
p_pizza->bake();
p_pizza->cut();
p_pizza->box();
}
return p_pizza;
}
};
#endif
main.cpp
#include "PizzaStore.h"
int main()
{
PizzaStore pizza_store;
Pizza *p_pizza = pizza_store.OrderPizza("greek");
if ( p_pizza )
{
delete p_pizza;
}
return ;
}
Headfirst设计模式的C++实现——简单工厂模式(Simple Factory)的更多相关文章
- Headfirst设计模式的C++实现——简单工厂模式(Simple Factory)之二
为了引出后续的工厂方法,把在简单工厂模式的基础上增加了新功能——加盟店 简而言之就是把原来的单一简单工厂(能生产cheese和greek两种pizza)细分成了纽约地区的和芝加哥地区的(每种地区都能生 ...
- 设计模式之简单工厂模式Simple Factory(四创建型)
工厂模式简介. 工厂模式专门负责将大量有共同接口的类实例化 工厂模式可以动态决定将哪一个类实例化,不必事先知道每次要实例化哪一个类. 工厂模式有三种形态: 1.简单工厂模式Simple Factory ...
- 【设计模式】简单工厂模式 Simple Factory Pattern
简单工厂模式Simple Factory Pattern[Simple Factory Pattern]是设计模式里最简单的一个模式,又叫静态工厂模式[Static Factory Pattern], ...
- Golang设计模式—简单工厂模式(Simple Factory Pattern)
Golang设计模式--简单工厂模式 背景 假设我们在做一款小型翻译软件,软件可以将德语.英语.日语都翻译成目标中文,并显示在前端. 思路 我们会有三个具体的语言翻译结构体,或许以后还有更多,但现在分 ...
- 创建型模式(前引)简单工厂模式Simple Factory
一引出的原因(解决下面的问题) 简单工厂模式(Simple Factory Pattern):又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式. 在简单工厂模式 ...
- 大白话简单工厂模式 (Simple Factory Pattern)
大白话简单工厂模式 (Simple Factory Pattern) 从买车经历说起 毕业两年,码农张小两口无法忍受挤公交,凌晨起床抢火车票的痛苦,遂计划买车.逛了多家4S店,最终定下日产某车型的轿车 ...
- 设计模式的征途—2.简单工厂(Simple Factory)模式
工厂模式是最常用的一种创建型模式,通常所说的工厂模式一般是指工厂方法模式.本篇是是工厂方法模式的“小弟”,我们可以将其理解为工厂方法模式的预备知识,它不属于GoF 23种设计模式,但在软件开发中却也应 ...
- Net设计模式实例之简单工厂模式(Simple Factory Pattern)
一.简单工厂模式简介(Bref Introduction) 简单工厂模式(Simple Factory Pattern)的优点是,工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类, ...
- C#设计模式-1简单工厂模式Simple Factory)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 简单的工 ...
随机推荐
- Codeforces Round #226 (Div. 2)C. Bear and Prime Numbers
/* 可以在筛选质数的同时,算出每组数据中能被各个质数整除的个数, 然后算出[0,s]的个数 [l,r] 的个数即为[0,r]的个数减去[0,l]个数. */ #include <stdio.h ...
- 互联网挣钱info
AdSense – Google 广告 http://www.freehao123.com/tag/mianfeiphpkongjian/ [免费资源部落;] ntpdate -u time-b.ti ...
- A Tour of Go Making slices
Slices are created with the make function. It works by allocating a zeroed array and returning a sli ...
- Hibernate一张图
- iOS不勾选设置,实现某个界面强制横屏
1.在不勾选横屏的前提下,实现某一个界面横屏显示,比如播放视频.图表显示等. 2.只能Present跳转,Push会无效. 3.实现代码 在需要横屏的VC里,添加如下代码 #pragma mark 强 ...
- SRM566 1000pts
绍一的模拟赛题 [题意] 小Z养了$
- SweetAlert 使用
$(".delete").click(function(){ var work_name = $(this).data('name'); var item_id = $(this) ...
- 利用Splatting提交参数(Hash,哈希)
$infos = @{} $infos.Path = 'c:\Windows' $infos.Recurse = $true $infos.Filter = '*.log' $infos.ErrorA ...
- 实现app上对csdn的文章查看,以及文章中图片的保存 (制作csdn app 完结篇)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24022165 今天给大家带来CSDN的完结篇,即增加文章的查看和文章中图片的保存 ...
- Linux curl命令参数详解--转载
linux curl是通过url语法在命令行下上传或下载文件的工具软件,它支持http,https,ftp,ftps,telnet等多种协议,常被用来抓取网页和监控Web服务器状态. 一.Linux ...