feat: add "Factory Method" and "Abstract Factory"
This commit is contained in:
parent
8ea61d1bc8
commit
f5724bf850
1
Studio/DesignPatterns/AbstractFactory.cpp
Normal file
1
Studio/DesignPatterns/AbstractFactory.cpp
Normal file
@ -0,0 +1 @@
|
|||||||
|
#include "AbstractFactory.h"
|
141
Studio/DesignPatterns/AbstractFactory.h
Normal file
141
Studio/DesignPatterns/AbstractFactory.h
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
/*******************************************************************
|
||||||
|
* Copyright (c) 2022~2023 XMuli All rights reserved.
|
||||||
|
* GitHub: https://github.com/XMuli
|
||||||
|
* Description: 抽象工厂模式(Abstract Factory)
|
||||||
|
* Up产品 up1、up2 两款式产品,Down 有 down1、down2 两款式产品;
|
||||||
|
* 每个工厂一次只生产 Up + Down 系列产品,即可组成套装出售;比如
|
||||||
|
* AFactory 生产 up1 + down1; BFactory 生产 up2 + down2;
|
||||||
|
* CFactory 生产 up1 + down2; 等各种组合
|
||||||
|
* See: https://blog.csdn.net/silangquan/article/details/20492293
|
||||||
|
******************************************************************/
|
||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
/***************************************************************************
|
||||||
|
每一个具体的 Factory 类,都可以创建多款 product,但是每款只能够生成 1 个(对象)
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
class IUpProduct
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~IUpProduct() = default;
|
||||||
|
virtual void doSomeThing() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Up1Product : public IUpProduct
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~Up1Product() = default;
|
||||||
|
virtual void doSomeThing() override {
|
||||||
|
cout << "this is 1 UpProduct, do some thing..." << endl;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Up2Product : public IUpProduct
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~Up2Product() = default;
|
||||||
|
virtual void doSomeThing() override {
|
||||||
|
cout << "this is 2 UpProduct, do some thing..." << endl;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class IDownProduct
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~IDownProduct() = default;
|
||||||
|
virtual void doSomeThing() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Down1Product : public IDownProduct
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~Down1Product() = default;
|
||||||
|
virtual void doSomeThing() override {
|
||||||
|
cout << "this is 1 DownProduct, do some thing..." << endl;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Down2Product : public IDownProduct
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~Down2Product() = default;
|
||||||
|
virtual void doSomeThing() override {
|
||||||
|
cout << "this is 2 DownProduct, do some thing..." << endl;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class IFactory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~IFactory() = default;
|
||||||
|
virtual IUpProduct* creatorUpProduct() = 0;
|
||||||
|
virtual IDownProduct* creatorDownProduct() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class AFactory : public IFactory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~AFactory() = default;
|
||||||
|
virtual IUpProduct* creatorUpProduct() override {
|
||||||
|
return new Up1Product();
|
||||||
|
}
|
||||||
|
virtual IDownProduct* creatorDownProduct() override {
|
||||||
|
return new Down1Product();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class BFactory : public IFactory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~BFactory() = default;
|
||||||
|
virtual IUpProduct* creatorUpProduct() override {
|
||||||
|
return new Up2Product();
|
||||||
|
}
|
||||||
|
virtual IDownProduct* creatorDownProduct() override {
|
||||||
|
return new Down2Product();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class CFactory : public IFactory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~CFactory() = default;
|
||||||
|
virtual IUpProduct* creatorUpProduct() override {
|
||||||
|
return new Up1Product();
|
||||||
|
}
|
||||||
|
virtual IDownProduct* creatorDownProduct() override {
|
||||||
|
return new Down2Product();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//int main()
|
||||||
|
//{
|
||||||
|
// AFactory aFactory;
|
||||||
|
// aFactory.creatorUpProduct()->doSomeThing();
|
||||||
|
// aFactory.creatorDownProduct()->doSomeThing();
|
||||||
|
// cout << endl;
|
||||||
|
//
|
||||||
|
// BFactory bFactory;
|
||||||
|
// bFactory.creatorUpProduct()->doSomeThing();
|
||||||
|
// bFactory.creatorDownProduct()->doSomeThing();
|
||||||
|
// cout << endl;
|
||||||
|
//
|
||||||
|
// CFactory cFactory;
|
||||||
|
// cFactory.creatorUpProduct()->doSomeThing();
|
||||||
|
// cFactory.creatorDownProduct()->doSomeThing();
|
||||||
|
// cout << endl;
|
||||||
|
// return 0;
|
||||||
|
//}
|
||||||
|
|
||||||
|
/*****************************打印结果*******************************
|
||||||
|
this is 1 UpProduct, do some thing...
|
||||||
|
this is 1 DownProduct, do some thing...
|
||||||
|
|
||||||
|
this is 2 UpProduct, do some thing...
|
||||||
|
this is 2 DownProduct, do some thing...
|
||||||
|
|
||||||
|
this is 1 UpProduct, do some thing...
|
||||||
|
this is 2 DownProduct, do some thing...
|
||||||
|
******************************************************************/
|
@ -139,11 +139,15 @@
|
|||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="AbstractFactory.cpp" />
|
||||||
<ClCompile Include="DesignPatterns.cpp" />
|
<ClCompile Include="DesignPatterns.cpp" />
|
||||||
|
<ClCompile Include="FactoryMethod.cpp" />
|
||||||
<ClCompile Include="Observer.cpp" />
|
<ClCompile Include="Observer.cpp" />
|
||||||
<ClCompile Include="Singleton.cpp" />
|
<ClCompile Include="Singleton.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="AbstractFactory.h" />
|
||||||
|
<ClInclude Include="FactoryMethod.h" />
|
||||||
<ClInclude Include="Observer.h" />
|
<ClInclude Include="Observer.h" />
|
||||||
<ClInclude Include="Singleton.h" />
|
<ClInclude Include="Singleton.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -24,6 +24,12 @@
|
|||||||
<ClCompile Include="Observer.cpp">
|
<ClCompile Include="Observer.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="FactoryMethod.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="AbstractFactory.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Singleton.h">
|
<ClInclude Include="Singleton.h">
|
||||||
@ -32,5 +38,11 @@
|
|||||||
<ClInclude Include="Observer.h">
|
<ClInclude Include="Observer.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="FactoryMethod.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="AbstractFactory.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
1
Studio/DesignPatterns/FactoryMethod.cpp
Normal file
1
Studio/DesignPatterns/FactoryMethod.cpp
Normal file
@ -0,0 +1 @@
|
|||||||
|
#include "FactoryMethod.h"
|
94
Studio/DesignPatterns/FactoryMethod.h
Normal file
94
Studio/DesignPatterns/FactoryMethod.h
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
/*******************************************************************
|
||||||
|
* Copyright (c) 2022~2023 XMuli All rights reserved.
|
||||||
|
* GitHub: https://github.com/XMuli
|
||||||
|
* Description: 工厂方法模式(Factory Method)
|
||||||
|
* IProduct 下面有多个具体类,都通过 IFactory 定义的好接口,
|
||||||
|
* 通过具体的 Factory 去 new 具体的 Product 对象
|
||||||
|
* See: https://blog.csdn.net/silangquan/article/details/20492293
|
||||||
|
******************************************************************/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
/********************************************************
|
||||||
|
每创建一个具体的 Product 类,就得创建一个具体的 Factory 类
|
||||||
|
********************************************************/
|
||||||
|
class IProduct
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~IProduct() = default;
|
||||||
|
virtual void doSomeThing() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class AProduct : public IProduct
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~AProduct() = default;
|
||||||
|
virtual void doSomeThing() override {
|
||||||
|
cout << "this is A Product, do some thing..." << endl;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
class BProduct : public IProduct
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~BProduct() = default;
|
||||||
|
virtual void doSomeThing() override {
|
||||||
|
cout << "this is B Product, do some thing..." << endl;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
class CProduct : public IProduct
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~CProduct() = default;
|
||||||
|
virtual void doSomeThing() override {
|
||||||
|
cout << "this is C Product, do some thing..." << endl;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
class IFactory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual IProduct* creatProduct() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class AFactory : public IFactory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~AFactory() = default;
|
||||||
|
virtual IProduct* creatProduct() override { return new AProduct(); };
|
||||||
|
};
|
||||||
|
|
||||||
|
class BFactory : public IFactory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~BFactory() = default;
|
||||||
|
virtual IProduct* creatProduct() override { return new BProduct(); };
|
||||||
|
};
|
||||||
|
|
||||||
|
class CFactory : public IFactory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~CFactory() = default;
|
||||||
|
virtual IProduct* creatProduct() override { return new CProduct(); };
|
||||||
|
};
|
||||||
|
|
||||||
|
//int main()
|
||||||
|
//{
|
||||||
|
// AFactory aFactory;
|
||||||
|
// BFactory bFactory;
|
||||||
|
// CFactory cFactory;
|
||||||
|
// aFactory.creatProduct()->doSomeThing();
|
||||||
|
// bFactory.creatProduct()->doSomeThing();
|
||||||
|
// cFactory.creatProduct()->doSomeThing();
|
||||||
|
// return 0;
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
/*****************************打印结果*******************************
|
||||||
|
this is A Product, do some thing...
|
||||||
|
this is B Product, do some thing...
|
||||||
|
this is C Product, do some thing...
|
||||||
|
******************************************************************/
|
Loading…
Reference in New Issue
Block a user