1 简单工厂模式
所谓简单工厂模式,是一种实例化对象的方式,只要输入需要实例化对象的名字,就可以通过工厂对象的相应工厂函数来制造你需要的对象。
简单工厂模式的核心是,对于一个父类的多个继承子类,工厂对象的工厂函数根据用户输入,自动new出一个子类对象并返回其父类的指针,这样利用父类的指针执行父类的虚函数,就可以动态绑定子类的重写函数,从而实现多态。打个比方,假设有一个工厂,他能生产出A、B两种产品。当客户需要产品的时候一定要告诉共产是哪种产品,是A还是B。当新增加一种新产品的时候,那么就要去修改工厂的类
1.1 代码实现1
#include<iostream>
using namespace std;
class Product
{
public:
virtual void show() = 0;
};
class Product_A : public Product
{
public:
void show()
{
cout << "Product_A" << endl;
}
};
class Product_B : public Product
{
public:
void show()
{
cout << "Product_B" << endl;
}
};
class Factory
{
public:
Product* Create(int i)
{
switch (i)
{
case 1:
return new Product_A;
break;
case 2:
return new Product_B;
break;
default:
break;
}
}
};
int main()
{
Factory *factory = new Factory();
factory->Create(1)->show();
factory->Create(2)->show();
system("pause");
return 0;
}
1.2 代码实现2
#ifndef __SIMPLEFACTORY__H
#define __SIMPLEFACTORY__H
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef void* (*Constructor)();
class Api
{
public:
virtual void test(string s) = 0;
protected:
Api() {}
};
class ApiImplOne :public Api
{
public:
void test(string s)
{
cout << "ApiImplOne:" << s << endl;
}
};
class ApiImplTwo :public Api
{
public:
void test(string s)
{
cout << "ApiImplTwo:" << s << endl;
}
};
class CobjectFactory
{
public:
static void RegisterClass(const string className, Constructor constructor)
{
constructors()[className] = constructor;
}
static void* CreateObject(const string& className)
{
Constructor constructor = NULL;
if (constructors().find(className) != constructors().end())
{
constructor = constructors().find(className)->second;
}
if (constructor == NULL)
return NULL;
return constructor();
}
private:
inline static std::map<std::string, Constructor>& constructors()
{
static std::map<std::string, Constructor> instance;
return instance;
}
};
class AutoFactory
{
public:
static Api* CreateApi()
{
Api* p = NULL;
//p = static_cast<Api*>(CobjectFactory::CreateObject("Impltwo"));
p = static_cast<Api*>(CobjectFactory::CreateObject("ImplOne"));
return p;
}
};
#define REG_CLASS(className) \
class className##Helper \
{ \
public: \
className##Helper() \
{ \
CobjectFactory::RegisterClass(#className, className##Helper::CreateObjFunc); \
} \
static void* CreateObjFunc() \
{ \
if(#className == "Impltwo") \
return new ApiImplTwo; \
else \
return new ApiImplOne; \
} \
}; \
className##Helper className##helper;
#endif
1.3 测试
#include "SimpleFactory.h"
// REG_CLASS(Impltwo)
REG_CLASS(ImplOne)
int main()
{
//Api* p = Factory::CreateApi(1);
//Api* p = Factory::CreateApi(2);
//ApiImplTwo* p = static_cast<ApiImplTwo*>(ImpltwoHelper::CreateObjFunc());
Api* p = AutoFactory::CreateApi();
p->test("test");
return 0;
}
2 工厂设计模式
上面的简单工厂模式的缺点是当新增产品的时候就要去修改工厂的类,这就违反了开放封闭原则,(类、模块、函数)可以扩展,但是不可以修改,于是,就出现了工厂方法模式。所谓工厂方法模式,是指定义一个用于创建对象的接口,让子类决定实例化哪一个类。
打个比方现在有A、B两种产品,那么久开两个工厂。工厂A负责生产A产品,工厂B负责生产B种产品。这时候客户不需要告诉共产生产哪种产品了,只需要告诉共产生产就可以了。
2.1 代码实现1
#include "stdafx.h"
#include<iostream>
using namespace std;
class Product
{
public:
virtual void show() = 0;
};
class Product_A : public Product
{
public:
void show()
{
cout << "Product_A" << endl;
}
};
class Product_B : public Product
{
public:
void show()
{
cout << "Product_B" << endl;
}
};
class Factory
{
public:
virtual Product* create() = 0;
};
class Factory_A : public Factory
{
public:
Product* create()
{
return new Product_A;
}
};
class Factory_B : public Factory
{
public:
Product* create()
{
return new Product_B;
}
};
int main()
{
Factory_A* productA = new Factory_A();
Factory_B* productB = new Factory_B();
productA->create()->show();
productB->create()->show();
system("pause");
return 0;
}
2.2 代码实现2
#include <iostream>
#include <string>
#include <memory>
#include <map>
#include <functional>
using namespace std;
class ExportFileApi
{
public:
virtual bool ExportData(string data) = 0;
protected:
ExportFileApi(){}
};
class ExportTextFile:public ExportFileApi
{
public:
bool ExportData(string data)
{
cout << "export " << data << endl;
return true; }
};
class ExportDB :public ExportFileApi
{
public:
bool ExportData(string data)
{
cout << "export " << data << endl;
return true;
}
};
class ExportOperate
{
public:
bool ExportData(string data)
{
ExportFileApi* pApi = FactoryMethod();
return pApi->ExportData(data);
}
protected:
virtual ExportFileApi* FactoryMethod() = 0;
};
class ExportTextFileOperate :public ExportOperate
{
protected:
ExportFileApi* FactoryMethod()
{
return new ExportTextFile();
}
};
class ExportDBOperate :public ExportOperate
{
protected:
ExportFileApi* FactoryMethod()
{
return new ExportDB;
}
};
int main()
{
ExportOperate* p1 = new ExportTextFileOperate;
p1->ExportData("TEXT");
ExportOperate* p2 = new ExportDBOperate;
p2->ExportData("DB");
return 0;
}
3 抽象工厂设计模式
为什么要有抽象工厂模式,假如我们A产品中有A1和A2两种型号的厂品,B产品中有B1和B2两种型号的厂品,那怎么办,上面两种工厂模式就不能解决了。这个时候抽象工厂模式就登场了。还是开设两家工厂,工厂A负责生产A1 、A2型号产品,B工厂负责生产B1、B2型号的产品。
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。 适用性:一个系统要独立于它的产品的创建、组合和表示时。一个系统要由多个产品系列中的一个来配置时。当你要强调一系列相关的产品对象的设计以便进行联合使用时。当你提供一个产品类库,而只想显示它们的接口而不是实现时。
#include <iostream>
using namespace std;
//定义抽象类
class product1
{
public:
virtual void show() = 0;
};
//定义具体类
class product_A1 :public product1
{
public:
void show(){ cout << "product A1" << endl; }
};
class product_B1 :public product1
{
public:
void show(){ cout << "product B1" << endl; }
};
//定义抽象类
class product2
{
public:
virtual void show() = 0;
};
//定义具体类
class product_A2 :public product2
{
public:
void show(){ cout << "product A2" << endl; }
};
class product_B2 :public product2
{
public:
void show(){ cout << "product B2" << endl; }
};
class Factory
{
public:
virtual product1 *creat1() = 0;
virtual product2 *creat2() = 0;
};
class FactoryA:public Factory
{
public:
product1 *creat1(){ return new product_A1(); }
product2 *creat2(){ return new product_A2(); }
};
class FactoryB:public Factory
{
public:
product1 *creat1(){ return new product_B1(); }
product2 *creat2(){ return new product_B2(); }
};
int main()
{
FactoryA *factoryA = new FactoryA();
factoryA->creat1()->show();
factoryA->creat2()->show();
FactoryB *factoryB = new FactoryB();
factoryB->creat1()->show();
factoryB->creat2()->show();
return 0;
}