设计模式读书笔记:Abstract Factory(抽象工厂)
2015-07-03 10:28:09
意图:
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
结构图:来自 《23种设计模式 - 郗晓勇》
实现:https://github.com/panshiqu/patterns/tree/master/AbstractFactory
AbstractFactory
namespace NS_ABSTRACT_FACTORY {
class AbstractProductA;
class AbstractProductB;
class AbstractFactory {
public:
AbstractFactory() {}
virtual ~AbstractFactory() {}
enum FACTORY_TYPE {
FACTORY_ONE,
FACTORY_TWO,
};
static AbstractFactory *getFactory(int type);
virtual AbstractProductA *createProductA() = 0;
virtual AbstractProductB *createProductB() = 0;
};
} /* namespace NS_ABSTRACT_FACTORY */
#include "AbstractFactory.h"
#include "ConcreteFactory1.h"
#include "ConcreteFactory2.h"
namespace NS_ABSTRACT_FACTORY {
AbstractFactory *AbstractFactory::getFactory(int type)
{
if (FACTORY_ONE == type)
return new ConcreteFactory1();
else if (FACTORY_TWO == type)
return new ConcreteFactory2();
else return 0;
}
} /* namespace NS_ABSTRACT_FACTORY */
ConcreteFactory1
#include "AbstractFactory.h"
namespace NS_ABSTRACT_FACTORY {
class ConcreteFactory1 : public AbstractFactory
{
public:
ConcreteFactory1() {}
virtual ~ConcreteFactory1() {}
virtual AbstractProductA *createProductA();
virtual AbstractProductB *createProductB();
};
} /* namespace NS_ABSTRACT_FACTORY */
#include "ConcreteFactory1.h"
#include "ConcreteProductA1.h"
#include "ConcreteProductB1.h"
namespace NS_ABSTRACT_FACTORY {
AbstractProductA *ConcreteFactory1::createProductA()
{
return new ConcreteProductA1();
}
AbstractProductB *ConcreteFactory1::createProductB()
{
return new ConcreteProductB1();
}
} /* namespace NS_ABSTRACT_FACTORY */
ConcreteFactory2
#include "AbstractFactory.h"
namespace NS_ABSTRACT_FACTORY {
class ConcreteFactory2 : public AbstractFactory
{
public:
ConcreteFactory2() {}
virtual ~ConcreteFactory2() {}
virtual AbstractProductA *createProductA();
virtual AbstractProductB *createProductB();
};
} /* namespace NS_ABSTRACT_FACTORY */
#include "ConcreteFactory2.h"
#include "ConcreteProductA2.h"
#include "ConcreteProductB2.h"
namespace NS_ABSTRACT_FACTORY {
AbstractProductA *ConcreteFactory2::createProductA()
{
return new ConcreteProductA2();
}
AbstractProductB *ConcreteFactory2::createProductB()
{
return new ConcreteProductB2();
}
} /* namespace NS_ABSTRACT_FACTORY */
AbstractProductA
namespace NS_ABSTRACT_FACTORY {
class AbstractProductA {
public:
AbstractProductA() {}
virtual ~AbstractProductA() {}
virtual void printSelf(void) = 0;
};
} /* namespace NS_ABSTRACT_FACTORY */
ConcreteProductA1
#include "AbstractProductA.h"
#include <iostream>
namespace NS_ABSTRACT_FACTORY {
class ConcreteProductA1 : public AbstractProductA
{
public:
ConcreteProductA1() {}
virtual ~ConcreteProductA1() {}
virtual void printSelf(void)
{
std::cout << "I'm ConcreteProductA1." << std::endl;
}
};
} /* namespace NS_ABSTRACT_FACTORY */
ConcreteProductA2
#include "AbstractProductA.h"
#include <iostream>
namespace NS_ABSTRACT_FACTORY {
class ConcreteProductA2 : public AbstractProductA
{
public:
ConcreteProductA2() {}
virtual ~ConcreteProductA2() {}
virtual void printSelf(void)
{
std::cout << "I'm ConcreteProductA2." << std::endl;
}
};
} /* namespace NS_ABSTRACT_FACTORY */
AbstractProductB
namespace NS_ABSTRACT_FACTORY {
class AbstractProductB {
public:
AbstractProductB() {}
virtual ~AbstractProductB() {}
virtual void printSelf(void) = 0;
};
} /* namespace NS_ABSTRACT_FACTORY */
ConcreteProductB1
#include "AbstractProductB.h"
#include <iostream>
namespace NS_ABSTRACT_FACTORY {
class ConcreteProductB1 : public AbstractProductB
{
public:
ConcreteProductB1() {}
virtual ~ConcreteProductB1() {}
virtual void printSelf(void)
{
std::cout << "I'm ConcreteProductB1." << std::endl;
}
};
} /* namespace NS_ABSTRACT_FACTORY */
ConcreteProductB2
#include "AbstractProductB.h"
#include <iostream>
namespace NS_ABSTRACT_FACTORY {
class ConcreteProductB2 : public AbstractProductB
{
public:
ConcreteProductB2() {}
virtual ~ConcreteProductB2() {}
virtual void printSelf(void)
{
std::cout << "I'm ConcreteProductB2." << std::endl;
}
};
} /* namespace NS_ABSTRACT_FACTORY */
main
#include "AbstractFactory/AbstractFactory.h"
#include "AbstractFactory/AbstractProductA.h"
#include "AbstractFactory/AbstractProductB.h"
using namespace NS_ABSTRACT_FACTORY;
int main(void)
{
AbstractFactory *factory = AbstractFactory::getFactory(AbstractFactory::FACTORY_TWO);
AbstractProductA *producta = factory->createProductA();
AbstractProductB *productb = factory->createProductB();
producta->printSelf();
productb->printSelf();
delete factory;
delete producta;
delete productb;
}
附加:
场景1:实现一个商城,售卖不同种类商品,手机,果蔬。关于一个商品它们固有商品编号,商品名称,商品介绍,关键字等等,但是手机附加的一些属性譬如手机网络,手机材质等等,相比果蔬则可能会具有保质期等属性,至少手机品类附加的那些属性果蔬基本都没有,这时我采取的做法是不同品类有共同父类:Product,数据库有type字段标识属于那个品类,我在数据库加载的时候根据type实例化不同品类商品实例,这时候应该符合简单工厂模式的场景。结构图不再提供了,仔细看一下抽象工厂的结构图,仅有一个工厂,且不再是抽象的,不再有ProductB相关系列产品,仅有一个ProductA系列产品,工厂负责实例化ProductA1和ProductA2等等,这就是简单工厂。