设计模式学习——工厂模式(Factory Pattern)
1、有一个工厂,专门生产不同品牌的汽车。当有人需要从此工厂提货的时候,只需要告诉他,要什么品牌的,就可以了,并不关心这些车是怎么生产出来的。
2、以上方式,如果增加品牌的时候,也要修改工厂,有点麻烦。于是,把工厂也抽象了。
1的类图与实现:

首先,是通用的车
///
/// @file Car.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-12 20:10:31
/// #ifndef __CAR_H__
#define __CAR_H__ #include <iostream> namespace marrs{ using std::cout;
using std::cerr;
using std::endl; class Car
{
public:
Car() : b_IsRunning(){}
virtual ~Car(){};
public:
virtual void Run() = ;
virtual void Stop() = ;
protected:
bool b_IsRunning;
}; } #endif //__CAR_H__
然后是不同品牌的车,继承自Car
///
/// @file Benz.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-12 20:20:54
/// #ifndef __BENZ_H__
#define __BENZ_H__ #include "Car.h" namespace marrs{ class Benz
: public Car
{
public:
~Benz(){}
public:
void Run();
void Stop();
}; } #endif //__BENZ_H__
///
/// @file Benz.cc
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-12 20:21:54
/// #include "Benz.h" namespace marrs{ void Benz::Run()
{
if (b_IsRunning)
{
cerr << "Benz is running!" << endl;
} cout << "Benz is going to running!" << endl;
b_IsRunning = true;
} void Benz::Stop()
{
if (!b_IsRunning)
{
cerr << "Benz isn't running..." << endl;
} cout << "Benz is going to stopping!" << endl;
b_IsRunning = false;
} }
///
/// @file Audi.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-12 20:20:54
/// #ifndef __AUDI_H__
#define __AUDI_H__ #include "Car.h" namespace marrs{ class Audi
: public Car
{
public:
~Audi(){}
public:
void Run();
void Stop();
}; } #endif//__AUDI_H__
///
/// @file Audi.cc
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-12 20:21:54
/// #include "Audi.h" namespace marrs{ void Audi::Run()
{
if (b_IsRunning)
{
cerr << "Audi is running!" << endl;
} cout << "Audi is going to running!" << endl;
b_IsRunning = true;
} void Audi::Stop()
{
if (!b_IsRunning)
{
cerr << "Audi isn't running..." << endl;
} cout << "Audi is going to stopping!" << endl;
b_IsRunning = false;
} }
///
/// @file Lamborghini.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-12 20:20:54
/// #ifndef __LAMBORGHINI_H__
#define __LAMBORGHINI_H__ #include "Car.h" namespace marrs{ class Lamborghini
: public Car
{
public:
~Lamborghini(){}
public:
void Run();
void Stop();
}; } #endif//__LAMBORGHINI_H__
///
/// @file Lamborghini.cc
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-12 20:21:54
/// #include "Lamborghini.h" namespace marrs{ void Lamborghini::Run()
{
if (b_IsRunning)
{
cerr << "Lamborghini is running!" << endl;
} cout << "Lamborghini is going to running!" << endl;
b_IsRunning = true;
} void Lamborghini::Stop()
{
if (!b_IsRunning)
{
cerr << "Lamborghini isn't running..." << endl;
} cout << "Lamborghini is going to stopping!" << endl;
b_IsRunning = false;
} }
接着,有个生产工厂
///
/// @file Factory.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-12 20:27:42
/// #ifndef __FACTORY_H__
#define __FACTORY_H__ #include "Benz.h"
#include "Audi.h"
#include "Lamborghini.h" enum Brand
{
EN_BRAND_CAR_BANZ = ,
EN_BRAND_CAR_AUDI,
EN_BRAND_CAR_LAMBORGHINI,
}; namespace marrs{ using std::cout;
using std::endl; class Factory
{
public:
Car * Produce(int int_brand);
void Reclaim(Car * car_brand);
}; } #endif //__FACTORY_H__
///
/// @file Factory.cc
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-12 20:39:05
/// #include "Factory.h" namespace marrs{ Car * Factory::Produce(int int_brand)
{
switch(int_brand)
{
case EN_BRAND_CAR_BANZ:
return new Benz;
case EN_BRAND_CAR_AUDI:
return new Audi;
case EN_BRAND_CAR_LAMBORGHINI:
return new Lamborghini;
default:break;
}
return NULL;
} void Factory::Reclaim(Car * car_brand)
{
delete car_brand;
} }
为了方便统一处理方式,我把车的销毁也放到工厂类里了。
///
/// @file main.cc
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-12 20:40:59
/// #include "Factory.h" using namespace marrs; int main()
{
Factory factory; Car * car_first = factory.Produce(EN_BRAND_CAR_BANZ);
car_first->Run();
car_first->Stop();
factory.Reclaim(car_first); Car * car_second = factory.Produce(EN_BRAND_CAR_AUDI);
car_second->Run();
car_second->Stop();
factory.Reclaim(car_second); Car * car_third = factory.Produce(EN_BRAND_CAR_LAMBORGHINI);
car_third->Run();
car_third->Stop();
factory.Reclaim(car_third); }
编译,运行
[ccx@ubuntu ~/object-oriented/Factory-Pattern]$>g++ * -o car_factory.exe
[ccx@ubuntu ~/object-oriented/Factory-Pattern]$>./car_factory.exe
Benz is going to running!
Benz is going to stopping!
Audi is going to running!
Audi is going to stopping!
Lamborghini is going to running!
Lamborghini is going to stopping!
2的类图与实现 (画图功底不行....略乱)

