From 92a17eed1404e397b0cb28c69dbecbadde0f9def Mon Sep 17 00:00:00 2001 From: ichdream Date: Mon, 30 Aug 2021 22:19:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B1=BB=E5=92=8C=E5=BA=94=E7=94=A8=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E7=AE=A1=E7=90=86=E7=BB=9F=E4=B8=80=E6=94=B9=E4=B8=BA?= =?UTF-8?q?shared=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