概念:
抽象工厂模式是一种创建型模式,和工厂模式一样也是为了软件的可扩展性的一种设计模式。通过名字也可以知道抽象工厂模式和工厂模式有一定的关系。小编这里理解抽象工厂模式就是工厂模式的扩展。抽象工厂是将一系列相关的产品进行创建的一种模式。而工厂模式单一产品的创建。
这里有个理解误区就是抽象,这里的抽象指的是产品的抽象,和代码的抽象没有任何关系。
使用场景:
1、代码需要与多个不同系列的相关产品交互,并且不知道后续是否有代码扩展。
实现方法:
1、为所有的产品声明抽象接口,然后让所有的具体产品类实现。
2、声明抽象工厂接口,并且在接口中为所有的抽象产品提供一组构建方法。
3、为每种产品变体实现一个具体的工厂类。
关系图:
代码:
#include <iostream>
#include "DanLiMoShi.h"
#include "ChouXiangGongChangMoshi.h"
int main()
{
std::cout << "欢迎东哥来到设计模式的世界!\n";
cout << "汽车:" << endl;
ChouXiangfactory* cxCarFactory = new CarCXFactory();
ProductBase* carBase = cxCarFactory->createProductBase();
cout << "createProductBase:" << carBase->m_name << endl;
ManualProduct* carManualBase = cxCarFactory->createProductManual();
cout << "createProductBase:" << carManualBase->m_name << endl;
cout << "手机:" << endl;
ChouXiangfactory* cxPhoneFactory = new PhoneCXFactory();
ProductBase* xPhoneBase = cxPhoneFactory->createProductBase();
cout << "createProductBase:" << carBase->m_name << endl;
ManualProduct* xPhoneManualBase = cxPhoneFactory->createProductManual();
cout << "createProductBase:" << xPhoneManualBase->m_name << endl;
}
#pragma once
#include <string>
using namespace std;
class ProductBase;
class ManualProduct;
class ChouXiangfactory
{
public:
ChouXiangfactory();
~ChouXiangfactory();
virtual ProductBase* createProductBase();
virtual ManualProduct* createProductManual();
};
class CarCXFactory :public ChouXiangfactory {
public:
CarCXFactory();
~CarCXFactory();
ProductBase* createProductBase() override;
ManualProduct* createProductManual() override;
};
class PhoneCXFactory :public ChouXiangfactory {
public:
PhoneCXFactory();
~PhoneCXFactory();
ProductBase* createProductBase() override;
ManualProduct* createProductManual() override;
};
class ProductBase {
public:
ProductBase(string name) { m_name = name; };
~ProductBase() {};
string m_name;
};
class ManualProduct {
public:
ManualProduct(string name) { m_name = name; };
~ManualProduct() {};
string m_name;
};
class CarProductBase :public ProductBase {
public:
CarProductBase(string name) :ProductBase(name) { m_name = name; };
~CarProductBase() {};
string m_name;
};
class PhoneProductBase :public ProductBase {
public:
PhoneProductBase(string name) :ProductBase(name) { m_name = name; };
~PhoneProductBase() {};
string m_name;
};
class CarManualProduct :public ManualProduct {
public:
CarManualProduct(string name) :ManualProduct(name) { m_name = name; };
~CarManualProduct() {};
string m_name;
};
class PhoneManualProduct :public ManualProduct {
public:
PhoneManualProduct(string name) :ManualProduct(name) { m_name = name; };
~PhoneManualProduct() {};
string m_name;
};
#include "ChouXiangGongChangMoShi.h"
ChouXiangfactory::ChouXiangfactory()
{
}
ChouXiangfactory::~ChouXiangfactory()
{
}
ProductBase* ChouXiangfactory::createProductBase()
{
return nullptr;
}
ManualProduct* ChouXiangfactory::createProductManual()
{
return nullptr;
}
CarCXFactory::CarCXFactory()
{
}
CarCXFactory::~CarCXFactory()
{
}
ProductBase* CarCXFactory::createProductBase()
{
return new CarProductBase("汽车实体");
}
ManualProduct* CarCXFactory::createProductManual()
{
return new CarManualProduct("汽车说明书");
}
PhoneCXFactory::PhoneCXFactory()
{
}
PhoneCXFactory::~PhoneCXFactory()
{
}
ProductBase* PhoneCXFactory::createProductBase()
{
return new PhoneProductBase("手机实体");
}
ManualProduct* PhoneCXFactory::createProductManual()
{
return new PhoneManualProduct("手机说明书");
}