// factorypattern.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class IProduct
{
public:
IProduct() {};
virtual ~IProduct() {};
};
class IPhone :public IProduct
{
public:
IPhone() {};
virtual ~IPhone() {};
};
class IPad : public IProduct
{
public:
IPad() {};
virtual ~IPad() {};
};
class IFactory
{
public:
IFactory() {};
virtual ~IFactory(void) {};
virtual IProduct* getProduct() = 0;
};
class IPadFactory : public IFactory
{
public:
IPadFactory() {};
virtual ~IPadFactory() {};
virtual IProduct* getProduct()
{
return new IPad();
}
};
class IPhoneFactory :public IFactory
{
public:
IPhoneFactory() {};
virtual ~IPhoneFactory() {};
virtual IProduct *getProduct()
{
return new IPhone();
}
};
int main()
{
IFactory *pFac;
IProduct *pPro;
pFac = new IPadFactory();
pPro = pFac->getProduct();
pFac = new IPhoneFactory();
pPro = pFac->getProduct();
delete pFac;
pFac = NULL;
delete pPro;
pPro = NULL;
return 0;
}
工厂模式-c++实现
最新推荐文章于 2024-10-18 18:23:24 发布