在1的基础之上,修改Factory
///
/// @file Factory.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-12 20:27:42
/// #ifndef __FACTORY_H__
#define __FACTORY_H__ #include "Car.h" namespace marrs{ using std::cout;
using std::endl; class Factory
{
public:
virtual ~Factory(){}
public:
virtual Car * Produce() = ;
void Reclaim(Car * car_brand)
{
delete car_brand;
}
}; } #endif //__FACTORY_H__
然后是不同的工厂
///
/// @file Benz_Factory.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-12 21:21:58
/// #ifndef __BENZ_FACTORY_H__
#define __BENZ_FACTORY_H__ #include "Factory.h"
#include "Benz.h" namespace marrs{ class BenzFactory
: public Factory
{
public:
Car * Produce()
{
return new Benz;
}
}; } #endif // __BENZ_FACTORY_H__
///
/// @file Audi_Factory.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-12 21:21:58
/// #ifndef __AUDI_FACTORY_H__
#define __AUDI_FACTORY_H__ #include "Factory.h"
#include "Audi.h" namespace marrs{ class AudiFactory
: public Factory
{
public:
Car * Produce()
{
return new Audi;
}
}; } #endif // __AUDI_FACTORY_H__
///
/// @file Lamborghini_Factory.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-12 21:21:58
/// #ifndef __LAMBORGHINI_FACTORY_H__
#define __LAMBORGHINI_FACTORY_H__ #include "Factory.h"
#include "Lamborghini.h" namespace marrs{ class LamborghiniFactory
: public Factory
{
public:
Car * Produce()
{
return new Lamborghini;
}
}; } #endif // __LAMBORGHINI_FACTORY_H__
最后修改main.cc
///
/// @file main.cc
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-12 20:40:59
/// #include "Benz_Factory.h"
#include "Audi_Factory.h"
#include "Lamborghini_Factory.h" using namespace marrs; void BenzAction()
{
Factory * factory = new BenzFactory;
Car * car_first = factory->Produce();
car_first->Run();
car_first->Stop();
factory->Reclaim(car_first);
delete factory;
} void AudiAction()
{
Factory * factory = new AudiFactory;
Car * car_first = factory->Produce();
car_first->Run();
car_first->Stop();
factory->Reclaim(car_first);
delete factory;
} void LamborghiniAction()
{
Factory * factory = new LamborghiniFactory;
Car * car_first = factory->Produce();
car_first->Run();
car_first->Stop();
factory->Reclaim(car_first);
delete factory;
} int main()
{
BenzAction();
AudiAction();
LamborghiniAction(); return ;
}
编译,运行
[ccx@ubuntu ~/object-oriented/Factory-Pattern_2]$>g++ * -o car_Factory.exe
[ccx@ubuntu ~/object-oriented/Factory-Pattern_2]$>./car_Factory.exe
Benz is going to running!
Benz is going to stopping!
Audi is going to running!
Audi is going to stopping!
Lamborghini is going to running!
Lamborghini is going to stopping!
设计模式学习——工厂模式(Factory Pattern)的更多相关文章
- python 设计模式之工厂模式 Factory Pattern (简单工厂模式,工厂方法模式,抽象工厂模式)
十一回了趟老家,十一前工作一大堆忙成了狗,十一回来后又积累了一大堆又 忙成了狗,今天刚好抽了一点空开始写工厂方法模式 我看了<Head First 设计模式>P109--P133 这25页 ...
- 【设计模式】工厂模式 Factory Pattern
1)简单工厂(不是模式) 简单工厂只是一种变成习惯,并非23种设计模式之一. 简单工厂提供将实例话那种类型留给运行时判断,而非编译时指定.简单工厂模式就是由一个工厂类根据传入的参数决定创建出哪一个类的 ...
- JAVA设计模式之工厂模式—Factory Pattern
1.工厂模式简介 工厂模式用于对象的创建,使得客户从具体的产品对象中被解耦. 2.工厂模式分类 这里以制造coffee的例子开始工厂模式设计之旅. 我们知道coffee只是一种泛举,在点购咖啡时需要指 ...
- 设计模式 - 工厂模式(factory pattern) 具体解释
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/27081511 工厂模式(factory pa ...
- 设计模式学习--迭代器模式(Iterator Pattern)和组合模式(Composite Pattern)
设计模式学习--迭代器模式(Iterator Pattern) 概述 ——————————————————————————————————————————————————— 迭代器模式提供一种方法顺序 ...
- 设计模式之工厂模式(Factory)
设计模式的工厂模式一共有三种:简单工厂模式,工厂模式,抽象工厂模式 简单工厂模式原理:只有一个工厂类,通过传参的形式确定所创建的产品对象种类 代码如下: #include <stdio.h> ...
- 23种设计模式--工厂模式-Factory Pattern
一.工厂模式的介绍 工厂模式让我们相到的就是工厂,那么生活中的工厂是生产产品的,在代码中的工厂是生产实例的,在直白一点就是生产实例的类,代码中我们常用new关键字,那么这个new出来的实例 ...
- 创建型模式篇(工厂模式Factory Pattern)
一.工厂模式(Factory Pattern) 1.定义: 在软件系统,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实现经常面临着剧烈的变化,但是它却拥有比较稳定的接口.提供一种封 ...
- java_设计模式_工厂模式_Factory Pattern(2016-08-04)
工厂模式主要是为创建对象提供了接口.工厂模式按照<Java与模式>中的提法分为三类: (1)简单工厂(Simple Factory)模式,又称静态工厂方法模式(Static Factory ...
随机推荐
- git 拖下laravel 代码后报错 Warning: require(D:\WWW\laravel\bootstrap/../vendor/autoload.php
omposer install 执行 Problem 1 - Installation request for doctrine/annotations v1.5.0 -> sat ...
- canvas 绘制圆形进度条
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 前端 day 30 html 基础一
前情提要: html基础一 一:html标签入门 1: 1 HTML结构 1)一个html文件有且只有一个html标签.这个就是HTML的根标签.2)一个HTML文件主要由两部分组成:文件头和文件体. ...
- iOS开发证书与配置文件的使用
前提 众所周知,开发iOS应用必须要有iOS证书(Certificates)和配置文件(Provisioning Profiles),那么问题来了: 1.什么是iOS证书,它是如何与app应用关联的? ...
- _new_()与_init_()的区别
先上代码 其中,__new__()不是一定要有,只有继承自object的类才有,该方法可以return父类(通过super(当前类名, cls).__new__())出来的实例,或者直接是obje ...
- (转)python函数: 内置函数
原文:https://blog.csdn.net/pipisorry/article/details/44755423 https://juejin.im/post/5ae3ee096fb9a07aa ...
- (转)Python 字符串
原文:http://www.runoob.com/python/python-strings.html
- css网页布局 --- 左边固定,右边自适应
div的布局统一如下: <body> <div class="wrap"> <div class="left"></d ...
- ArrayList的subList方法
参考博文使用java.util.List.subList时最好小心点 List接口中定义: List<E> subList(int fromIndex, int toIndex); 英文注 ...
- gocommand:一个跨平台的golang命令行执行package
最近在做一个项目的时候,需要使用golang来调用操作系统中的命令行,来执行shell命令或者直接调用第三方程序,这其中自然就用到了golang自带的exec.Command. 但是如果直接使用原生e ...