#pragma once
//product.h
#ifndef PRODUCT_H
#define PRODUCT_H
#include
using namespace std;
class Product
{
public:
Product() {}
~Product(){}
virtual voID name() = 0;
};
class Product1:public Product
{
public:
Product1() {}
~Product1(){}
voID name() { cout << "Product1" << endl; }
};
class Product2:public Product
{
public:
Product2() {}
~Product2(){}
voID name() { cout << "Product2" << endl; }
};
#endif // !PRODUCT_H
#pragma once
//factory.h
#ifndef FACTORY_H
#define FACTORY_H
#include "product.h"
class Factory
{
public:
Factory() {}
~Factory(){}
virtual Product* Create() = 0;
};
class ConcreteFactory1 :public Factory
{
public:
ConcreteFactory1() {}
~ConcreteFactory1() {}
Product*Create() { return new Product1; }
};
class ConcreteFactory2 :public Factory
{
public:
ConcreteFactory2() {}
~ConcreteFactory2() {}
Product*Create() { return new Product2; }
};
#endif // !FACTORY_H
//main.cpp
#include
#include "factory.h"
int main()
{
cout << "生产开始"<< endl;
Factory* pFactory1 = new ConcreteFactory1();
Product* pProduct1 = pFactory1->Create();
pProduct1->name();
Factory* pFactory2 = new ConcreteFactory2();
Product* pProduct2 = pFactory2->Create();
pProduct2->name();
delete pFactory1;
pFactory1 = NulL;
delete pFactory2;
pFactory2 = NulL;
system("pause");
return 0;
}
总结以上是内存溢出为你收集整理的Factory Method工厂方法C++版全部内容,希望文章能够帮你解决Factory Method工厂方法C++版所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)