Advertisement
Guest User

Untitled

a guest
Feb 13th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. /**
  6. * Wykorzystywany jest tutaj: Singleton, Prototyp, Kompozyt
  7. */
  8.  
  9. //! Kompozyt & Prototyp - Klasa bazowa Istoty (Being)
  10. class Being
  11. {
  12. public:
  13.  
  14. virtual void printId() = 0;
  15.  
  16. //! Prototyp - metoda kopiująca prototyp
  17. virtual Being * clone() const = 0;
  18. virtual ~Being() {}
  19. };
  20.  
  21. //! Kompozyt - Klasa Facet
  22. class Male : public Being
  23. {
  24. public:
  25. const std::string name;
  26. Male (std::string pname): name(pname) {}
  27. Male (const Male & clone): name( clone.name ) {} //! Prototyp - konstruktor kopiujący
  28. void printId() { std::cout << name << std::endl; }
  29. Being * clone() const { return new Male( *this ); } //! Prototyp - klonowanie
  30. };
  31.  
  32. //! Kompozyt - Klasa Kobieta
  33. class Female : public Being
  34. {
  35. public:
  36. const std::string name;
  37. Female (std::string pname): name(pname) {}
  38. Female (const Female & clone): name( clone.name ) {} //! Prototyp - konstruktor kopiujący
  39. void printId() { std::cout << name << std::endl; }
  40. Being * clone() const { return new Female( *this ); } //! Prototyp - klonowanie
  41.  
  42. };
  43.  
  44. //! Kompozyt - Klasa Osoby - Kontener
  45. class Beings : public Being
  46. {
  47. //! Kompozyt - Kontener przechowujacy instancje komponentow
  48. std::vector < Being * > beings;
  49. const std::string name;
  50.  
  51. public:
  52.  
  53. Beings (): name("Beings") {}
  54. Beings (const Beings & clone): name( clone.name ), beings( clone.beings ) {} //! Prototyp - konstruktor kopiujący
  55.  
  56. Being * clone() const { return new Beings( *this ); } //! Prototyp - klonowanie
  57.  
  58. //! Kompozyt - Zarządzanie instancjami istot
  59. void addBeing ( Being* comp )
  60. {
  61. beings.push_back( comp );
  62. }
  63.  
  64. //! Kompozyt - Wyświetla swoje Id (bo jest instancja component) oraz wszystkich component ktore posiada
  65. void printId ()
  66. {
  67. std::cout << name << std::endl;
  68. for ( unsigned i = 0; i < beings.size(); i++ )
  69. {
  70. beings[i]->printId();
  71. }
  72. }
  73. };
  74.  
  75. //! Singleton, Bog jest tylko jeden :P
  76. class God
  77. {
  78. private:
  79.  
  80. //! Singleton - Prywatne konstruktory
  81. God () {}
  82. God (const God &);
  83. God & operator=(const God &);
  84. ~God () {}
  85.  
  86. public:
  87.  
  88. Beings beings;
  89.  
  90. //! Singleton - Metoda zwracająca tylko jedną instancje
  91. static God & get()
  92. {
  93. //! Singleton - Jedyna instancja klasy
  94. static God instance;
  95. return instance;
  96. }
  97. };
  98.  
  99. int main()
  100. {
  101. Being* magdalena = new Female("Magdalena");
  102. Being* rafal = new Male("Rafal");
  103. Being* bogdan = new Male("Bogdan");
  104.  
  105. //! Sposob Uzycie Singletona, Kompozyta, Prototypa
  106.  
  107. God::get().beings.addBeing( magdalena );
  108. God::get().beings.addBeing( rafal );
  109. God::get().beings.addBeing( bogdan );
  110.  
  111. God::get().beings.printId();
  112.  
  113. Being* bogdanClone = bogdan->clone();
  114.  
  115. God::get().beings.addBeing( bogdanClone );
  116.  
  117. std::cout << std::endl;
  118.  
  119. God::get().beings.printId();
  120.  
  121. return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement