Composite.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <stdio.h>
  2. /*
  3. * C++ Design Patterns: COMPOSITE
  4. * https://www.plantuml.com/plantuml/uml/VL7BRjH04BpxArhYu8J5hBYkh5LWnG49259oNxN75JDe7bPFpqeHy3_m7Vanx65Z0alIfvcgckegpKLI5itULUkQTfSVFbrUlRj-I_MwciO6XTom0UsY7sD0KFfQqJGD-NrWZt9vHd64iDeOpiwF2FVzcGraQFFxRRAAyuKL-7Z0gU183z3TP-kAWvq4DkWzXP3zl0kJr_UgCevJelVWcoNOAFR00_QsBy3g9Dpg_oPBYh-T57ipPn44Ei1ebCTid-oyUhhpwj7Eft7__4NxhMbA-N-nnVq1qJJF7-1LY0hYH1zVbIrjjsrDT54s4c_PlN4PsqNThWkrRTVTzbvzVJjYCmDqfPADPc5NkDSfmNUE1r1Vn-uBD57F8L4Ee7JKeaXPkYX81a7nWbnj-xkV82Ib7jHnO6SJf4XTpSRS_KZKJ3vMsGeevbatICpZikjbXT3JNAwwc4xPkry0
  5. *
  6. */
  7. #include <iostream>
  8. #include <string>
  9. #include <vector>
  10. class Component {
  11. public:
  12. std::string m_name;
  13. Component() { std::cout << "Constructor Component" << std::endl; }
  14. virtual ~Component() {}
  15. virtual void operation() = 0;
  16. virtual void add(Component *) {};
  17. virtual void remove(Component *) {};
  18. virtual Component* getChild(int) { return nullptr; };
  19. virtual void enumerate() = 0 ;
  20. };
  21. class Leaf : public Component {
  22. public:
  23. Leaf(int id) : private_id(id) { std::cout << "Constructor Leaf" << std::endl; }
  24. ~Leaf() {}
  25. void operation() { std::cout << "This is operation from Leaf: " << private_id << std::endl; }
  26. void enumerate() { std::cout << "Enumerate from Leaf: " << private_id << std::endl;};
  27. private:
  28. int private_id;
  29. };
  30. class Composite : public Component {
  31. public:
  32. Composite() { std::cout << "Constructor Composite" << std::endl; }
  33. ~Composite() {
  34. for (unsigned int i = 0; i < private_children.size(); ++i) {
  35. delete private_children[i];
  36. }
  37. }
  38. void operation() {
  39. std::cout << "size of children: " << private_children.size() << std::endl;
  40. for (unsigned int i = 0; i < private_children.size(); ++i) {
  41. private_children[i]->operation();
  42. }
  43. }
  44. void add(Component *c) {
  45. private_children.push_back(c);
  46. }
  47. void remove(Component *c) {
  48. for (auto iter = private_children.begin(); iter != private_children.end(); ++iter) {
  49. if (*iter == c) {
  50. private_children.erase(iter);
  51. }
  52. }
  53. }
  54. Component* getChild(unsigned int idx) {
  55. return idx < private_children.size() ? private_children[idx] : nullptr;
  56. }
  57. void group(const std::string &n) {m_name=n;};
  58. void enumerate() {
  59. std::cout << "Group " << m_name.c_str() << " contains:" << std::endl;
  60. for (auto &&o : private_children)
  61. o->enumerate();
  62. }
  63. private:
  64. std::vector<Component*> private_children;
  65. };
  66. int main() {
  67. Composite composite;
  68. composite.group("principal");
  69. for (unsigned int i = 0; i < 3; ++i) {
  70. composite.add(new Leaf(i));
  71. }
  72. Composite composite2;
  73. composite2.group("secondaire");
  74. composite.add(&composite2);
  75. composite.remove(0);
  76. composite.operation();
  77. Component *component1 = composite.getChild(0);
  78. component1->operation();
  79. Component *component2 = composite.getChild(3);
  80. component2->operation();
  81. composite.enumerate();
  82. }