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