简单工厂模式又称静态工厂方法(Static Factory Method)模式,它不是Gof 所讲的23种设计模式之一,但是它却是我们在编码过程中经常使用的方法之一。
简单工厂模式是工厂模式中最简单的一种,它是一种实例化对象的方式,它可以用比较简单的方式隐藏创建对象的细节,一般只需要告诉工厂类一个参数,工厂类就会返回需要的产品类,但客户端看到的只是产品的抽象对象,无需关心到底是返回了哪个子类。客户端唯一需要知道的具体子类就是工厂子类。
简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。
如下图所示,工厂(Factory)可以生产三个产品(products):A,B,C,客户只需传递给工厂一个参数('a'、'b'、'c'),客户就会得到对应的产品,这样生产过程对客户不可见。
简单工厂模式图示
简单工厂模式包含如下角色:
工厂角色负责创建所有实例的内部逻辑。
抽象产品角色是所创建所有对象的父类,负责描述所有实例的公共接口。
具体产品角色是创建目标,所有创建的对象都充当这个角色的摸个具体类的实例。
优点:
缺点:
工厂类负责创建的对象比较少;
客户只知道传入工厂类的参数,对于如何创建对象(逻辑)不关心;
由于简单工厂很容易违反高内聚责任分配原则,因此一般只在很简单的情况下应用。
SimpleFactory
#-*- coding: utf-8 -*-
class Product:
'''
产品基类
'''
def detail(self):
print('This is the base class of produce')
class ProductA(Product):
'''
产品A
'''
def detail(self):
print('This is ProductA.')
class ProductB(Product):
'''
产品B
'''
def detail(self):
print('This is ProductB.')
class ProductC(Product):
'''
产品C
'''
def detail(self):
print('This is ProductC.')
class Factory:
'''
工厂类
'''
def produce(self, para):
if para == 'a':
return ProductA()
elif para == 'b':
return ProductB()
elif para == 'c':
return ProductC()
else:
print('The parameter you given is wrong.')
return None
def main():
factory = Factory()
productA = factory.produce('a')
productA.detail()
print('==================')
productB = factory.produce('b')
productB.detail()
print('==================')
productC = factory.produce('c')
productC.detail()
print('==================')
if __name__ == '__main__':
main()
运行结果:
Python运行结果
#ifndef PRODUCT_H
#define PRODUCT_H
#include <iostream>
// 产品基类
class Product
{
public:
Product() {}
// 这里析构函数定义成虚函数原因如下:
// 基类里有虚函数,定义了基类指针指向派生类,就会需要定义基类虚析构,这样,基类指针析构的时候,就会先析构派生类,再析构基类。
// 如果不定义虚析构,就会基类指针直接析构基类。这样派生类对象销毁不完整。编译器会出现如下警告:
// deleting object of polymorphic class type which has non_virtual destructor
virtual ~Product(){}
virtual void detail() const
{
std::cout << "This is the base class of product." << std::endl;
}
};
// 产品A
class ProductA : public Product
{
public:
ProductA() {}
~ProductA() {}
virtual void detail() const
{
std::cout << "This is ProductA." << std::endl;
}
};
// 产品B
class ProductB : public Product
{
public:
ProductB() {}
~ProductB() {}
virtual void detail() const
{
std::cout << "This is ProductB." << std::endl;
}
};
// 产品C
class ProductC : public Product
{
public:
ProductC() {}
~ProductC() {}
virtual void detail() const
{
std::cout << "This is ProductC." << std::endl;
}
};
#endif // PRODUCT_H
#ifndef FACTORY_H
#define FACTORY_H
#include "product.h"
class Factory
{
public:
Factory();
~Factory();
static Product* produce(char para);
};
#endif // FACTORY_H
#include "factory.h"
Factory::Factory()
{
}
Factory::~Factory()
{
}
Product *Factory::produce(char para)
{
if ('a' == para)
return new ProductA();
else if ('b' == para)
return new ProductB();
else if ('c' == para)
return new ProductC();
else
return NULL;
}
#include <iostream>
#include "factory.h"
using namespace std;
int main()
{
Product* productA = NULL;
productA = Factory::produce('a');
productA->detail();
cout << "==============" << endl;
Product* productB = NULL;
productB = Factory::produce('b');
productB->detail();
cout << "==============" << endl;
Product* productC = NULL;
productC = Factory::produce('c');
productC->detail();
cout << "==============" << endl;
delete productA;
delete productB;
delete productC;
return 0;
}
运行结果: