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)的更多相关文章

  1. python 设计模式之工厂模式 Factory Pattern (简单工厂模式,工厂方法模式,抽象工厂模式)

    十一回了趟老家,十一前工作一大堆忙成了狗,十一回来后又积累了一大堆又 忙成了狗,今天刚好抽了一点空开始写工厂方法模式 我看了<Head First 设计模式>P109--P133 这25页 ...

  2. 【设计模式】工厂模式 Factory Pattern

    1)简单工厂(不是模式) 简单工厂只是一种变成习惯,并非23种设计模式之一. 简单工厂提供将实例话那种类型留给运行时判断,而非编译时指定.简单工厂模式就是由一个工厂类根据传入的参数决定创建出哪一个类的 ...

  3. JAVA设计模式之工厂模式—Factory Pattern

    1.工厂模式简介 工厂模式用于对象的创建,使得客户从具体的产品对象中被解耦. 2.工厂模式分类 这里以制造coffee的例子开始工厂模式设计之旅. 我们知道coffee只是一种泛举,在点购咖啡时需要指 ...

  4. 设计模式 - 工厂模式(factory pattern) 具体解释

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/27081511 工厂模式(factory pa ...

  5. 设计模式学习--迭代器模式(Iterator Pattern)和组合模式(Composite Pattern)

    设计模式学习--迭代器模式(Iterator Pattern) 概述 ——————————————————————————————————————————————————— 迭代器模式提供一种方法顺序 ...

  6. 设计模式之工厂模式(Factory)

    设计模式的工厂模式一共有三种:简单工厂模式,工厂模式,抽象工厂模式 简单工厂模式原理:只有一个工厂类,通过传参的形式确定所创建的产品对象种类 代码如下: #include <stdio.h> ...

  7. 23种设计模式--工厂模式-Factory Pattern

    一.工厂模式的介绍       工厂模式让我们相到的就是工厂,那么生活中的工厂是生产产品的,在代码中的工厂是生产实例的,在直白一点就是生产实例的类,代码中我们常用new关键字,那么这个new出来的实例 ...

  8. 创建型模式篇(工厂模式Factory Pattern)

    一.工厂模式(Factory Pattern) 1.定义: 在软件系统,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实现经常面临着剧烈的变化,但是它却拥有比较稳定的接口.提供一种封 ...

  9. java_设计模式_工厂模式_Factory Pattern(2016-08-04)

    工厂模式主要是为创建对象提供了接口.工厂模式按照<Java与模式>中的提法分为三类: (1)简单工厂(Simple Factory)模式,又称静态工厂方法模式(Static Factory ...

随机推荐

  1. jvm(1)类的加载(二)(自定义类加载器)

    [深入Java虚拟机]之四:类加载机制 1,从Java虚拟机的角度,只存在两种不同的类加载器: 1,启动类加载器:它使用C++实现(这里仅限于Hotspot,也就是JDK1.5之后默认的虚拟机,有其他 ...

  2. js面试题-数组去重

    今天,在聊天群里看到数组去重的话题,面试者的答案如下: 参考答案如下: 程序员思维,做出如下测试: 未考虑到:1,‘1’是不同的,应该不去重 未考虑到对象 所以,参考答案只能去重基础类型 根据以往看过 ...

  3. iOS —— GCD 详解

    一.什么是GCD Grand Central Dispatch (强大的中枢调度器) ,是异步执行任务的技术之一.纯C语言,有很多强大的函数. 二.GCD的优势 (1)GCD是苹果公司为多核并行运算提 ...

  4. Linux CentOS7系统中php安装配置

    本篇讲解如何配置php开发环境,让你的php代码可以正常的在网页中运行. 准备工作 linux centos7操作系统 ssh软件 nginx php资源 想要了解更多关于php的内容,请访问: ph ...

  5. Python运行MapReducer程序时所遇异常

    landen@Master:~/UntarFile/hadoop-1.0.4$ bin/hadoop jar contrib/streaming/hadoop-streaming-1.0.4.jar ...

  6. (转) centos 7.0 nginx 1.7.9成功安装过程

    centos 7.0根目录 的目录构成 [root@localhost /]# lsbin dev home lib64 mnt proc run srv tmp varboot etc lib me ...

  7. github的本地配置和项目创建

    之前完成了github的安装和账号的注册,接下来要进行项目的创建和本地代码仓库的建立 1.创建项目 2.填写项目相关信息 注意:在给项目起名时,尽量起一些有意义的名字,否则会被管理员删除.因为服务器上 ...

  8. seek()方法的使用

    seek()方法用于移动文件读取指针到指定位置. file.seek()方法标准格式是:file.seek(offset,whence) offset:开始的偏移量,也就是代表需要移动偏移的字节数 w ...

  9. python 代码的缩进位置决定执行部分

    通过近期的代码训练,我发现同样的一个语句,在同一行缩进的位置不同,决定的执行部分也不同.先给一个例子(我想打出1-100中的前20个偶数) 比较第一张图和第二张图.第一张截图实现了我想要的功能.可是当 ...

  10. Spring Security构建Rest服务-1204-Spring Security OAuth开发APP认证框架之Token处理

    token处理之一基本参数配置 处理token时间.存储策略,客户端配置等 以前的都是spring security oauth默认的token生成策略,token默认在org.springframe ...