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 简单的工 ...
随机推荐
- POSIX-Centos查看rpm包安装位置及相关信息
rpm -qifl `which svnserve`; http://www.ctohome.com/FuWuQi/56/122.html [国外服务器及ip租用价格] Centos6 kvm网桥配置 ...
- SNMP: Simple? Network Management Protocol(转)
转自:http://www.rane.com/note161.html An SNMP Overview The Message Format The Actual Bytes Introductio ...
- A Tour of Go Struct Literals
A struct literal denotes a newly allocated struct value by listing the values of its fields. You can ...
- tomcat配置多个web网站的配置详解
假如只有一台服务器,需要配置多个web网站(端口不同我还没试),该怎么样配置tomcat呢,其实很简单,只需要将tomcat 下面的 server.xml 中增加两个甚至是多个<Host> ...
- 第三周作业、实时操作系统µC/OS介绍及其它内容
作业要求 见<实时控制软件设计>第三周作业 1 阅读笔记--µC/OS 1.1 基本介绍 µC/OS是由Micrium公司研发的实时操作系统,以µC/OS-II或µC/OS-III为内核, ...
- 自己动手Jquery插件
最近Web应用程序中越来越多地用到了JQuery等Web前端技术.这些技术框架有效地改善了用户的操作体验,同时也提高了开发人员构造丰富客户 端UI的效率.JQuery本身提供了丰富的操作,但是,有时候 ...
- cocos2d-x 卡牌翻牌效果的实现
转自:http://blog.csdn.net/yanghuiliu/article/details/9115833 这个能实现翻牌的action就是CCOrbitCamera. static CCO ...
- Hyper-V介绍
Hyer-v主机是高端虚拟主机用户的最佳选择.您不再受其他用户程序对您造成的影响,您将得到的是更加公平的资源分配,远远低于虚拟主机的故障率.Hyper-V的分区包含两种:父分区和客户分区.Hyper- ...
- MySQL启动和关闭服务命令
MySQL启动和关闭服务命令 1.启动服务命令 net start mysql 2.关闭服务命令 net stop mysql
- [React] React Fundamentals: Owner Ownee Relationship
The owner-ownee relationship is used to designate a parent-child relationship with React components ...