c/cpp / 设计模式 / 简单工厂模式 -ag凯发k8国际
ag凯发k8国际
收集整理的这篇文章主要介绍了
c/cpp / 设计模式 / 简单工厂模式
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
---------------------------------------------
-- 时间:2019-03-19
-- 创建人:ruo_xiao
-- 邮箱:xclsoftware@163.com
---------------------------------------------
using namespace std;class virtualcar
{
public:virtualcar(void){};virtual ~virtualcar(void){};
public:virtual string carname() = 0;
};
#endif
#include
using namespace std;#include "source\virtualcar\virtualcar.h"
#include "source\carfactory\carfactory.hpp"int main()
{ virtualcar *pcarbase;carfactory carall;pcarbase = carall.getcar(1);cout<carname()<carname()<carname()<6、结果:
一、实质
本质是工厂实例创建并输出产品类实例的过程。
二、栗子
1、产品类(抽象类):汽车
#ifndef __virtualcar_h__ #define __virtualcar_h__#include2、产品实例:具体汽车
#ifndef __audicar_h__ #define __audicar_h__#include "../virtualcar/virtualcar.h" class audicar : public virtualcar { public:audicar(void){};~audicar(void){}; public:string carname(){return "my name is audi!";} };#endif#ifndef __porschecar_h__ #define __porschecar_h__#include "../virtualcar/virtualcar.h" class porschecar : public virtualcar { public:porschecar(void){};~porschecar(void){}; public:string carname(){return "my name is porsche!";} };#endif #ifndef __benzcar_h__ #define __benzcar_h__#include "../virtualcar/virtualcar.h"class benzcar : public virtualcar { public:benzcar(void){};~benzcar(void){}; public:string carname(){return "my name is benz!";} };#endif
3、工厂:汽车工厂
#ifndef __carfactory_h__ #define __carfactory_h__#include "../virtualcar/virtualcar.h" #include "../audicar/audicar.hpp" #include "../benzcar/benzcar.hpp" #include "../porschecar/porschecar.hpp"class carfactory { public:carfactory(void){paudi = new audicar();pbenz = new benzcar();pporsche = new porschecar();}~carfactory(void){delete paudi; paudi = nullptr;delete pbenz; pbenz = nullptr;delete pporsche; pporsche = nullptr;} private:audicar *paudi;benzcar *pbenz;porschecar *pporsche; public:virtualcar *getcar(int imode){switch (imode){case 1:return paudi;case 2:return pbenz;case 3:return pporsche;}return nullptr;} };#endif4、客户端
#include三、源码工程
https://download.csdn.net/download/itworld123/11036841
(saw:game over!)
总结
以上是ag凯发k8国际为你收集整理的c/cpp / 设计模式 / 简单工厂模式的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: