Factory Pattern
编程技术  /  houtizong 发布于 3年前   79
#ifndef _PRODUCT_H_#define _PRODUCT_H_#include <iostream>using namespace std;class Product{public: virtual ~Product()=0;protected: Product(); private:};class ConcreteProductA :public Product{public: ~ConcreteProductA(); ConcreteProductA();protected:private:};class ConcreteProductB :public Product{public: ~ConcreteProductB(); ConcreteProductB();protected:private:};#endif
#include "Product.h"#include <iostream>using namespace std;Product::Product(){}Product::~Product(){}ConcreteProductA::ConcreteProductA(){ cout << "ConcreteProductA....." <<endl;}ConcreteProductA::~ConcreteProductA(){}ConcreteProductB::ConcreteProductB(){ cout << "ConcreteProductB....." <<endl;}ConcreteProductB::~ConcreteProductB(){}
#ifndef FACTORY_H_#define FACTORY_H_#include "Product.h"#include <iostream>using namespace std;class Product;class Factory{public: virtual ~Factory()=0; virtual Product* creatProductA()=0; virtual Product* creatProductB()=0;protected: Factory();private:};class ConcreteFactory:public Factory{public: ConcreteFactory(); ~ConcreteFactory(); Product* creatProductA(); Product* creatProductB();protected:private:};#endif
#include "Factory.h"#include "Product.h"#include <iostream>using namespace std;Factory::Factory(){}Factory::~Factory(){}ConcreteFactory::ConcreteFactory(){ cout << "ConcreteFactory..."<<endl;}ConcreteFactory::~ConcreteFactory(){}Product* ConcreteFactory::creatProductA(){ return new ConcreteProductA();}Product* ConcreteFactory::creatProductB(){ return new ConcreteProductB();}
#include <iostream>#include "Factory.h"#include "Product.h"using namespace std;int main(){ Factory* factory = new ConcreteFactory(); Product* productA = factory->creatProductA(); Product* productB = factory->creatProductB(); return 0;}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接