Factory Pattern

编程技术  /  houtizong 发布于 3年前   79
[img]C:\Documents and Settings\Administrator\桌面\编程\设计模式\Factory.jpg[/img]

刚开始学习设计模式,还不是很懂,慢慢来吧,首先应该是Factory Pattern,下面是代码:
Product.h
#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



Product.cpp

#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(){}


Factory.h

#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


Factory.cpp

#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();}


main.cpp

#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];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客