feat: add "Factory Method" and "Abstract Factory"

This commit is contained in:
XMuli 2022-12-25 19:36:56 +08:00
parent 8ea61d1bc8
commit f5724bf850
No known key found for this signature in database
GPG Key ID: 9554B5DD5B8E986A
6 changed files with 253 additions and 0 deletions

View File

@ -0,0 +1 @@
#include "AbstractFactory.h"

View File

@ -0,0 +1,141 @@
/*******************************************************************
* Copyright (c) 2022~2023 XMuli All rights reserved.
* GitHub: https://github.com/XMuli
* Description: (Abstract Factory)
* Up产品 up1up2 Down down1down2
* 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...
******************************************************************/

View File

@ -139,11 +139,15 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="AbstractFactory.cpp" />
<ClCompile Include="DesignPatterns.cpp" />
<ClCompile Include="FactoryMethod.cpp" />
<ClCompile Include="Observer.cpp" />
<ClCompile Include="Singleton.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="AbstractFactory.h" />
<ClInclude Include="FactoryMethod.h" />
<ClInclude Include="Observer.h" />
<ClInclude Include="Singleton.h" />
</ItemGroup>

View File

@ -24,6 +24,12 @@
<ClCompile Include="Observer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="FactoryMethod.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="AbstractFactory.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Singleton.h">
@ -32,5 +38,11 @@
<ClInclude Include="Observer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="FactoryMethod.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="AbstractFactory.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -0,0 +1 @@
#include "FactoryMethod.h"

View 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...
******************************************************************/