From f5724bf850734308d69c9597c3aa12ae01fc4c4b Mon Sep 17 00:00:00 2001
From: XMuli <xmulitech@gmail.com>
Date: Sun, 25 Dec 2022 19:36:56 +0800
Subject: [PATCH] feat: add "Factory Method" and  "Abstract Factory"

---
 Studio/DesignPatterns/AbstractFactory.cpp     |   1 +
 Studio/DesignPatterns/AbstractFactory.h       | 141 ++++++++++++++++++
 Studio/DesignPatterns/DesignPatterns.vcxproj  |   4 +
 .../DesignPatterns.vcxproj.filters            |  12 ++
 Studio/DesignPatterns/FactoryMethod.cpp       |   1 +
 Studio/DesignPatterns/FactoryMethod.h         |  94 ++++++++++++
 6 files changed, 253 insertions(+)
 create mode 100644 Studio/DesignPatterns/AbstractFactory.cpp
 create mode 100644 Studio/DesignPatterns/AbstractFactory.h
 create mode 100644 Studio/DesignPatterns/FactoryMethod.cpp
 create mode 100644 Studio/DesignPatterns/FactoryMethod.h

diff --git a/Studio/DesignPatterns/AbstractFactory.cpp b/Studio/DesignPatterns/AbstractFactory.cpp
new file mode 100644
index 0000000..a3036c7
--- /dev/null
+++ b/Studio/DesignPatterns/AbstractFactory.cpp
@@ -0,0 +1 @@
+#include "AbstractFactory.h"
diff --git a/Studio/DesignPatterns/AbstractFactory.h b/Studio/DesignPatterns/AbstractFactory.h
new file mode 100644
index 0000000..c3e7e01
--- /dev/null
+++ b/Studio/DesignPatterns/AbstractFactory.h
@@ -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...
+ ******************************************************************/
\ No newline at end of file
diff --git a/Studio/DesignPatterns/DesignPatterns.vcxproj b/Studio/DesignPatterns/DesignPatterns.vcxproj
index e4812f6..a38a08e 100644
--- a/Studio/DesignPatterns/DesignPatterns.vcxproj
+++ b/Studio/DesignPatterns/DesignPatterns.vcxproj
@@ -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>
diff --git a/Studio/DesignPatterns/DesignPatterns.vcxproj.filters b/Studio/DesignPatterns/DesignPatterns.vcxproj.filters
index a0a15c0..b898ad8 100644
--- a/Studio/DesignPatterns/DesignPatterns.vcxproj.filters
+++ b/Studio/DesignPatterns/DesignPatterns.vcxproj.filters
@@ -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>
\ No newline at end of file
diff --git a/Studio/DesignPatterns/FactoryMethod.cpp b/Studio/DesignPatterns/FactoryMethod.cpp
new file mode 100644
index 0000000..99077a7
--- /dev/null
+++ b/Studio/DesignPatterns/FactoryMethod.cpp
@@ -0,0 +1 @@
+#include "FactoryMethod.h"
diff --git a/Studio/DesignPatterns/FactoryMethod.h b/Studio/DesignPatterns/FactoryMethod.h
new file mode 100644
index 0000000..029d560
--- /dev/null
+++ b/Studio/DesignPatterns/FactoryMethod.h
@@ -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...
+ ******************************************************************/
\ No newline at end of file