Abstract Factory
动机(motivation)
软件系统中,经常面临着“一系列相互依赖的对象的创建工作”;同时,由于需求的变化,往往存在更多系列对象的创建工作。
如何应对这种变化?如何绕过常规的对象创建方法(new),提供一种“封装机制”来避免客户程序和这种“多系列具体对象创建工作”的紧耦合。
模式定义
提供一个接口,让该接口负责创建一系列”相关或者相互依赖的对象“,无需指定它们具体的类。——《设计模式》GoF
要点总结
如果没有应对”多系列对象创建“的需求变化,则没有必要使用Abstract Factory模式,这时候使用简单的工厂即可。
”系列对象“指的是在某一个特定系列的对象之间有相互依赖、或作用的关系。不同系列的对象之间不能相互依赖。
Abstract Factory模式主要在于应用”新系列“的需求变动。其缺点在与难以应对”新对象“的需求变动。
代码结构
.
├── build.sh
├── clearBuild.sh
├── CMakeLists.txt
├── src
│ ├── examStu.cpp
│ ├── include
│ │ └── examStu.h
│ └── main.cpp
源码例子
examStu.h
#ifndef _EXANSTU__
#define _EXANSTU__
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class ShoesBase
{
public:
ShoesBase(){
cout << "ShoesBase() "<<endl;}
virtual ~ShoesBase(){
cout << "~ShoesBase() "<<endl;}
virtual void ShowTag()=0;
private:
int _shoesBase;
};
class ClotheBase
{
public:
ClotheBase(){
cout << "ClotheBase() "<<endl;}
virtual ~ClotheBase(){
cout << "~ClotheBase() "<<endl;}
virtual void ShowTag()=0;
private:
int _clotheBase;
};
class NikeShoes :public ShoesBase
{
public:
NikeShoes(){
cout << "NikeShoes() "<<endl;}
virtual ~NikeShoes(){
cout << "~NikeShoes() "<<endl;}
void ShowTag() override {
cout << "NikeShoes::ShowTag() called"<<endl;}
private:
};
class NikeClothe :public ClotheBase
{
public:
NikeClothe(){
cout << "NikeClothe() "<<endl;}
virtual ~NikeClothe(){
cout << "~NikeClothe() "<<endl;}
void ShowTag() override {
cout << "Nike Clothe::ShowTag() called"<<endl;}
private:
};
class AdidasShoes: public ShoesBase
{
public:
AdidasShoes(){
cout << "AdidasShoes() "<<endl;}
virtual ~AdidasShoes(){
cout << "~AdidasShoes() "<<endl;}
void ShowTag() override {
cout << "AdidasShoes::ShowTag() called"<<endl;}
};
class AdidasClothe :public ClotheBase
{
public:
AdidasClothe(){
cout << "AdidasClothe() "<<endl;}
virtual ~AdidasClothe(){
cout << "~AdidasClothe() "<<endl;}
void ShowTag() override {
cout << "AdidasClothe::ShowTag() called"<<endl;}
private:
};
class HuiLiShoes: public ShoesBase
{
public:
HuiLiShoes(){
cout << "HuiLiShoes() "<<endl;}
virtual ~HuiLiShoes(){
cout << "~HuiLiShoes() "<<endl;}
void ShowTag() override {
cout << "HuiLiShoes::ShowTag() called"<<endl;}
};
class HuiLiClothe :public ClotheBase
{
public:
HuiLiClothe(){
cout << "HuiLiClothe() "<<endl;}
virtual ~HuiLiClothe(){
cout << "~HuiLiClothe() "<<endl;}
void ShowTag() override {
cout << "HuiLiClothe::ShowTag() called"<<endl;}
private:
};
class ShoesFactory
{
public:
ShoesFactory(){
cout << "ShoesFactory() "<<endl;}
virtual ~ShoesFactory(){
cout << "~ShoesFactory() "<<endl;}
virtual std::shared_ptr<ShoesBase> CreateShoes() = 0;
virtual std::shared_ptr<ClotheBase> CreateClothe() = 0;
};
class NikeShoesFactory :public ShoesFactory
{
public:
NikeShoesFactory(){
cout << "NikeShoesFactory() "<<endl;}
virtual ~NikeShoesFactory(){
cout << "~NikeShoesFactory() "<<endl;}
std::shared_ptr<ShoesBase> CreateShoes() override{
std::shared_ptr<ShoesBase> pNikeShoes = std::make_shared<NikeShoes>();
return pNikeShoes;
}
std::shared_ptr<ClotheBase> CreateClothe() override{
std::shared_ptr<ClotheBase> pNikeClothe = std::make_shared<NikeClothe>();
return pNikeClothe;
}
private:
};
class AdidasShoesFactory :public ShoesFactory
{
public:
AdidasShoesFactory(){
cout << "AdidasShoesFactory() "<<endl;}
virtual ~AdidasShoesFactory(){
cout << "~AdidasShoesFactory() "<<endl;}
std::shared_ptr<ShoesBase> CreateShoes() override {
std::shared_ptr<ShoesBase> pAdidasShoes = std::make_shared<AdidasShoes>();
return pAdidasShoes;
}
std::shared_ptr<ClotheBase> CreateClothe() override{
std::shared_ptr<ClotheBase> pAdidasClothe = std::make_shared<AdidasClothe>();
return pAdidasClothe;
}
private:
};
class HuiLiShoesFactory :public ShoesFactory
{
public:
HuiLiShoesFactory(){
cout << "HuiLiShoesFactory() "<<endl;}
virtual ~HuiLiShoesFactory(){
cout << "~HuiLiShoesFactory() "<<endl;}
std::shared_ptr<ShoesBase> CreateShoes() override{
std::shared_ptr<ShoesBase> pHuiLiShoes = std::make_shared<HuiLiShoes>();
return pHuiLiShoes;
}
std::shared_ptr<ClotheBase> CreateClothe() override{
std::shared_ptr<ClotheBase> pHuiLiClothe = std::make_shared<HuiLiClothe>();
return pHuiLiClothe;
}
private:
};
#endif
main.cpp
#include <iostream>
#include <string.h>
#include <memory>
#include "examStu.h"
using namespace std;
int main(int argc, char *argv[])
{
auto pAdidasShoesFactory = std::make_shared<AdidasShoesFactory>();
std::shared_ptr<ShoesBase> pAdidasshoes = pAdidasShoesFactory->CreateShoes();
std::shared_ptr<ClotheBase> pAdidasClothe = pAdidasShoesFactory->CreateClothe();
pAdidasshoes->ShowTag();
pAdidasClothe->ShowTag();
return 0;
}