From 092d1a21f94e78c8e979759937a3fb445dadbc9d Mon Sep 17 00:00:00 2001 From: Ian Dinwoodie Date: Tue, 14 May 2019 19:19:26 -0400 Subject: [PATCH] Rolling back use of typedef with smart pointers. This is to reduce the obfuscation of the code. --- examples/creational/abstract_factory.cpp | 23 ++++++++++------------- examples/creational/builder.cpp | 10 ++++------ examples/creational/factory_method.cpp | 9 ++++----- examples/creational/simple_factory.cpp | 6 +++--- 4 files changed, 21 insertions(+), 27 deletions(-) diff --git a/examples/creational/abstract_factory.cpp b/examples/creational/abstract_factory.cpp index 324b036..30a990a 100644 --- a/examples/creational/abstract_factory.cpp +++ b/examples/creational/abstract_factory.cpp @@ -4,7 +4,6 @@ class Door { public: - typedef std::shared_ptr ptr_t; virtual void getDescription(void) = 0; }; @@ -29,7 +28,6 @@ class IronDoor : public Door class DoorFittingExpert { public: - typedef std::shared_ptr ptr_t; virtual void getDescription(void) = 0; }; @@ -48,26 +46,25 @@ class Carpenter : public DoorFittingExpert void getDescription(void) { std::cout << "I can only fit wooden doors." << std::endl; - } }; class DoorFactory { public: - virtual Door::ptr_t makeDoor(void) = 0; - virtual DoorFittingExpert::ptr_t makeFittingExpert(void) = 0; + virtual std::shared_ptr makeDoor(void) = 0; + virtual std::shared_ptr makeFittingExpert(void) = 0; }; class WoodenDoorFactory : public DoorFactory { public: - Door::ptr_t makeDoor(void) + std::shared_ptr makeDoor(void) { return std::make_shared(); } - DoorFittingExpert::ptr_t makeFittingExpert(void) + std::shared_ptr makeFittingExpert(void) { return std::make_shared(); } @@ -76,12 +73,12 @@ class WoodenDoorFactory : public DoorFactory class IronDoorFactory : public DoorFactory { public: - Door::ptr_t makeDoor(void) + std::shared_ptr makeDoor(void) { return std::make_shared(); } - DoorFittingExpert::ptr_t makeFittingExpert(void) + std::shared_ptr makeFittingExpert(void) { return std::make_shared(); } @@ -90,15 +87,15 @@ class IronDoorFactory : public DoorFactory int main() { WoodenDoorFactory woodenFactory = WoodenDoorFactory(); - Door::ptr_t door = woodenFactory.makeDoor(); - DoorFittingExpert::ptr_t expert = woodenFactory.makeFittingExpert(); + std::shared_ptr door = woodenFactory.makeDoor(); + std::shared_ptr expert = woodenFactory.makeFittingExpert(); door->getDescription(); // Output: I am a wooden door. expert->getDescription(); // Output: I can only fit wooden doors. IronDoorFactory ironFactory = IronDoorFactory(); - Door::ptr_t door2 = ironFactory.makeDoor(); - DoorFittingExpert::ptr_t expert2 = woodenFactory.makeFittingExpert(); + std::shared_ptr door2 = ironFactory.makeDoor(); + std::shared_ptr expert2 = woodenFactory.makeFittingExpert(); door2->getDescription(); // Output: I am an iron door. expert2->getDescription(); // Output: I can only fit iron doors. diff --git a/examples/creational/builder.cpp b/examples/creational/builder.cpp index 1e57de4..af6426c 100644 --- a/examples/creational/builder.cpp +++ b/examples/creational/builder.cpp @@ -6,7 +6,6 @@ class BurgerBuilder; class Burger { public: - typedef std::shared_ptr ptr_t; Burger(BurgerBuilder* builder); void getDescription(void); @@ -51,10 +50,9 @@ class BurgerBuilder return (*this); } - Burger::ptr_t build(void) + std::shared_ptr build(void) { - Burger::ptr_t burger(new Burger(this)); - return burger; + return std::make_shared(this); } int patties; @@ -92,7 +90,7 @@ void Burger::getDescription(void) int main() { // One double patty burger with no dairy. - Burger::ptr_t burger = BurgerBuilder(2). + std::shared_ptr burger = BurgerBuilder(2). addPepperoni(). addLettuce(). addTomato(). @@ -101,7 +99,7 @@ int main() burger->getDescription(); // One triple patty buger with everything. - Burger::ptr_t burger2 = BurgerBuilder(3). + std::shared_ptr burger2 = BurgerBuilder(3). addPepperoni(). addCheese(). addLettuce(). diff --git a/examples/creational/factory_method.cpp b/examples/creational/factory_method.cpp index 4057dcc..338afb2 100644 --- a/examples/creational/factory_method.cpp +++ b/examples/creational/factory_method.cpp @@ -4,7 +4,6 @@ class Interviewer { public: - typedef std::shared_ptr ptr_t; virtual void askQuestions(void) = 0; }; @@ -31,18 +30,18 @@ class HiringManager public: void takeInterview(void) { - Interviewer::ptr_t interviewer = makeInterviewer(); + std::shared_ptr interviewer = makeInterviewer(); interviewer->askQuestions(); } protected: - virtual Interviewer::ptr_t makeInterviewer(void) = 0; + virtual std::shared_ptr makeInterviewer(void) = 0; }; class DevelopmentManager : public HiringManager { protected: - Interviewer::ptr_t makeInterviewer(void) + std::shared_ptr makeInterviewer(void) { return std::make_shared(); } @@ -51,7 +50,7 @@ class DevelopmentManager : public HiringManager class MarketingManager : public HiringManager { protected: - Interviewer::ptr_t makeInterviewer(void) + std::shared_ptr makeInterviewer(void) { return std::make_shared(); } diff --git a/examples/creational/simple_factory.cpp b/examples/creational/simple_factory.cpp index 2a13d7e..90134a7 100644 --- a/examples/creational/simple_factory.cpp +++ b/examples/creational/simple_factory.cpp @@ -35,7 +35,7 @@ class WoodenDoor : public Door class DoorFactory { public: - static Door::ptr_t makeDoor(float width, float height) + static std::shared_ptr makeDoor(float width, float height) { return std::make_shared(width, height); } @@ -44,7 +44,7 @@ class DoorFactory int main() { // Make a door with dimensions 100x200. - Door::ptr_t door = DoorFactory::makeDoor(100, 200); + std::shared_ptr door = DoorFactory::makeDoor(100, 200); // Output: width = 100 std::cout << "width = " << door->getWidth() << std::endl; @@ -52,7 +52,7 @@ int main() std::cout << "height = " << door->getHeight() << std::endl; // We can use the factory again to make a door with dimensions 50x100. - Door::ptr_t door2 = DoorFactory::makeDoor(50, 100); + std::shared_ptr door2 = DoorFactory::makeDoor(50, 100); // Output: width = 50 std::cout << "width = " << door2->getWidth() << std::endl;