抽象工厂(Abstract Factory)模式提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。抽象工厂模式又称为Kit模式,属于对象创建型模式。
抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式。抽象工厂模式可以向客户端提供一个接口,使客户端在不必指定产品的具体的情况下,创建多个产品族中的产品对象。根据里氏替换原则,任何接受父类型的地方,都应当能够接受子类型。因此,实际上系统所需要的,仅仅是类型与这些抽象产品角色相同的一些实例,而不是这些抽象产品的实例。换言之,也就是这些抽象产品的具体子类的实例。工厂类负责创建抽象产品的具体子类的实例。
为了更清晰地理解抽象工厂模式,需要引入两个概念:
现有两个产品族ProductA和ProductB,A1、A2与ProductA在同一产品等级结构中,B1、B2与ProductB在同一产品等级。现有两个工厂FactoryA与FactoryB,FactoryA可以生产A1、B1,Factory可以生产A2、B2。
如果要生产A1,客户需要知道生产A1的工厂,并且给其特定的参数,FactoryA才可以生产出A1。
图解抽象工厂模式
工厂方法模式包含如下角色:
优点:
缺点:
在以下情况下可以使用抽象工厂模式:
FactoryMethodUml
#ifndef PRODUCT_H
#define PRODUCT_H
#include <iostream>
// 产品基类
class ProductA
{
public:
ProductA() {}
virtual ~ProductA(){}
virtual void detail() const
{
std::cout << "This is the base class of productA." << std::endl;
}
};
class ProductB
{
public:
ProductB() {}
virtual ~ProductB(){}
virtual void detail() const
{
std::cout << "This is the base class of productB." << std::endl;
}
};
// 产品A1
class ProductA1 : public ProductA
{
public:
ProductA1() {}
~ProductA1() {}
virtual void detail() const
{
std::cout << "This is ProductA1." << std::endl;
}
};
// 产品A2
class ProductA2 : public ProductA
{
public:
ProductA2() {}
~ProductA2() {}
virtual void detail() const
{
std::cout << "This is ProductA2." << std::endl;
}
};
// 产品B1
class ProductB1 : public ProductB
{
public:
ProductB1() {}
~ProductB1() {}
virtual void detail() const
{
std::cout << "This is ProductB1." << std::endl;
}
};
// 产品B2
class ProductB2 : public ProductB
{
public:
ProductB2() {}
~ProductB2() {}
virtual void detail() const
{
std::cout << "This is ProductB2." << std::endl;
}
};
#endif // PRODUCT_H
#ifndef FACTORY_H
#define FACTORY_H
#include "product.h"
// 工厂基类
class Factory
{
public:
Factory(){}
virtual ~Factory(){}
static ProductA* produceA()
{
return NULL;
}
static ProductB* produceB()
{
return NULL;
}
};
// 工厂A
class FactoryA : public Factory
{
public:
FactoryA();
virtual ~FactoryA();
static ProductA* produceA()
{
return new ProductA1();
}
static ProductB* produceB()
{
return new ProductB1();
}
};
// 工厂B
class FactoryB : public Factory
{
public:
FactoryB();
virtual ~FactoryB();
static ProductA* produceA()
{
return new ProductA2();
}
static ProductB* produceB()
{
return new ProductB2();
}
};
#endif // FACTORY_H
#include <iostream>
#include "factory.h"
using namespace std;
int main()
{
cout << "There are what FactoryA produces." << endl;
ProductA* productA1 = NULL;
productA1 = FactoryA::produceA();
productA1->detail();
cout << "======================" << endl;
ProductB* productB1 = NULL;
productB1 = FactoryA::produceB();
productB1->detail();
cout << "======================" << endl;
cout << "There are what FactoryB produces." << endl;
ProductA* productA2 = NULL;
productA2 = FactoryB::produceA();
productA2->detail();
cout << "======================" << endl;
ProductB* productB2 = NULL;
productB2 = FactoryB::produceB();
productB2->detail();
cout << "======================" << endl;
delete productA1;
delete productB1;
delete productA2;
delete productB2;
return 0;
}
运行结果:
C++运行结果
#-*- coding: utf-8 -*-
'''
抽象工厂模式
'''
class ProductA:
'''
产品A基类
'''
def detail(self):
print('This is the base class of productA')
class ProductB:
'''
产品B基类
'''
def detail(self):
print('This is the base class of productB')
class ProductA1(ProductA):
'''
产品A1
'''
def detail(self):
print('This is ProductA1.')
class ProductA2(ProductA):
'''
产品A2
'''
def detail(self):
print('This is ProductA2.')
class ProductB1(ProductB):
'''
产品B1
'''
def detail(self):
print('This is ProductB1.')
class ProductB2(ProductB):
'''
产品B2
'''
def detail(self):
print('This is ProductB2.')
class Factory:
'''
工厂基类
'''
def produceA(self):
return None
def produceB(self):
return None
class FactoryA(Factory):
'''
工厂A
'''
def produceA(self):
return ProductA1()
def produceB(self):
return ProductB1()
class FactoryB(Factory):
'''
工厂B
'''
def produceA(self):
return ProductA2()
def produceB(self):
return ProductB2()
def main():
print('There are what FactoryA produces.')
fA = FactoryA()
productA1 = fA.produceA()
productA1.detail()
print('======================')
productB1 =fA.produceB()
productB1.detail()
print('======================')
print('There are what FactoryB produces.')
fB = FactoryB()
productA2 = fB.produceA()
productA2.detail()
print('======================')
productB2 =fB.produceB()
productB2.detail()
print('======================')
if __name__ == '__main__':
main()
运行结果: