From 72a37f177ac246b06345f92aed9820f032a8d0d3 Mon Sep 17 00:00:00 2001 From: ichdream Date: Mon, 30 Aug 2021 20:13:41 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=94=B9=E7=94=A8shared=5Fptr=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E7=AE=A1=E7=90=86=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 01.SimpleFactory/2.Code/main.cpp | 32 +++++++++--------------- 02.FactoryMethod/2.Code/main.cpp | 43 ++++++++------------------------ 2 files changed, 23 insertions(+), 52 deletions(-) diff --git a/01.SimpleFactory/2.Code/main.cpp b/01.SimpleFactory/2.Code/main.cpp index 6dc0ed4..2c5c171 100644 --- a/01.SimpleFactory/2.Code/main.cpp +++ b/01.SimpleFactory/2.Code/main.cpp @@ -1,30 +1,22 @@ #include +#include #include "SimpleFactory.h" int main() { - printf("简单工厂模式\n"); + printf("绠鍗曞伐鍘傛ā寮廫n"); - //定义工厂类对象 - Factory *fac = new Factory(); - AbstractSportProduct *product = NULL; + //瀹氫箟宸ュ巶绫诲璞 + std::shared_ptr fac = std::make_shared(); + std::shared_ptrproduct = std::shared_ptr(fac->getSportProduct("Basketball")); + + fac = std::make_shared(); + product = std::shared_ptr(fac->getSportProduct("Football")); - product = fac->getSportProduct("Basketball"); - if (product) { - delete product; - } - - product = fac->getSportProduct("Football"); - if (product) { - delete product; - } - - product = fac->getSportProduct("Volleyball"); - if (product) { - delete product; - } - - delete fac; + fac = std::make_shared(); + product = std::shared_ptr(fac->getSportProduct("Volleyball")); +#ifdef win32 system("pause"); +#endif return 0; } \ No newline at end of file diff --git a/02.FactoryMethod/2.Code/main.cpp b/02.FactoryMethod/2.Code/main.cpp index b332a4d..76785a7 100644 --- a/02.FactoryMethod/2.Code/main.cpp +++ b/02.FactoryMethod/2.Code/main.cpp @@ -1,44 +1,23 @@ #include #include "FactoryMethod.h" +#include int main() { printf("工厂方法模式\n"); //定义工厂类对象和产品类对象 - AbstractFactory *fac = NULL; - AbstractSportProduct *product = NULL; - - fac = new BasketballFactory(); - product = fac->getSportProduct(); - if (fac) - { - delete fac; - } - if (product) { - delete product; - } - - fac = new FootballFactory(); - product = fac->getSportProduct(); - if (fac) - { - delete fac; - } - if (product) { - delete product; - } - - fac = new VolleyballFactory(); - product = fac->getSportProduct(); - if (fac) - { - delete fac; - } - if (product) { - delete product; - } + + std::shared_ptr fac = make_shared(); + std::shared_ptr product = std::shared_ptr(fac->getSportProduct()); + fac = make_shared(); + // product = std::shared_ptr(fac->getSportProduct()); + product = std::shared_ptr(fac->getSportProduct()); + + fac = make_shared(); + product = std::shared_ptr(fac->getSportProduct()); + system("pause"); return 0; } \ No newline at end of file From ea3ffad91611c90fe7e6b0f1654ed6b2cad97422 Mon Sep 17 00:00:00 2001 From: ichdream Date: Mon, 30 Aug 2021 21:56:10 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=B0=86fileencoding=E6=94=B9=E4=B8=BAutf-?= =?UTF-8?q?8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 01.SimpleFactory/2.Code/SimpleFactory.h | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/01.SimpleFactory/2.Code/SimpleFactory.h b/01.SimpleFactory/2.Code/SimpleFactory.h index 94b7aa8..26c8969 100644 --- a/01.SimpleFactory/2.Code/SimpleFactory.h +++ b/01.SimpleFactory/2.Code/SimpleFactory.h @@ -5,20 +5,22 @@ #include using namespace std; -//抽象产品类AbstractProduct +//鎶借薄浜у搧绫籄bstractProduct class AbstractSportProduct { public: AbstractSportProduct(){ } - virtual ~AbstractSportProduct(){} - //抽象方法: - virtual void printName(){}; - virtual void play(){}; + virtual ~AbstractSportProduct(){}; + //鎶借薄鏂规硶锛 + // virtual void printName(){}; + virtual void printName() = 0; + // virtual void play(){}; + virtual void play() = 0; }; -//具体产品类Basketball +//鍏蜂綋浜у搧绫籅asketball class Basketball :public AbstractSportProduct { public: @@ -30,7 +32,7 @@ public: { } - //具体实现方法 + //鍏蜂綋瀹炵幇鏂规硶 void printName(){ printf("Jungle get Basketball\n"); } @@ -39,7 +41,7 @@ public: } }; -//具体产品类Football +//鍏蜂綋浜у搧绫籉ootball class Football :public AbstractSportProduct { public: @@ -51,7 +53,7 @@ public: { } - //具体实现方法 + //鍏蜂綋瀹炵幇鏂规硶 void printName(){ printf("Jungle get Football\n"); } @@ -60,7 +62,7 @@ public: } }; -//具体产品类Volleyball +//鍏蜂綋浜у搧绫籚olleyball class Volleyball :public AbstractSportProduct { public: @@ -72,7 +74,7 @@ public: { } - //具体实现方法 + //鍏蜂綋瀹炵幇鏂规硶 void printName(){ printf("Jungle get Volleyball\n"); } From 11e35f05894557776a88115b6dd1205aa9e7ccd7 Mon Sep 17 00:00:00 2001 From: ichdream Date: Mon, 30 Aug 2021 21:57:31 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=B7=A5=E5=85=B7=E9=85=8D=E7=BD=AEMakefile?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 01.SimpleFactory/2.Code/Makefile | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 01.SimpleFactory/2.Code/Makefile diff --git a/01.SimpleFactory/2.Code/Makefile b/01.SimpleFactory/2.Code/Makefile new file mode 100644 index 0000000..ce492a8 --- /dev/null +++ b/01.SimpleFactory/2.Code/Makefile @@ -0,0 +1,29 @@ +DIR_INC = ./ +DIR_SRC = ./ +DIR_OBJ = ./obj +DIR_BIN = ./bin + +SRC = $(wildcard ${DIR_SRC}/*.cpp) +OBJ = $(patsubst %.cpp,${DIR_OBJ}/%.o,$(notdir ${SRC})) + +TARGET = main + +BIN_TARGET = ${DIR_BIN}/${TARGET} + +CC = g++ +CFLAGS = -g -Wall -I${DIR_INC} + + +${BIN_TARGET}:${OBJ} + mkdir -p $(DIR_BIN) + $(CC) $(OBJ) -o $@ + +${DIR_OBJ}/%.o:${DIR_SRC}/%.cpp + mkdir -p $(DIR_OBJ) + $(CC) $(CFLAGS) -c $< -o $@ + +.PHONY:clean + +clean: + rm -rf bin obj + rm -rf ./main \ No newline at end of file From e3c2ea210eefee9117e4567a4070699da78deaf6 Mon Sep 17 00:00:00 2001 From: ichdream Date: Mon, 30 Aug 2021 21:59:55 +0800 Subject: [PATCH 4/5] add .gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0fd9bc6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bin +obj +main +*.o From 92a17eed1404e397b0cb28c69dbecbadde0f9def Mon Sep 17 00:00:00 2001 From: ichdream Date: Mon, 30 Aug 2021 22:19:44 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E7=B1=BB=E5=92=8C=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E7=AE=A1=E7=90=86=E7=BB=9F=E4=B8=80=E6=94=B9?= =?UTF-8?q?=E4=B8=BAshared=5Fptr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 01.SimpleFactory/2.Code/SimpleFactory.h | 10 +++++----- 01.SimpleFactory/2.Code/main.cpp | 9 ++++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/01.SimpleFactory/2.Code/SimpleFactory.h b/01.SimpleFactory/2.Code/SimpleFactory.h index 26c8969..17b8aa9 100644 --- a/01.SimpleFactory/2.Code/SimpleFactory.h +++ b/01.SimpleFactory/2.Code/SimpleFactory.h @@ -86,17 +86,17 @@ public: class Factory { public: - AbstractSportProduct *getSportProduct(string productName) + std::shared_ptr getSportProduct(string productName) { - AbstractSportProduct *pro = NULL; + std::shared_ptr pro; if (productName == "Basketball"){ - pro = new Basketball(); + pro = std::shared_ptr(new Basketball()); } else if (productName == "Football"){ - pro = new Football(); + pro = std::shared_ptr(new Football()); } else if (productName == "Volleyball"){ - pro = new Volleyball(); + pro = std::shared_ptr(new Volleyball()); } return pro; } diff --git a/01.SimpleFactory/2.Code/main.cpp b/01.SimpleFactory/2.Code/main.cpp index 2c5c171..ab8dfae 100644 --- a/01.SimpleFactory/2.Code/main.cpp +++ b/01.SimpleFactory/2.Code/main.cpp @@ -8,13 +8,16 @@ int main() //瀹氫箟宸ュ巶绫诲璞 std::shared_ptr fac = std::make_shared(); - std::shared_ptrproduct = std::shared_ptr(fac->getSportProduct("Basketball")); + // std::shared_ptr product = std::shared_ptr(fac->getSportProduct("Basketball")); + std::shared_ptr product = fac->getSportProduct("Basketball"); fac = std::make_shared(); - product = std::shared_ptr(fac->getSportProduct("Football")); + product = fac->getSportProduct("Football"); + // product = std::shared_ptr(fac->getSportProduct("Football")); fac = std::make_shared(); - product = std::shared_ptr(fac->getSportProduct("Volleyball")); + product = fac->getSportProduct("Volleyball"); + // product = std::shared_ptr(fac->getSportProduct("Volleyball")); #ifdef win32 system("pause"); #endif