学习笔记——抽象工厂模式Abstract Factory

时间:2023-01-27 23:49:27

学习笔记——抽象工厂模式Abstract Factory

在工厂模式的基础上,通过为工厂类增加接口,实现其他产品的生产,而不用一类产品就增加一个工厂。

依然以《真菌世界》游戏故事类比,树作为工厂,如果现在有两类树,一类生产快速弄真菌飞机和20毫米炮,一类生产慢速弄真菌飞机和10毫米炮。

产品类:

真菌飞机接口

IFungus.h

 #pragma once
class IFungus
{
public:
IFungus(void);
virtual ~IFungus(void);
public:
virtual void Fight()=;
};

IFungus.cpp

 #include "IFungus.h"

 IFungus::IFungus(void)
{
} IFungus::~IFungus(void)
{
}

真菌飞机A

FungusA.h

 #pragma once
#include "ifungus.h"
class CFungusA :
public IFungus
{
public:
CFungusA(void);
~CFungusA(void);
public:
void Fight();
};

FungusA.cpp

 #include "FungusA.h"
#include <iostream> CFungusA::CFungusA(void)
{
} CFungusA::~CFungusA(void)
{
} void CFungusA::Fight()
{
std::cout<<"快速攻击型"<<std::endl;
}

真菌飞机B

FungusB.h

 #pragma once
#include "ifungus.h"
class CFungusB :
public IFungus
{
public:
CFungusB(void);
~CFungusB(void);
public:
void Fight();
};

FungusB.cpp

 #include "FungusB.h"
#include <iostream> CFungusB::CFungusB(void)
{
} CFungusB::~CFungusB(void)
{
} void CFungusB::Fight()
{
std::cout<<"慢慢攻击型"<<std::endl;
}

炮接口

ICannon.h

 #pragma once
class ICannon
{
public:
ICannon(void);
virtual ~ICannon(void);
public:
virtual void Fire() = ;
};

ICannon.cpp

 #include "ICannon.h"

 ICannon::ICannon(void)
{
} ICannon::~ICannon(void)
{
}

炮A

CannonA.h

 #pragma once
#include "icannon.h"
class CCannonA :
public ICannon
{
public:
CCannonA(void);
~CCannonA(void);
public:
void Fire();
};

CannonA.cpp

 #include "CannonA.h"
#include <iostream> CCannonA::CCannonA(void)
{
} CCannonA::~CCannonA(void)
{
} void CCannonA::Fire()
{
std::cout<<"使用10毫米弹"<<std::endl;
}

炮B

CannonB.h

 #pragma once
#include "icannon.h"
class CCannonB :
public ICannon
{
public:
CCannonB(void);
~CCannonB(void);
public:
void Fire();
};

CannonB.cpp

 #include "CannonB.h"
#include <iostream> CCannonB::CCannonB(void)
{
} CCannonB::~CCannonB(void)
{
} void CCannonB::Fire()
{
std::cout<<"使用20毫米弹"<<std::endl;
}

工厂类:

树接口

ITree.h

 #pragma once
#include "IFungus.h"
#include "ICannon.h" class ITree
{
public:
ITree(void);
virtual ~ITree(void);
public:
virtual IFungus* GetFungus() = ;
virtual ICannon* GetCannon() = ;
};

ITree.cpp

 #include "ITree.h"

 ITree::ITree(void)
{
} ITree::~ITree(void)
{
}

树A

TreeA.h

 #pragma once
#include "itree.h"
class CTreeA :
public ITree
{
public:
CTreeA(void);
~CTreeA(void);
public:
IFungus* GetFungus();
ICannon* GetCannon();
};

TreeB.cpp

 #include "TreeA.h"
#include "FungusA.h"
#include "CannonB.h"; CTreeA::CTreeA(void)
{
} CTreeA::~CTreeA(void)
{
} IFungus* CTreeA::GetFungus()
{
return new CFungusA();
} ICannon* CTreeA::GetCannon()
{
return new CCannonB();
}

树B

TreeB.h

 #pragma once
#include "itree.h"
class CTreeB :
public ITree
{
public:
CTreeB(void);
~CTreeB(void);
public:
IFungus* GetFungus();
ICannon* GetCannon();
};

TreeB.cpp

 #include "TreeB.h"
#include "FungusB.h"
#include "CannonA.h" CTreeB::CTreeB(void)
{
} CTreeB::~CTreeB(void)
{
} IFungus* CTreeB::GetFungus()
{
return new CFungusB();
} ICannon* CTreeB::GetCannon()
{
return new CCannonA();
}

场景

main.cpp

 #include "ITree.h"
#include "TreeA.h"
#include "TreeB.h" int main()
{
ITree* pTreeA = new CTreeA();
ITree* pTreeB = new CTreeB(); pTreeA->GetFungus()->Fight();
pTreeA->GetCannon()->Fire(); pTreeB->GetFungus()->Fight();
pTreeB->GetCannon()->Fire(); delete pTreeB;
delete pTreeA; return ;
}

如果又有了其他类型的真菌飞机和炮,我们扩展相应的类型,然后创立相应的工厂(树)进行组合生产就行了。

其实很想通过构造函数传参数,通过参数来决定生产的是什么,但是这样的话,总觉得侵入式方法破坏了工厂类的自主权,干涉了这种设计模式的目的:不用关心具体产品,只要知道哪个工厂生产什么就行。