Abstract_Factory.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * C++ Design Patterns: ABSTRACT FACTORY
  3. * http://www.plantuml.com/plantuml/uml/VP51ojim48NtEiN0lqXBCARRO4piG9PjIBFfKfQdZOeifQDnmRIzIvVrENonIgeJ-XiJGMBptZVFevczPqdSTqRa6wZgZvTpTRZ0CVnzE7-2JIx2KKPw3mUZqR8GsZBIPwaGgiOpISN7y7Cqm0y1uRo08fIC9t9jhxXwytPDheFywn7jh29aV1sqNG_QhWMjT3Nx8Um5lEkGhzf-cSN_FtwF3-1s7LoGzHFogbi4x6uyG8TTW-I5M8wf-9bbookBPTSHUhJsrAt5lyr0bfNBbQnPSvaGrZ62zF3-NPfhqPI5SiRHZjmWJMrwBEyrCESLUIBA8c_AUz5bkE4k2d28Uz7EIfEy4rA734Q2ecbC3hGRrtn5zl2jrmdopWP4JwE7wJUqw27DEZwTjJtwH25349sQnc1uQJt8xqvJB348Dvf6fJrIOjje6-nfJef1YgO_Z09j2_7MOX_U-ixy1G00
  4. * 2021 / JMB
  5. *
  6. */
  7. #include <iostream>
  8. class AbstractProductA {
  9. public:
  10. AbstractProductA() { std::cout << "AbstractProductA instancied" << std::endl; }
  11. virtual ~AbstractProductA() {}
  12. virtual void somethingProduct() = 0;
  13. };
  14. class AbstractProductB {
  15. public:
  16. AbstractProductB() { std::cout << "AbstractProductB instancied" << std::endl; }
  17. virtual ~AbstractProductB() {}
  18. virtual void somethingProduct() = 0;
  19. };
  20. class ProductA1 : public AbstractProductA {
  21. public:
  22. ProductA1() { std::cout << "ProductA1 instancied" << std::endl; }
  23. ~ProductA1() {}
  24. void somethingProduct() { std::cout << "ProductA1 has been created" << std::endl;}
  25. };
  26. class ProductA2 : public AbstractProductA {
  27. public:
  28. ProductA2() { std::cout << "ProductA2 instancied" << std::endl; }
  29. ~ProductA2() {}
  30. void somethingProduct() { std::cout << "ProductA2 has been created" << std::endl; }
  31. };
  32. class ProductB1 : public AbstractProductB {
  33. public:
  34. ProductB1() { std::cout << "ProductB1 instancied" << std::endl; }
  35. ~ProductB1() {}
  36. void somethingProduct() { std::cout << "ProductB1 has been created" << std::endl; }
  37. };
  38. class ProductB2 : public AbstractProductB {
  39. public:
  40. ProductB2() { std::cout << "ProductB2 instancied" << std::endl; }
  41. ~ProductB2() {}
  42. void somethingProduct() { std::cout << "ProductB2 has been created" << std::endl; }
  43. };
  44. class AbstractFactory {
  45. public:
  46. AbstractFactory() { std::cout << "AbstractFactory instancied" << std::endl; }
  47. virtual ~AbstractFactory() {}
  48. virtual AbstractProductA* createProductA() = 0;
  49. virtual AbstractProductB* createProductB() = 0;
  50. };
  51. class ConcreateFactory1 : public AbstractFactory {
  52. public:
  53. ConcreateFactory1() { std::cout << "ConcreateFactory1 instancied" << std::endl; }
  54. ~ConcreateFactory1() {}
  55. AbstractProductA* createProductA() { return new ProductA1; }
  56. AbstractProductB* createProductB() { return new ProductB1; }
  57. };
  58. class ConcreateFactory2 : public AbstractFactory {
  59. public:
  60. ConcreateFactory2() { std::cout << "ConcreateFactory2 instancied" << std::endl; }
  61. ~ConcreateFactory2() {}
  62. AbstractProductA* createProductA() { return new ProductA2; }
  63. AbstractProductB* createProductB() { return new ProductB2; }
  64. };
  65. int main(int argc, char* argv[]) {
  66. AbstractFactory* cf1 = new ConcreateFactory1();
  67. AbstractProductA* productA1 = cf1->createProductA();
  68. productA1->somethingProduct();
  69. AbstractProductB* productB1 = cf1->createProductB();
  70. productB1->somethingProduct();
  71. AbstractFactory* cf2 = new ConcreateFactory2();
  72. AbstractProductA* productA2 = cf2->createProductA();
  73. productA2->somethingProduct();
  74. AbstractProductB* productB2 = cf1->createProductB();
  75. productB2->somethingProduct();
  76. }