记录一个工厂模式简单的实现,其中COrder是最基类。
#pragma once
struct IFactory
{
virtual COrder* CreateOrder() = 0;
};
template<typename T>
class CFactoryOrder :public IFactory
{
public:
static IFactory* GetInstance()
{
static CFactoryOrder FactoryOrder;
return &FactoryOrder;
}
CFactoryOrder(){}
virtual ~CFactoryOrder(){}
private:
virtual COrder* CreateOrder() { return new T(); }
};
下面是用法
CFactoryOrder<COrderChild>::GetInstance()->CreateOrder();