Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- /**
- * Wykorzystywany jest tutaj: Singleton, Prototyp, Kompozyt
- */
- //! Kompozyt & Prototyp - Klasa bazowa Istoty (Being)
- class Being
- {
- public:
- virtual void printId() = 0;
- //! Prototyp - metoda kopiująca prototyp
- virtual Being * clone() const = 0;
- virtual ~Being() {}
- };
- //! Kompozyt - Klasa Facet
- class Male : public Being
- {
- public:
- const std::string name;
- Male (std::string pname): name(pname) {}
- Male (const Male & clone): name( clone.name ) {} //! Prototyp - konstruktor kopiujący
- void printId() { std::cout << name << std::endl; }
- Being * clone() const { return new Male( *this ); } //! Prototyp - klonowanie
- };
- //! Kompozyt - Klasa Kobieta
- class Female : public Being
- {
- public:
- const std::string name;
- Female (std::string pname): name(pname) {}
- Female (const Female & clone): name( clone.name ) {} //! Prototyp - konstruktor kopiujący
- void printId() { std::cout << name << std::endl; }
- Being * clone() const { return new Female( *this ); } //! Prototyp - klonowanie
- };
- //! Kompozyt - Klasa Osoby - Kontener
- class Beings : public Being
- {
- //! Kompozyt - Kontener przechowujacy instancje komponentow
- std::vector < Being * > beings;
- const std::string name;
- public:
- Beings (): name("Beings") {}
- Beings (const Beings & clone): name( clone.name ), beings( clone.beings ) {} //! Prototyp - konstruktor kopiujący
- Being * clone() const { return new Beings( *this ); } //! Prototyp - klonowanie
- //! Kompozyt - Zarządzanie instancjami istot
- void addBeing ( Being* comp )
- {
- beings.push_back( comp );
- }
- //! Kompozyt - Wyświetla swoje Id (bo jest instancja component) oraz wszystkich component ktore posiada
- void printId ()
- {
- std::cout << name << std::endl;
- for ( unsigned i = 0; i < beings.size(); i++ )
- {
- beings[i]->printId();
- }
- }
- };
- //! Singleton, Bog jest tylko jeden :P
- class God
- {
- private:
- //! Singleton - Prywatne konstruktory
- God () {}
- God (const God &);
- God & operator=(const God &);
- ~God () {}
- public:
- Beings beings;
- //! Singleton - Metoda zwracająca tylko jedną instancje
- static God & get()
- {
- //! Singleton - Jedyna instancja klasy
- static God instance;
- return instance;
- }
- };
- int main()
- {
- Being* magdalena = new Female("Magdalena");
- Being* rafal = new Male("Rafal");
- Being* bogdan = new Male("Bogdan");
- //! Sposob Uzycie Singletona, Kompozyta, Prototypa
- God::get().beings.addBeing( magdalena );
- God::get().beings.addBeing( rafal );
- God::get().beings.addBeing( bogdan );
- God::get().beings.printId();
- Being* bogdanClone = bogdan->clone();
- God::get().beings.addBeing( bogdanClone );
- std::cout << std::endl;
- God::get().beings.printId();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement