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. Union Find-547. Friend Circles

    There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...

  2. 服务器 apache配置https,http强制跳转https(搭建http与https共存)

    公司linux服务器上的nginx的已经改成https了,现在还剩下一个windows云服务器没配置. 环境 windows wampserver2.5 64位 1.腾讯云申请的ssl 包含三个文件: ...

  3. pymongo学习第1篇——增删改查

    参考文档: 1.https://docs.mongodb.org/getting-started/python/ 2.http://api.mongodb.org/python/current/api ...

  4. grafana 运行

    1,下载好项目,然后进入到目录 键入: ./bin/grafana-server web 运行 https://www.waitig.com/grafana-config-and-run.html 2 ...

  5. centOS 自动锁屏 解决办法

    System-->preferences --> Screensaver中 找到 Lock screen when screensaver is active 把前面的钩去掉

  6. JavaScript函数学习总结(一)---函数定义

    博客原文地址:Claiyre的个人博客 如需转载,请在文章开头注明原文地址 在许多传统的OO语言中,对象可以包含数据,还可拥有方法,也就是属于该对象的函数.但在JavaScript中,函数也被认为是一 ...

  7. PHP 批量获取 百度搜索结果 网址列表

    <?php set_time_limit(0); function curl($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $u ...

  8. sql返回行id

    1.sql语句中 insert into tableName() output inserted.id values() 2 .存储过程中 ALTER PROCEDURE dbo.getBuyMedi ...

  9. ES6-Proxy and Reflect

    依赖文件地址 :https://github.com/chanceLe/ES6-Basic-Syntax/tree/master/js <!DOCTYPE html> <html&g ...

  10. 带你了解数据库中group by的用法

    前言 本章主要介绍数据库中group by的用法,也是我们在使用数据库时非常基础的一个知识点.并且也会涉及Join的使用,关于Join的用法,可以看我写的上一篇文章:带你了解数据库中JOIN的用法如有 ...