diff --git a/examples/creational/abstract_factory.cpp b/examples/creational/abstract_factory.cpp new file mode 100644 index 0000000..324b036 --- /dev/null +++ b/examples/creational/abstract_factory.cpp @@ -0,0 +1,107 @@ +#include +#include + +class Door +{ + public: + typedef std::shared_ptr ptr_t; + virtual void getDescription(void) = 0; +}; + +class WoodenDoor : public Door +{ + public: + void getDescription(void) + { + std::cout << "I am a wooden door." << std::endl; + } +}; + +class IronDoor : public Door +{ + public: + void getDescription(void) + { + std::cout << "I am an iron door." << std::endl; + } +}; + +class DoorFittingExpert +{ + public: + typedef std::shared_ptr ptr_t; + virtual void getDescription(void) = 0; +}; + +class Welder : public DoorFittingExpert +{ + public: + void getDescription(void) + { + std::cout << "I can only fit iron doors." << std::endl; + } +}; + +class Carpenter : public DoorFittingExpert +{ + public: + 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; +}; + +class WoodenDoorFactory : public DoorFactory +{ + public: + Door::ptr_t makeDoor(void) + { + return std::make_shared(); + } + + DoorFittingExpert::ptr_t makeFittingExpert(void) + { + return std::make_shared(); + } +}; + +class IronDoorFactory : public DoorFactory +{ + public: + Door::ptr_t makeDoor(void) + { + return std::make_shared(); + } + + DoorFittingExpert::ptr_t makeFittingExpert(void) + { + return std::make_shared(); + } +}; + +int main() +{ + WoodenDoorFactory woodenFactory = WoodenDoorFactory(); + Door::ptr_t door = woodenFactory.makeDoor(); + DoorFittingExpert::ptr_t 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(); + + door2->getDescription(); // Output: I am an iron door. + expert2->getDescription(); // Output: I can only fit iron doors. + + return 0; +}