├── 10_mediatormemento ├── mediator │ ├── ex1 │ │ └── Mediator │ │ │ ├── Colleague.h │ │ │ ├── ConcreteColleague1.h │ │ │ ├── ConcreteColleague2.h │ │ │ ├── ConcreteMediator.h │ │ │ ├── Mediator.cbp │ │ │ ├── Mediator.depend │ │ │ ├── Mediator.h │ │ │ ├── Mediator.layout │ │ │ └── MediatorMain.cpp │ ├── ex2 │ │ └── Chatroom.cpp │ ├── ex3 │ │ └── Thread2.cpp │ └── ex4 │ │ └── PanelMediator.cpp └── memento │ ├── ex1 │ └── Memento │ │ ├── Caretaker.h │ │ ├── Memento.cbp │ │ ├── Memento.depend │ │ ├── Memento.h │ │ ├── Memento.layout │ │ ├── MementoMain.cpp │ │ └── Originator.h │ ├── ex2 │ ├── Caretaker.h │ ├── Memento.cbp │ ├── Memento.depend │ ├── Memento.h │ ├── Memento.layout │ ├── MementoMain.cpp │ └── Originator.h │ └── ex3 │ └── Number.cpp ├── 11_observerstate └── 11_observerstate │ ├── observer │ ├── Project1 │ │ ├── Debug │ │ │ ├── Project1.exe │ │ │ ├── Project1.ilk │ │ │ └── Project1.pdb │ │ ├── Project1.sln │ │ └── Project1 │ │ │ ├── Debug │ │ │ ├── Project1.log │ │ │ ├── Project1.tlog │ │ │ │ ├── CL.command.1.tlog │ │ │ │ ├── CL.read.1.tlog │ │ │ │ ├── CL.write.1.tlog │ │ │ │ ├── Project1.lastbuildstate │ │ │ │ ├── link.command.1.tlog │ │ │ │ ├── link.read.1.tlog │ │ │ │ └── link.write.1.tlog │ │ │ ├── Source.obj │ │ │ ├── vc141.idb │ │ │ └── vc141.pdb │ │ │ ├── Project1.vcxproj │ │ │ ├── Project1.vcxproj.filters │ │ │ └── Source.cpp │ ├── ex1 │ │ └── Observer │ │ │ ├── ConcreteObserver.h │ │ │ ├── ConcreteSubject.h │ │ │ ├── Observer.cbp │ │ │ ├── Observer.depend │ │ │ ├── Observer.h │ │ │ ├── Observer.layout │ │ │ ├── ObserverMain.cpp │ │ │ └── Subject.h │ ├── ex2 │ │ ├── Aukcija.cpp │ │ ├── Aukcija.exe │ │ └── Aukcija.o │ ├── ex3 │ │ └── YouTube.cpp │ └── ex4 │ │ └── PlayerObserver.cpp │ └── state │ ├── ex1 │ └── State │ │ ├── ConcreteStateA.h │ │ ├── ConcreteStateB.h │ │ ├── Context.h │ │ ├── State.cbp │ │ ├── State.depend │ │ ├── State.h │ │ ├── State.layout │ │ ├── StateMain.cpp │ │ ├── bin │ │ └── Debug │ │ │ └── State.exe │ │ └── obj │ │ └── Debug │ │ └── StateMain.o │ ├── ex2 │ └── TCPConnection.cpp │ ├── ex3 │ └── DFAState.cpp │ └── ex4 │ └── PlayerState.cpp ├── 12_strategytemplate ├── strategy │ ├── ex1 │ │ └── Strategy │ │ │ ├── ConcreteStrategyA.h │ │ │ ├── ConcreteStrategyB.h │ │ │ ├── ConcreteStrategyC.h │ │ │ ├── Context.h │ │ │ ├── Strategy.cbp │ │ │ ├── Strategy.depend │ │ │ ├── Strategy.h │ │ │ ├── Strategy.layout │ │ │ ├── StrategyMain.cpp │ │ │ ├── bin │ │ │ └── Debug │ │ │ │ └── Strategy.exe │ │ │ └── obj │ │ │ └── Debug │ │ │ └── StrategyMain.o │ ├── ex2 │ │ └── TextFormatter.cpp │ ├── ex3 │ │ └── Sort.cpp │ ├── ex4 │ │ └── String.cpp │ └── ex5 │ │ └── GamePlayer.cpp └── template_method │ ├── ex1 │ └── TemplateMethod │ │ ├── AbstractClass.h │ │ ├── ConcreteClass.h │ │ ├── TemplateMethod.cbp │ │ ├── TemplateMethod.cpp │ │ ├── TemplateMethod.depend │ │ ├── TemplateMethod.layout │ │ ├── bin │ │ └── Debug │ │ │ └── TemplateMethod.exe │ │ └── obj │ │ └── Debug │ │ └── TemplateMethod.o │ ├── ex2 │ └── Shape.cpp │ └── ex3 │ └── Game.cpp ├── 13_visitor ├── ex1 │ └── Visitor │ │ ├── ConcreteElementA.h │ │ ├── ConcreteElementB.h │ │ ├── ConcreteVisitor1.h │ │ ├── ConcreteVisitor2.h │ │ ├── Element.h │ │ ├── ObjectStructure.h │ │ ├── Visitor.cbp │ │ ├── Visitor.depend │ │ ├── Visitor.h │ │ ├── Visitor.layout │ │ └── VisitorMain.cpp ├── ex2 │ └── PointVisitor.cpp └── ex3 │ └── ShapeVisitor.cpp ├── 1_streams ├── cin_cout.cpp ├── custom_in_out.cpp ├── fin_fout.cpp ├── fin_fout_binary.cpp ├── imenik1.txt ├── imenik2.txt ├── imenik3.txt ├── imenik_primer.cpp ├── matrica.cpp ├── stringstream_example.cpp ├── stringstream_primer.cpp ├── test1.txt ├── test2.txt └── ulaz.txt ├── 2_abstractbuilder ├── abstract_factory │ ├── ex1 │ │ └── AbstractFactory │ │ │ ├── AbstractFactory.cbp │ │ │ ├── AbstractFactory.depend │ │ │ ├── AbstractFactory.h │ │ │ ├── AbstractFactory.layout │ │ │ ├── AbstractFactoryMain.cpp │ │ │ ├── AbstractProductA.h │ │ │ ├── AbstractProductB.h │ │ │ ├── ConcreteFactory1.cpp │ │ │ ├── ConcreteFactory1.h │ │ │ ├── ConcreteFactory2.cpp │ │ │ ├── ConcreteFactory2.h │ │ │ ├── ConcreteProductA1.cpp │ │ │ ├── ConcreteProductA1.h │ │ │ ├── ConcreteProductA2.cpp │ │ │ ├── ConcreteProductA2.h │ │ │ ├── ConcreteProductB1.cpp │ │ │ ├── ConcreteProductB1.h │ │ │ ├── ConcreteProductB2.cpp │ │ │ ├── ConcreteProductB2.h │ │ │ ├── bin │ │ │ └── Debug │ │ │ │ └── AbstractFactory.exe │ │ │ └── obj │ │ │ └── Debug │ │ │ ├── AbstractFactoryMain.o │ │ │ ├── ConcreteFactory1.o │ │ │ ├── ConcreteFactory2.o │ │ │ ├── ConcreteProductA1.o │ │ │ ├── ConcreteProductA2.o │ │ │ ├── ConcreteProductB1.o │ │ │ └── ConcreteProductB2.o │ ├── ex2 │ │ ├── nakon_primene_sablona.cpp │ │ └── pre_primene_sablona.cpp │ ├── ex3 │ │ └── FabrikaOblika.cpp │ └── ex5 │ │ └── FabrikaZivotinja.cpp └── builder │ ├── ex1 │ └── Builder │ │ ├── Builder.cbp │ │ ├── Builder.depend │ │ ├── Builder.h │ │ ├── Builder.layout │ │ ├── BuilderMain.cpp │ │ ├── ConcreteBuilder.h │ │ ├── Director.h │ │ ├── Product.h │ │ ├── bin │ │ └── Debug │ │ │ └── Builder.exe │ │ └── obj │ │ └── Debug │ │ └── BuilderMain.o │ ├── ex2 │ └── GraditeljProizvoda │ │ ├── Graditelj.h │ │ ├── GraditeljProizvoda.cbp │ │ ├── GraditeljProizvoda.depend │ │ ├── GraditeljProizvoda.layout │ │ ├── Proizvod.cpp │ │ ├── Proizvod.h │ │ ├── ProizvodMain.cpp │ │ ├── bin │ │ └── Debug │ │ │ └── GraditeljProizvoda.exe │ │ └── obj │ │ └── Debug │ │ ├── Proizvod.o │ │ └── ProizvodMain.o │ ├── ex3 │ └── PicaGraditelj.cpp │ ├── ex4 │ └── KonvertorTeksta.cpp │ └── ex5 │ └── GraditeljAviona.cpp ├── 3_factorymethodprototype ├── factory_method │ ├── ex1 │ │ └── FactoryMethod │ │ │ ├── ConcreteCreator.cpp │ │ │ ├── ConcreteCreator.h │ │ │ ├── ConcreteProduct.cpp │ │ │ ├── ConcreteProduct.h │ │ │ ├── Creator.h │ │ │ ├── FactoryMethod.cbp │ │ │ ├── FactoryMethod.depend │ │ │ ├── FactoryMethod.layout │ │ │ ├── FactoryMethodMain.cpp │ │ │ └── Product.h │ ├── ex2 │ │ └── MelodijaFabrickiMetod.cpp │ ├── ex3 │ │ └── DokumentFabrickiMetod.cpp │ ├── ex4 │ │ └── MobilniPaketMetod.cpp │ ├── ex5 │ │ └── PicaFabrickiMetod.cpp │ └── ex6 │ │ ├── nakon_upotrebe_fabrickog_metoda.cpp │ │ └── pre_upotrebe_fabrickog_metoda.cpp └── prototype │ ├── ex1 │ └── Prototype │ │ ├── ConcretePrototype1.cpp │ │ ├── ConcretePrototype1.h │ │ ├── ConcretePrototype2.cpp │ │ ├── ConcretePrototype2.h │ │ ├── Prototype.cbp │ │ ├── Prototype.depend │ │ ├── Prototype.h │ │ ├── Prototype.layout │ │ └── PrototypeMain.cpp │ ├── ex2 │ └── OsobaPrototip.cpp │ ├── ex3 │ └── ObjekatPrototip.cpp │ ├── ex4 │ └── celija.cpp │ └── ex5 │ └── PrototipSlike.cpp ├── 4_singletonadapter ├── adapter │ ├── ex1 │ │ └── ClassAdapter │ │ │ ├── Adaptee.h │ │ │ ├── Adapter.h │ │ │ ├── AdapterMain.cpp │ │ │ ├── ClassAdapter.cbp │ │ │ ├── ClassAdapter.depend │ │ │ ├── ClassAdapter.layout │ │ │ └── Target.h │ ├── ex2 │ │ └── ObjectAdapter │ │ │ ├── Adaptee.h │ │ │ ├── Adapter.h │ │ │ ├── AdapterMain.cpp │ │ │ ├── ClassAdapter.layout │ │ │ ├── ObjectAdapter.cbp │ │ │ ├── ObjectAdapter.depend │ │ │ ├── ObjectAdapter.layout │ │ │ └── Target.h │ ├── ex4 │ │ └── HashMap.cpp │ ├── ex5 │ │ └── AdapterIzvrsavanje.cpp │ └── ex6 │ │ └── RacuniAdapter.cpp └── singleton │ ├── ex1 │ └── Singleton │ │ ├── Singleton.cbp │ │ ├── Singleton.depend │ │ ├── Singleton.h │ │ ├── Singleton.layout │ │ ├── SingletonMain.cpp │ │ ├── bin │ │ └── Debug │ │ │ └── Singleton.exe │ │ └── obj │ │ └── Debug │ │ └── SingletonMain.o │ ├── ex3 │ ├── SingletonTemplate.cpp │ └── SingletonTemplate2.cpp │ ├── ex4 │ └── UpravljacStampaca.cpp │ ├── ex5 │ └── FileSystem.cpp │ └── ex6 │ ├── IspravanSingletonParalelan.cpp │ ├── IspravanSingletonParalelan2.cpp │ └── PogresanSingletonParalelan.cpp ├── 5_bridgecomposite ├── bridge │ ├── ex1 │ │ └── Bridge │ │ │ ├── Abstraction.h │ │ │ ├── Bridge.cbp │ │ │ ├── Bridge.depend │ │ │ ├── Bridge.layout │ │ │ ├── BridgeMain.cpp │ │ │ ├── ConcreteImplementorA.h │ │ │ ├── ConcreteImplementorB.h │ │ │ ├── Implementor.h │ │ │ └── RefinedAbstraction.h │ ├── ex2 │ │ └── WindowBridge.cpp │ ├── ex3 │ │ └── PorukaBridge.cpp │ ├── ex4 │ │ ├── StekBridge.cpp │ │ └── StekBridgeUniquePtr.cpp │ └── ex5 │ │ └── CrtanjeBridge.cpp ├── composite │ ├── ex1 │ │ └── CompositePattern │ │ │ ├── Component.h │ │ │ ├── Composite.h │ │ │ ├── CompositeMain.cpp │ │ │ ├── CompositePattern.cbp │ │ │ ├── CompositePattern.depend │ │ │ ├── CompositePattern.layout │ │ │ └── Leaf.h │ ├── ex2 │ │ └── Oprema.cpp │ ├── ex3 │ │ └── CompositeTemplate.cpp │ └── ex4 │ │ └── ElementiZaCrtanje.cpp └── v5_07112019 │ ├── directories_composite.cpp │ └── korpa_bridge.cpp ├── 6_facadedecorator ├── decorator │ ├── ex1 │ │ └── Decorator │ │ │ ├── Component.h │ │ │ ├── ConcreteComponent.h │ │ │ ├── ConcreteDecorator.h │ │ │ ├── Decorator.cbp │ │ │ ├── Decorator.depend │ │ │ ├── Decorator.h │ │ │ ├── Decorator.layout │ │ │ └── DecoratorMain.cpp │ ├── ex2 │ │ └── ProzorDopuna.cpp │ ├── ex3 │ │ ├── ZivotinjaDecorator.cpp │ │ ├── ZivotinjaDecorator.exe │ │ └── ZivotinjaDecorator.o │ ├── ex5 │ │ └── BibliotekaDopuna.cpp │ └── ex6 │ │ └── StringDecorator.cpp └── facade │ ├── ex1 │ └── Facade │ │ ├── Class1.h │ │ ├── Class2.h │ │ ├── Class3.h │ │ ├── Facade.cbp │ │ ├── Facade.depend │ │ ├── Facade.h │ │ ├── Facade.layout │ │ └── FacadeMain.cpp │ ├── ex2 │ └── FajlTransfer.cpp │ ├── ex3 │ └── Kredit.cpp │ ├── ex4 │ └── Kompjuter.cpp │ └── ex5 │ └── Computer.cpp ├── 7_flyweightproxy ├── flyweight │ ├── ex1 │ │ └── Flyweight │ │ │ ├── ConcreteFlyweight.h │ │ │ ├── Flyweight.cbp │ │ │ ├── Flyweight.depend │ │ │ ├── Flyweight.h │ │ │ ├── Flyweight.layout │ │ │ ├── FlyweightFactory.h │ │ │ ├── FlyweightMain.cpp │ │ │ └── UnsharedConcreteFlyweight.h │ ├── ex3 │ │ └── SlovaMuva.cpp │ ├── ex4 │ │ └── SlikeDrzava.cpp │ └── ex5 │ │ └── RatnaIgra.cpp └── proxy │ ├── ex1 │ └── Proxy │ │ ├── Proxy.cbp │ │ ├── Proxy.depend │ │ ├── Proxy.h │ │ ├── Proxy.layout │ │ ├── ProxyMain.cpp │ │ ├── RealSubject.h │ │ ├── Subject.h │ │ ├── bin │ │ └── Debug │ │ │ └── Proxy.exe │ │ └── obj │ │ └── Debug │ │ └── ProxyMain.o │ ├── ex2 │ └── SlikaZastupnik.cpp │ └── ex3 │ └── BankaZastupnik.cpp ├── 8_chainofresponsibilitycommand └── 8_chainofresponsibilitycommand │ ├── chain_of_responsibility │ ├── ex1 │ │ └── ChainOfResponsibility │ │ │ ├── ChainOfResponsibility.cbp │ │ │ ├── ChainOfResponsibility.depend │ │ │ ├── ChainOfResponsibility.layout │ │ │ ├── ChainOfResponsibilityMain.cpp │ │ │ ├── ConcreteHandler1.h │ │ │ ├── ConcreteHandler2.h │ │ │ ├── Handler.h │ │ │ └── Request.h │ ├── ex2 │ │ └── TesterDeljivosti.cpp │ ├── ex3 │ │ └── TreeHandler.cpp │ ├── ex4 │ │ └── FliperLanacOdgovornosti.cpp │ └── ex5 │ │ └── ProzorLanacOdgovornosti.cpp │ └── command │ ├── ex1 │ └── Command │ │ ├── Command.cbp │ │ ├── Command.depend │ │ ├── Command.h │ │ ├── Command.layout │ │ ├── CommandMain.cpp │ │ ├── ConcreteCommand.h │ │ ├── Invoker.h │ │ └── Receiver.h │ ├── ex2 │ └── DocumentCommand.cpp │ ├── ex3 │ └── DigitronKomanda.cpp │ └── ex4 │ └── IgracKomanda.cpp ├── 9_interpreteriterator ├── interpreter │ ├── ex1 │ │ └── Interpreter │ │ │ ├── AbstractExpression.h │ │ │ ├── Context.h │ │ │ ├── Interpreter.cbp │ │ │ ├── Interpreter.depend │ │ │ ├── Interpreter.layout │ │ │ ├── InterpreterMain.cpp │ │ │ ├── NonterminalExpression.h │ │ │ └── TerminalExpression.h │ ├── ex2 │ │ └── ExpressionIntepret.cpp │ ├── ex3 │ │ └── BooleanExpression.cpp │ └── ex4 │ │ └── RomanNumbersInterpreter.cpp └── iterator │ ├── ex1 │ └── Iterator │ │ ├── Aggregate.h │ │ ├── ConcreteAggregate.h │ │ ├── ConcreteIterator.h │ │ ├── Iterator.cbp │ │ ├── Iterator.depend │ │ ├── Iterator.h │ │ ├── Iterator.layout │ │ └── IteratorMain.cpp │ ├── ex2 │ ├── StackIterator.cpp │ └── StackIteratorOperators.cpp │ ├── ex3 │ └── TemplateIterator.cpp │ └── ex4 │ └── Tree.cpp ├── Alexander Shvets Design Patterns Explained Simply.pdf ├── Design Patterns, Elements of Reusable Object-Oriented Software.pdf ├── Lecture notes ├── 01. Abstract factory │ ├── README.md │ └── main.cpp ├── 04. Prototype │ ├── README.md │ ├── main.cpp │ └── primer_2.cpp ├── 05. Singleton │ ├── README.md │ └── singleton.cpp ├── 06. Adapter │ ├── AdapterClass.cpp │ ├── AdapterObject.cpp │ ├── README.md │ └── primer_2.cpp ├── 07. Bridge │ ├── README.md │ ├── main.cpp │ └── primer_2.cpp ├── 08. Composite │ ├── README.md │ └── main.cpp ├── 09. Decorator │ ├── README.md │ └── main.cpp ├── 10. Facade │ ├── README.md │ └── main.cpp ├── 11. Flyweight │ ├── README.md │ └── main.cpp ├── 12. Proxy │ ├── README.md │ └── main.cpp ├── 13. Chain of responsibility │ └── main.cpp ├── 17. Mediator │ └── main.cpp ├── 18. Memento │ └── main.cpp ├── 19. Observer │ └── main.cpp ├── 20. State │ └── main.cpp ├── 21. Strategy │ └── main.cpp ├── 22. Template Method │ └── main.cpp ├── 23. Visitor │ └── main.cpp ├── README.md └── Sa predavanja │ ├── Bridge │ ├── primer1.cpp │ └── primer3.cpp │ ├── CRTP │ └── crtp.cpp │ ├── Chain Of Responsibility │ └── Primer1.cpp │ ├── Command │ └── Command1.cpp │ ├── Decorator │ └── primer1.cpp │ ├── Facade │ └── primer1.cpp │ ├── Iterator │ └── iterator.cpp │ ├── Mediator │ └── Mediator.cpp │ ├── Memento │ └── Memento.cpp │ ├── Observer │ └── Observer.cpp │ ├── Prototype │ └── primer2.cpp │ ├── Proxy │ ├── primer1.cpp │ └── vector.cpp │ └── Visitor │ └── Visitor.cpp ├── Partha Kuchana - Software Architecture Design Patterns in Java (2004).pdf ├── README.md ├── SimplePatterns ├── Sve.vcxproj ├── Sve.vcxproj.filters ├── abstractfactory.cpp ├── abstractfactory.h ├── adapter.cpp ├── adapter.h ├── bridge.cpp ├── bridge.h ├── builder.cpp ├── builder.h ├── chainofresponsibility.cpp ├── chainofresponsibility.h ├── command.cpp ├── command.h ├── composite.cpp ├── composite.h ├── decorator.cpp ├── decorator.h ├── facade.cpp ├── facade.h ├── factorymethod.cpp ├── factorymethod.h ├── flyweight.cpp ├── flyweight.h ├── interpreter.cpp ├── interpreter.h ├── iterator.cpp ├── iterator.h ├── main.cpp ├── mediator.cpp ├── mediator.h ├── memento.cpp ├── memento.h ├── observer.cpp ├── observer.h ├── prototype.cpp ├── prototype.h ├── proxy.cpp ├── proxy.h ├── singleton.cpp ├── singleton.h ├── state.cpp ├── state.h ├── strategy.cpp ├── strategy.h ├── templatemethod.cpp ├── templatemethod.h ├── visitor.cpp └── visitor.h ├── _config.yml └── stl ├── algorithm.cpp ├── lista.cpp ├── mapa.cpp ├── queue.cpp ├── smart_pointers.cpp ├── stack.cpp └── vector.cpp /10_mediatormemento/mediator/ex1/Mediator/Colleague.h: -------------------------------------------------------------------------------- 1 | #ifndef __COLLEAGUE_H 2 | #define __COLLEAGUE_H 3 | 4 | #include "Mediator.h" 5 | 6 | /* interfejs kolega/ucesnika u komunikaciji 7 | poseduje metode za slanje Send(msg) i primanje Receive(msg) poruka 8 | poseduje i referencu na posrednika/mediatora koga koristi prilikom komunikacije 9 | sa ostalim ucesnicima */ 10 | class Colleague { 11 | public: 12 | Colleague(Mediator* mediator): m_mediator(mediator) {} 13 | virtual ~Colleague() {} 14 | virtual void Send(const string& message) = 0; 15 | virtual void Receive(const string& message) = 0; 16 | protected: 17 | Mediator* m_mediator; 18 | }; 19 | 20 | #endif // __COLLEAGUE_H 21 | -------------------------------------------------------------------------------- /10_mediatormemento/mediator/ex1/Mediator/ConcreteColleague1.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETECOLLEAGUE1_H 2 | #define __CONCRETECOLLEAGUE1_H 3 | 4 | #include "Colleague.h" 5 | 6 | /* konkretan kolega 7 | implementira metod Send tako sto pozove metod SendMessage na posredniku/medijatoru 8 | implementira i metod Receive preko koga mu se dopremaju poruke (u nasem slucaju 9 | ce posrednik pomocu ovog metoda da mu prosledjuje poruke) */ 10 | class ConcreteColleague1: public Colleague { 11 | public: 12 | ConcreteColleague1(Mediator* mediator): Colleague(mediator) {} 13 | virtual void Send(const string& message) { 14 | cout << "ConcreteColleague1.Send(" << message << ")" << endl; 15 | m_mediator->SendMessage(message, this); 16 | } 17 | virtual void Receive(const string& message) { 18 | cout << "ConcreteColleague1.Receive(" << message << ")" << endl; 19 | } 20 | }; 21 | 22 | #endif // __CONCRETECOLLEAGUE1_H 23 | -------------------------------------------------------------------------------- /10_mediatormemento/mediator/ex1/Mediator/ConcreteColleague2.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETECOLLEAGUE2_H 2 | #define __CONCRETECOLLEAGUE2_H 3 | 4 | #include "Colleague.h" 5 | 6 | /* konkretan kolega 7 | implementira metod Send tako sto pozove metod SendMessage na posredniku/medijatoru 8 | implementira i metod Receive preko koga mu se dopremaju poruke (u nasem slucaju 9 | ce posrednik pomocu ovog metoda da mu prosledjuje poruke) */ 10 | class ConcreteColleague2: public Colleague { 11 | public: 12 | ConcreteColleague2(Mediator* mediator): Colleague(mediator) {} 13 | virtual void Send(const string& message) { 14 | cout << "ConcreteColleague2.Send(" << message << ")" << endl; 15 | m_mediator->SendMessage(message, this); 16 | } 17 | virtual void Receive(const string& message) { 18 | cout << "ConcreteColleague2.Receive(" << message << ")" << endl; 19 | } 20 | }; 21 | 22 | #endif // __CONCRETECOLLEAGUE2_H 23 | 24 | -------------------------------------------------------------------------------- /10_mediatormemento/mediator/ex1/Mediator/Mediator.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 43 | 44 | -------------------------------------------------------------------------------- /10_mediatormemento/mediator/ex1/Mediator/Mediator.h: -------------------------------------------------------------------------------- 1 | #ifndef __MEDIATOR_H 2 | #define __MEDIATOR_H 3 | 4 | #include 5 | #include 6 | 7 | #include "Colleague.h" 8 | 9 | using namespace std; 10 | 11 | class Colleague; 12 | 13 | /* mediator / posrednik 14 | apstraktna klasa, nudi metode koji omogucavaju da korisnici, kolete / Colleague 15 | komuniciraju medjusobno, 16 | tako se broj medjusobnih veza za komunikaciju izmedju n ucesnika redukuje sa 17 | n * (n - 1) / 2 na samo n veza (veze izmedju konkretnih posrednika/mediatora i kolega/Collegaues) 18 | interfejs nudi jedan metod koji se koristi za slanje poruka SendMessage gde se kao parametri 19 | stavljaju poruka koju korisnik/kolega salje i sam posiljalac colleague 20 | ovaj metod SendMessage pozivaju kolege da bi poslali poruku drugim ucesnicima */ 21 | class Mediator { 22 | public: 23 | virtual void SendMessage(const string& message, Colleague* colleague) = 0; 24 | }; 25 | #endif // __MEDIATOR_H 26 | -------------------------------------------------------------------------------- /10_mediatormemento/mediator/ex1/Mediator/MediatorMain.cpp: -------------------------------------------------------------------------------- 1 | #include "ConcreteMediator.h" 2 | 3 | /* kreiramo posrednika/mediatora 4 | dva ucesnika u komunikaciji kolegu1 i kolegu2 5 | nakon toga posredniku postavimo ucesnike komunikacije setColl1 i setColl2 6 | posle toga kolega1 posalje poruku, nakon toga kolega 2 i mozemo da vidimo 7 | rezultate komunikacije */ 8 | int main() { 9 | ConcreteMediator* mediator = new ConcreteMediator(); 10 | ConcreteColleague1* colleague1 = new ConcreteColleague1(mediator); 11 | ConcreteColleague2* colleague2 = new ConcreteColleague2(mediator); 12 | 13 | mediator->SetColleague1(colleague1); 14 | mediator->SetColleague2(colleague2); 15 | 16 | colleague1->Send("Pozdrav od kolege C1"); 17 | colleague2->Send("Evo i ja se javljam, tvoj drugar C2"); 18 | 19 | delete mediator; 20 | delete colleague1; 21 | delete colleague2; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /10_mediatormemento/memento/ex1/Memento/Caretaker.h: -------------------------------------------------------------------------------- 1 | #ifndef __CARETAKER_H 2 | #define __CARETAKER_H 3 | 4 | #include 5 | #include "Memento.h" 6 | 7 | using namespace std; 8 | 9 | /* cuvar stanja/ caretaker 10 | cuva listu kreiranih podsetnika/memento 11 | moguce je uzeti neki podsetnik iz istorije kreiranih podsetnika 12 | i na taj nacin vratiti stanje objekta unazad kroz istoriju, 13 | imati opciju undo */ 14 | class Caretaker { 15 | public: 16 | virtual ~Caretaker() { 17 | for (vector::iterator it = m_memento.begin(); it != m_memento.end(); ++it) { 18 | delete *it; 19 | } 20 | } 21 | Memento* GetMemento(unsigned int index = 0) { 22 | if (index < m_memento.size()) return m_memento[m_memento.size() - index - 1]; 23 | return NULL; 24 | } 25 | void AddMemento(Memento* memento) { 26 | m_memento.push_back(memento); 27 | } 28 | protected: 29 | vector m_memento; 30 | }; 31 | 32 | #endif // __CARETAKER_H 33 | -------------------------------------------------------------------------------- /10_mediatormemento/memento/ex1/Memento/Memento.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 35 | 36 | -------------------------------------------------------------------------------- /10_mediatormemento/memento/ex1/Memento/Memento.depend: -------------------------------------------------------------------------------- 1 | # depslib dependency file v1.0 2 | 1450963983 source:c:\users\sidejan\google drive\oop2\vezbe_sredjene\10_mediator_memento\memento\ex1\memento\mementomain.cpp 3 | "Originator.h" 4 | "Caretaker.h" 5 | 6 | 1450963950 c:\users\sidejan\google drive\oop2\vezbe_sredjene\10_mediator_memento\memento\ex1\memento\originator.h 7 | "Memento.h" 8 | 9 | 1450963797 c:\users\sidejan\google drive\oop2\vezbe_sredjene\10_mediator_memento\memento\ex1\memento\memento.h 10 | 11 | 12 | 13 | 1450964014 c:\users\sidejan\google drive\oop2\vezbe_sredjene\10_mediator_memento\memento\ex1\memento\caretaker.h 14 | 15 | "Memento.h" 16 | 17 | 1450931686 source:d:\___predavanja\oop2\lekcije\10_mediator_memento\memento\ex1\memento\mementomain.cpp 18 | "Originator.h" 19 | "Caretaker.h" 20 | 21 | 1450931550 d:\___predavanja\oop2\lekcije\10_mediator_memento\memento\ex1\memento\originator.h 22 | "Memento.h" 23 | 24 | 1450931396 d:\___predavanja\oop2\lekcije\10_mediator_memento\memento\ex1\memento\memento.h 25 | 26 | 27 | 28 | 1450979283 d:\___predavanja\oop2\lekcije\10_mediator_memento\memento\ex1\memento\caretaker.h 29 | 30 | "Memento.h" 31 | 32 | -------------------------------------------------------------------------------- /10_mediatormemento/memento/ex1/Memento/Memento.h: -------------------------------------------------------------------------------- 1 | #ifndef __MEMENTO_H 2 | #define __MEMENTO_H 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | /* podsetnik/memento 9 | putem konstruktora postavljamo interno stanje objekta koje zelimo da sacuvamo 10 | metodom GetState mozemo da pristupimo tom internom stanju, sto bas i nije dobra praksa 11 | jer je tako to stanje izlozeno javno */ 12 | class Memento { 13 | public: 14 | Memento(const string& state): m_state(state) {} 15 | string GetState() { return m_state; } 16 | protected: 17 | string m_state; 18 | }; 19 | 20 | #endif // __MEMENTO_H 21 | -------------------------------------------------------------------------------- /10_mediatormemento/memento/ex1/Memento/Memento.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /10_mediatormemento/memento/ex1/Memento/MementoMain.cpp: -------------------------------------------------------------------------------- 1 | #include "Originator.h" 2 | #include "Caretaker.h" 3 | 4 | /* kreiramo subjekat/originator koji ima svoje interno stanje 5 | i nudi operacije za promenu internog stanja (metod Do()) 6 | nakon izvrsene neke promene u cuvaru podsetnika / caretaker 7 | dodamo novokreirani podsetnik subjekta, pozivajuci metod createMemento, 8 | zatim uradim jos neke operacije na subjektu a zatim vratimo neko od prethodnih 9 | stanja uzimajuci podsetnik iz cuvara podsetnika */ 10 | int main() { 11 | Originator origin; 12 | Caretaker caretaker; 13 | 14 | origin.Do("Prva linija"); 15 | caretaker.AddMemento(origin.CreateMemento()); 16 | origin.Display(); 17 | 18 | origin.Do("Druga linija"); 19 | origin.Display(); 20 | 21 | origin.SetMemento(caretaker.GetMemento(0)); 22 | origin.Display(); 23 | 24 | origin.Do("Treca linija"); 25 | caretaker.AddMemento(origin.CreateMemento()); 26 | origin.Display(); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /10_mediatormemento/memento/ex1/Memento/Originator.h: -------------------------------------------------------------------------------- 1 | #ifndef __ORIGINATOR_H 2 | 3 | #include "Memento.h" 4 | /* originator/subjekat 5 | poseduje neko interno stanje koje ne zeli da izlaze spolja, 6 | kako bismo sacuvali interno stanje tokom evolucije objekta 7 | kreiramo memento/podsetnik, koristeci metod CreateMemento, koji ce da sacuva trenutno 8 | interno stanje objekta 9 | da bismo vratili stanje objekta na neko prethodno koristimo metod SetMemento 10 | koji koristeci prosledjeni memento/podsetnik postavi trenutno interno stanje 11 | objekta na ono koje je sacuvano u podsetniku/mementu */ 12 | class Originator { 13 | public: 14 | Memento* CreateMemento() { 15 | return new Memento(m_state); 16 | } 17 | void SetMemento(Memento* m) { 18 | m_state = m->GetState(); 19 | } 20 | void Do(string newState) { m_state = newState; } 21 | void Display() { cout << "[Originator] state = " << m_state << endl; } 22 | protected: 23 | string m_state; 24 | }; 25 | 26 | #endif // __ORIGINATOR_H 27 | -------------------------------------------------------------------------------- /10_mediatormemento/memento/ex2/Caretaker.h: -------------------------------------------------------------------------------- 1 | #ifndef __CARETAKER_H 2 | #define __CARETAKER_H 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Originator; 9 | /* cuvar podsetnika */ 10 | class Caretaker { 11 | public: 12 | virtual ~Caretaker() { 13 | for (vector::iterator it = m_memento.begin(); it != m_memento.end(); ++it) { 14 | delete *it; 15 | } 16 | } 17 | void RestoreState(unsigned int index) { 18 | if (index < m_memento.size()) m_memento[m_memento.size() - index - 1]->RestoreState(); 19 | else throw("Undefined index"); 20 | } 21 | void AddMemento(Originator::Memento* memento) { 22 | m_memento.push_back(memento); 23 | } 24 | protected: 25 | vector m_memento; 26 | }; 27 | 28 | #endif // __CARETAKER_H 29 | -------------------------------------------------------------------------------- /10_mediatormemento/memento/ex2/Memento.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 31 | 32 | -------------------------------------------------------------------------------- /10_mediatormemento/memento/ex2/Memento.h: -------------------------------------------------------------------------------- 1 | #ifndef __MEMENTO_H 2 | #define __MEMENTO_H 3 | 4 | #include 5 | #include 6 | 7 | #include "Originator.h" 8 | 9 | using namespace std; 10 | 11 | 12 | 13 | #endif // __MEMENTO_H 14 | -------------------------------------------------------------------------------- /10_mediatormemento/memento/ex2/Memento.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /10_mediatormemento/memento/ex2/MementoMain.cpp: -------------------------------------------------------------------------------- 1 | #include "Originator.h" 2 | #include "Caretaker.h" 3 | 4 | /* simulacija kao u prvom primeru */ 5 | int main() { 6 | Originator origin; 7 | Caretaker caretaker; 8 | 9 | origin.Do("Prva linija"); 10 | caretaker.AddMemento(origin.CreateMemento()); 11 | origin.Display(); 12 | 13 | origin.Do("Druga linija"); 14 | origin.Display(); 15 | 16 | caretaker.RestoreState(0); 17 | origin.Display(); 18 | 19 | origin.Do("Treca linija"); 20 | 21 | caretaker.AddMemento(origin.CreateMemento()); 22 | origin.Display(); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /10_mediatormemento/memento/ex2/Originator.h: -------------------------------------------------------------------------------- 1 | #ifndef __ORIGINATOR_H 2 | 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | /* u ovom slucaju subjekt/originator 8 | ima klasu clanicu Memento/podsetnik 9 | u ovom slucaju ne izlazemo stanje sacuvano u podsetniku javno putem nekog metod, 10 | moguce je i prvi primer (ex1) izmeniti kako bi sledio taj princip */ 11 | class Originator { 12 | public: 13 | class Memento { 14 | public: 15 | Memento(Originator* originator, const string& state): m_state(state), m_originator(originator) {} 16 | void RestoreState() { m_originator->m_state = m_state; } 17 | protected: 18 | string m_state; 19 | Originator* m_originator; 20 | }; 21 | Memento* CreateMemento() { 22 | return new Memento(this, m_state); 23 | } 24 | void Do(string newState) { m_state = newState; } 25 | void Display() { cout << "[Originator] state = " << m_state << endl; } 26 | protected: 27 | string m_state; 28 | }; 29 | 30 | #endif // __ORIGINATOR_H 31 | -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/Project1/Debug/Project1.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/11_observerstate/11_observerstate/observer/Project1/Debug/Project1.exe -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/Project1/Debug/Project1.ilk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/11_observerstate/11_observerstate/observer/Project1/Debug/Project1.ilk -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/Project1/Debug/Project1.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/11_observerstate/11_observerstate/observer/Project1/Debug/Project1.pdb -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/Project1/Project1/Debug/Project1.log: -------------------------------------------------------------------------------- 1 |  Source.cpp 2 | Project1.vcxproj -> C:\Users\jaksa97\Desktop\OOP2 Primeri\11_observerstate\11_observerstate\observer\Project1\Debug\Project1.exe 3 | -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/Project1/Project1/Debug/Project1.tlog/CL.command.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/11_observerstate/11_observerstate/observer/Project1/Project1/Debug/Project1.tlog/CL.command.1.tlog -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/Project1/Project1/Debug/Project1.tlog/CL.read.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/11_observerstate/11_observerstate/observer/Project1/Project1/Debug/Project1.tlog/CL.read.1.tlog -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/Project1/Project1/Debug/Project1.tlog/CL.write.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/11_observerstate/11_observerstate/observer/Project1/Project1/Debug/Project1.tlog/CL.write.1.tlog -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/Project1/Project1/Debug/Project1.tlog/Project1.lastbuildstate: -------------------------------------------------------------------------------- 1 | #TargetFrameworkVersion=v4.0:PlatformToolSet=v141:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=10.0.16299.0 2 | Debug|Win32|C:\Users\jaksa97\Desktop\OOP2 Primeri\11_observerstate\11_observerstate\observer\Project1\| 3 | -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/Project1/Project1/Debug/Project1.tlog/link.command.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/11_observerstate/11_observerstate/observer/Project1/Project1/Debug/Project1.tlog/link.command.1.tlog -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/Project1/Project1/Debug/Project1.tlog/link.read.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/11_observerstate/11_observerstate/observer/Project1/Project1/Debug/Project1.tlog/link.read.1.tlog -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/Project1/Project1/Debug/Project1.tlog/link.write.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/11_observerstate/11_observerstate/observer/Project1/Project1/Debug/Project1.tlog/link.write.1.tlog -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/Project1/Project1/Debug/Source.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/11_observerstate/11_observerstate/observer/Project1/Project1/Debug/Source.obj -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/Project1/Project1/Debug/vc141.idb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/11_observerstate/11_observerstate/observer/Project1/Project1/Debug/vc141.idb -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/Project1/Project1/Debug/vc141.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/11_observerstate/11_observerstate/observer/Project1/Project1/Debug/vc141.pdb -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/Project1/Project1/Project1.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/ex1/Observer/ConcreteObserver.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETEOBSERVER_H 2 | #define __CONCRETEOBSERVER_H 3 | 4 | #include 5 | #include 6 | 7 | #include "Observer.h" 8 | 9 | using namespace std; 10 | 11 | /* konkretan posmatrac 12 | metod Update je implementiran tako da prikaze da je izvrsen i sacuva 13 | interno stanje subjekta koje je najsvezije */ 14 | class ConcreteObserver: public Observer { 15 | public: 16 | ConcreteObserver(): m_state("") {} 17 | virtual ~ConcreteObserver() {} 18 | virtual void Update(Subject* subject) { 19 | cout << "ConcreteObserver.Update(" << subject->GetState() << ")" << endl; 20 | m_state = subject->GetState(); 21 | } 22 | protected: 23 | string m_state; 24 | }; 25 | 26 | #endif // __CONCRETEOBSERVER_H 27 | -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/ex1/Observer/ConcreteSubject.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETESUBJECT_H 2 | #define __CONCRETESUBJECT_H 3 | 4 | #include 5 | #include 6 | 7 | #include "Subject.h" 8 | 9 | using namespace std; 10 | /* konkretan subjekt, 11 | implementira virtuelne metode 12 | u ovom slucaju SetState je implementiran tako da prilikom promene stanja 13 | ne poziva se automatski Notify na subjektu, 14 | vec je to zadatak korisnika konkretnog subjekta da nakon poziva SetState 15 | pozove Notify ako misli da je bitno da posmatraci budu obavesteni o dogadjaju */ 16 | class ConcreteSubject: public Subject { 17 | public: 18 | ConcreteSubject() {} 19 | virtual ~ConcreteSubject() {} 20 | virtual string GetState() { 21 | return m_state; 22 | } 23 | virtual void SetState(const string& state) { 24 | cout << "ConcreteSubject.SetState(" << state << ")" << endl; 25 | m_state = state; 26 | } 27 | protected: 28 | string m_state; 29 | }; 30 | 31 | #endif // __CONCRETESUBJECT_H 32 | -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/ex1/Observer/Observer.h: -------------------------------------------------------------------------------- 1 | #ifndef __OBSERVER_H 2 | #define __OBSERVER_H 3 | 4 | #include "Subject.h" 5 | 6 | class Subject; 7 | /* apstraktan posmatrac/observer 8 | poseduje metod Update(subjekt) preko koga subjekt kojem se desila promena od interesa 9 | moze da obavesti registrovanog posmatraca o dogadjaju */ 10 | class Observer { 11 | public: 12 | Observer() {} 13 | virtual ~Observer() {} 14 | virtual void Update(Subject* subject) = 0; 15 | }; 16 | 17 | #endif // __OBSERVER_H 18 | -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/ex1/Observer/Observer.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/ex1/Observer/ObserverMain.cpp: -------------------------------------------------------------------------------- 1 | #include "ConcreteSubject.h" 2 | #include "ConcreteObserver.h" 3 | 4 | /* kreiramo subjekta koji moze da trpi promene internog stanja i obavesti 5 | registrovane posmatrace o promeni 6 | kreiramo dva konkretna posmatraca, registrujemo ih kod subjekta 7 | promenimo stanje subjekta i pozovemo metod Notify spolja 8 | subjekt ce pri pozivu ovog metoda da pozove Update kod svih registrovanih posmatraca */ 9 | int main() { 10 | Subject* subject = new ConcreteSubject(); 11 | Observer* observer1 = new ConcreteObserver(); 12 | Observer* observer2 = new ConcreteObserver(); 13 | 14 | subject->Attach(observer1); 15 | subject->Attach(observer2); 16 | 17 | subject->SetState("Novo stanje A"); 18 | subject->Notify(); 19 | 20 | subject->Detach(observer1); 21 | subject->SetState("Novo stanje B"); 22 | subject->Notify(); 23 | 24 | delete subject; 25 | delete observer1; 26 | delete observer2; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/ex2/Aukcija.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/11_observerstate/11_observerstate/observer/ex2/Aukcija.exe -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/observer/ex2/Aukcija.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/11_observerstate/11_observerstate/observer/ex2/Aukcija.o -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/state/ex1/State/ConcreteStateA.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETESTATEA_H 2 | #define __CONCRETESTATEA_H 3 | 4 | #include 5 | 6 | #include "State.h" 7 | 8 | using namespace std; 9 | 10 | /* konkretno stanje / concrete state 11 | implementira metod handle koji konktekst zove prilikom razresavanja nekog 12 | zahteva */ 13 | class ConcreteStateA: public State { 14 | public: 15 | ConcreteStateA() {} 16 | virtual ~ConcreteStateA() {} 17 | virtual void Handle() { 18 | cout << "ConcreteStateA.Handle()" << endl; 19 | } 20 | }; 21 | 22 | #endif // __CONCRETESTATEA_H 23 | -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/state/ex1/State/ConcreteStateB.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETESTATEB_H 2 | #define __CONCRETESTATEB_H 3 | 4 | #include 5 | 6 | #include "State.h" 7 | 8 | using namespace std; 9 | 10 | /* konkretno stanje / concrete state 11 | implementira metod handle koji konktekst zove prilikom razresavanja nekog 12 | zahteva */ 13 | class ConcreteStateB: public State { 14 | public: 15 | ConcreteStateB() {} 16 | virtual ~ConcreteStateB() {} 17 | virtual void Handle() { 18 | cout << "ConcreteStateB.Handle()" << endl; 19 | } 20 | }; 21 | 22 | #endif // __CONCRETESTATEB_H 23 | 24 | -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/state/ex1/State/State.h: -------------------------------------------------------------------------------- 1 | #ifndef __STATE_H 2 | #define __STATE_H 3 | 4 | /* apstraktno stanje / state 5 | poseduje metod Handle koji kontekst zove da bi 6 | razresio neki zahtev konteksta, 7 | moguce je preko metoda Handle da prosledimo i referencu 8 | na kontekst koji je zatrazio razresenje nekog zahteva 9 | i da na taj nacin konkretno stanje moze da izvrsi 10 | odgovarajuce modifikacije pristupajuci konktekstu */ 11 | class State { 12 | public: 13 | State() {} 14 | virtual ~State() { } 15 | virtual void Handle() = 0; 16 | }; 17 | 18 | #endif // __STATE_H 19 | -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/state/ex1/State/State.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/state/ex1/State/StateMain.cpp: -------------------------------------------------------------------------------- 1 | #include "Context.h" 2 | 3 | /* kreiramo kontekst, u konstruktoru ce se postaviti neko pocetno konkretno 4 | stanje, kada zovnemo metod Request zahtev ce biti prosledjen putem metoda Handle 5 | na stanju, trenutnom konkretnom stanju da razresi taj zahtev, 6 | nakon toga promenimo stanje i opet razresimo neki zahtev, 7 | i mozemo da pratimo tok izvrsavanja */ 8 | int main() { 9 | Context context; 10 | 11 | context.Request(); 12 | 13 | context.ChangeState(); 14 | context.Request(); 15 | 16 | context.ChangeState(); 17 | context.Request(); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/state/ex1/State/bin/Debug/State.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/11_observerstate/11_observerstate/state/ex1/State/bin/Debug/State.exe -------------------------------------------------------------------------------- /11_observerstate/11_observerstate/state/ex1/State/obj/Debug/StateMain.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/11_observerstate/11_observerstate/state/ex1/State/obj/Debug/StateMain.o -------------------------------------------------------------------------------- /12_strategytemplate/strategy/ex1/Strategy/ConcreteStrategyA.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETESTRATEGYA_H 2 | #define __CONCRETESTRATEGYA_H 3 | 4 | #include "Strategy.h" 5 | 6 | /* konkretna strategija / concrete strategy 7 | daje implementaciju metoda AlgorithmInterface 8 | u nasem slucaju samo prikazujemo da je metod pozvan */ 9 | class ConcreteStrategyA: public Strategy { 10 | public: 11 | virtual void AlgorithmInterface() { 12 | cout << "ConcreteStrategyA.AlgorithmInterface()" << endl; 13 | } 14 | }; 15 | 16 | #endif // __CONCRETESTRATEGYA_H 17 | -------------------------------------------------------------------------------- /12_strategytemplate/strategy/ex1/Strategy/ConcreteStrategyB.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETESTRATEGYB_H 2 | #define __CONCRETESTRATEGYB_H 3 | 4 | #include "Strategy.h" 5 | 6 | /* konkretna strategija / concrete strategy 7 | daje implementaciju metoda AlgorithmInterface 8 | u nasem slucaju samo prikazujemo da je metod pozvan */ 9 | class ConcreteStrategyB: public Strategy { 10 | public: 11 | virtual void AlgorithmInterface() { 12 | cout << "ConcreteStrategyB.AlgorithmInterface()" << endl; 13 | } 14 | }; 15 | 16 | #endif // __CONCRETESTRATEGYB_H 17 | -------------------------------------------------------------------------------- /12_strategytemplate/strategy/ex1/Strategy/ConcreteStrategyC.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETESTRATEGYC_H 2 | #define __CONCRETESTRATEGYC_H 3 | 4 | #include "Strategy.h" 5 | 6 | /* konkretna strategija / concrete strategy 7 | daje implementaciju metoda AlgorithmInterface 8 | u nasem slucaju samo prikazujemo da je metod pozvan */ 9 | class ConcreteStrategyC: public Strategy { 10 | public: 11 | virtual void AlgorithmInterface() { 12 | cout << "ConcreteStrategyC.AlgorithmInterface()" << endl; 13 | } 14 | }; 15 | 16 | #endif // __CONCRETESTRATEGYC_H 17 | 18 | -------------------------------------------------------------------------------- /12_strategytemplate/strategy/ex1/Strategy/Strategy.h: -------------------------------------------------------------------------------- 1 | #ifndef __STRATEGY_H 2 | #define __STRATEGY_H 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | /* apstraktna strategija / strategy 9 | poseduje metod AlgorithmInterface kojim se resava neki problem 10 | razlicite konkretne strategije mogu da ponude sustinski razlicite 11 | implementacije ovog metoda, 12 | bilo u razlicitoj efikasnosti, bilo u razlicitim efektima koje 13 | taj metod moze da izazove na kontekstu */ 14 | class Strategy { 15 | public: 16 | virtual void AlgorithmInterface() = 0; 17 | virtual ~Strategy() { } 18 | }; 19 | 20 | #endif // __STRATEGY_H 21 | -------------------------------------------------------------------------------- /12_strategytemplate/strategy/ex1/Strategy/StrategyMain.cpp: -------------------------------------------------------------------------------- 1 | #include "Context.h" 2 | #include "ConcreteStrategyA.h" 3 | #include "ConcreteStrategyB.h" 4 | #include "ConcreteStrategyC.h" 5 | 6 | /* kreiramo kontekst sa odredjenom konkretnom strategijom A 7 | zatim uputimo zahtev da nam razresi neki problem, 8 | zatim promenimo strategiju i opet uputimo zahtev 9 | tok izvrsavanja mozemo pratiti na ekranu */ 10 | int main() { 11 | Context context(new ConcreteStrategyA()); 12 | context.ContextInterface(); 13 | 14 | context.SetStrategy(new ConcreteStrategyB()); 15 | context.ContextInterface(); 16 | 17 | context.SetStrategy(new ConcreteStrategyC()); 18 | context.ContextInterface(); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /12_strategytemplate/strategy/ex1/Strategy/bin/Debug/Strategy.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/12_strategytemplate/strategy/ex1/Strategy/bin/Debug/Strategy.exe -------------------------------------------------------------------------------- /12_strategytemplate/strategy/ex1/Strategy/obj/Debug/StrategyMain.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/12_strategytemplate/strategy/ex1/Strategy/obj/Debug/StrategyMain.o -------------------------------------------------------------------------------- /12_strategytemplate/template_method/ex1/TemplateMethod/AbstractClass.h: -------------------------------------------------------------------------------- 1 | #ifndef __ABSTRACTCLASS_H 2 | #define __ABSTRACTCLASS_H 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | /* apstraktna klasa koja poseduje neki sablonski metod / template method 9 | u nasem slucaju to je TemplateMethod 10 | u apstraktnoj klasi data je implementacija ovog metoda koristeci virtuelne metode 11 | PrimitiveOperation1/2 koji nisu implementirani, 12 | izvedene klase od AbstractClass treba da daju implementacije metoda PrimitiveOperation1/2 13 | ali ne menjajuci korake u TemplateMethod, 14 | na taj nacin konkretne klase mogu da daju odgovarajuce izmene ovih primitivnih operacija, 15 | razlika u odnosu na strategija obrazac je sto kod sablon metoda mozemo da vrsimo 16 | izmenu finijih delova implementacije, dok kod strategije kod konkretnih strategija 17 | moramo da dajemo implementacije celog metoda */ 18 | class AbstractClass { 19 | public: 20 | virtual void TemplateMethod() { 21 | cout << "AbstractClass.TemplateMethod()" << endl; 22 | PrimitiveOperation1(); 23 | PrimitiveOperation2(); 24 | } 25 | virtual void PrimitiveOperation1() = 0; 26 | virtual void PrimitiveOperation2() = 0; 27 | }; 28 | #endif // __ABSTRACTCLASS_H 29 | -------------------------------------------------------------------------------- /12_strategytemplate/template_method/ex1/TemplateMethod/ConcreteClass.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETECLASS_H 2 | #define __CONCRETECLASS_H 3 | 4 | #include 5 | #include "AbstractClass.h" 6 | 7 | using namespace std; 8 | 9 | /* konkretna klasa koja implementira operacije koriscenje u sablon metodu / template method 10 | iz apstraktne klase koju nasledjuje, 11 | taj metod se ne menja u konkretnoj klasi */ 12 | class ConcreteClass: public AbstractClass { 13 | public: 14 | virtual void PrimitiveOperation1() { 15 | cout << "ConcreteClass.PrimitiveOperation1()" << endl; 16 | } 17 | virtual void PrimitiveOperation2() { 18 | cout << "ConcreteClass.PrimitiveOperation2()" << endl; 19 | } 20 | }; 21 | #endif // __CONCRETECLASS_H 22 | -------------------------------------------------------------------------------- /12_strategytemplate/template_method/ex1/TemplateMethod/TemplateMethod.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 44 | 45 | -------------------------------------------------------------------------------- /12_strategytemplate/template_method/ex1/TemplateMethod/TemplateMethod.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ConcreteClass.h" 3 | 4 | /* kreiramo instancu konkretne klase i pozovemo sablon metod/ TemplateMethod */ 5 | int main() { 6 | unique_ptr c(new ConcreteClass()); 7 | c->TemplateMethod(); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /12_strategytemplate/template_method/ex1/TemplateMethod/TemplateMethod.depend: -------------------------------------------------------------------------------- 1 | # depslib dependency file v1.0 2 | 1452784060 source:c:\users\sidejan\google drive\oop2\vezbe_sredjene\12_strategy_template\template_method\ex1\templatemethod\templatemethod.cpp 3 | 4 | "ConcreteClass.h" 5 | 6 | 1452784090 c:\users\sidejan\google drive\oop2\vezbe_sredjene\12_strategy_template\template_method\ex1\templatemethod\concreteclass.h 7 | 8 | "AbstractClass.h" 9 | 10 | 1452784093 c:\users\sidejan\google drive\oop2\vezbe_sredjene\12_strategy_template\template_method\ex1\templatemethod\abstractclass.h 11 | 12 | 13 | 1452784062 source:d:\___predavanja\oop2\lekcije\12_strategy_template\template_method\ex1\templatemethod\templatemethod.cpp 14 | 15 | "ConcreteClass.h" 16 | 17 | 1452784092 d:\___predavanja\oop2\lekcije\12_strategy_template\template_method\ex1\templatemethod\concreteclass.h 18 | 19 | "AbstractClass.h" 20 | 21 | 1452784094 d:\___predavanja\oop2\lekcije\12_strategy_template\template_method\ex1\templatemethod\abstractclass.h 22 | 23 | 24 | -------------------------------------------------------------------------------- /12_strategytemplate/template_method/ex1/TemplateMethod/TemplateMethod.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /12_strategytemplate/template_method/ex1/TemplateMethod/bin/Debug/TemplateMethod.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/12_strategytemplate/template_method/ex1/TemplateMethod/bin/Debug/TemplateMethod.exe -------------------------------------------------------------------------------- /12_strategytemplate/template_method/ex1/TemplateMethod/obj/Debug/TemplateMethod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/12_strategytemplate/template_method/ex1/TemplateMethod/obj/Debug/TemplateMethod.o -------------------------------------------------------------------------------- /13_visitor/ex1/Visitor/ConcreteElementA.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETEELEMENTA_H 2 | #define __CONCRETEELEMENTA_H 3 | 4 | #include 5 | using namespace std; 6 | 7 | #include "Element.h" 8 | 9 | /* konkretan element A 10 | nove operacije ovoj klasi dodane su koriscenjem konkretnih posetilaca 11 | mora da implementira nasledjeni metod Accept gde na prosledjenom 12 | posetiocu pozove metod koji tretira instance ove konkretne klase 13 | preko metoda Accept se prosledjuje ta nova operacija koja ce biti izvrsena 14 | na konkretnim elementima, a koja je predstavljena u obliku prosledjenog 15 | konkretnog posetioca/visitora */ 16 | class ConcreteElementA: public Element { 17 | public: 18 | virtual ~ConcreteElementA() {} 19 | virtual void Accept(Visitor& visitor) { 20 | cout << "ConcreteElementA.Accept()" << endl; 21 | visitor.VisitConcreteElementA(*this); 22 | } 23 | virtual void OperationA() { 24 | cout << "ConcreteElementA.OperationA()" << endl; 25 | } 26 | }; 27 | 28 | #endif // __CONCRETEELEMENTA_H 29 | -------------------------------------------------------------------------------- /13_visitor/ex1/Visitor/ConcreteElementB.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETEELEMENTB_H 2 | #define __CONCRETEELEMENTB_H 3 | 4 | #include 5 | using namespace std; 6 | 7 | #include "Element.h" 8 | 9 | /* konkretan element B 10 | nove operacije ovoj klasi dodane su koriscenjem konkretnih posetilaca 11 | mora da implementira nasledjeni metod Accept gde na prosledjenom 12 | posetiocu pozove metod koji tretira instance ove konkretne klase 13 | preko metoda Accept se prosledjuje ta nova operacija koja ce biti izvrsena 14 | na konkretnim elementima, a koja je predstavljena u obliku prosledjenog 15 | konkretnog posetioca/visitora */ 16 | class ConcreteElementB: public Element { 17 | public: 18 | virtual ~ConcreteElementB() {} 19 | virtual void Accept(Visitor& visitor) { 20 | cout << "ConcreteElementB.Accept()" << endl; 21 | visitor.VisitConcreteElementB(*this); 22 | } 23 | virtual void OperationB() { 24 | cout << "ConcreteElementB.OperationB()" << endl; 25 | } 26 | }; 27 | 28 | #endif // __CONCRETEELEMENTB_H 29 | 30 | -------------------------------------------------------------------------------- /13_visitor/ex1/Visitor/ConcreteVisitor2.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETEVISITOR2_H 2 | #define __CONCRETEVISITOR2_H 3 | 4 | #include 5 | using namespace std; 6 | 7 | #include "ConcreteElementA.h" 8 | #include "ConcreteElementB.h" 9 | 10 | /* konkretan posetioc / concrete visitor 11 | za razliku od konkretnog posetioca 1 ovde se pozivaju neke 12 | operacije na instancama konkretnih klasa */ 13 | class ConcreteVisitor2: public Visitor { 14 | public: 15 | virtual ~ConcreteVisitor2() {} 16 | virtual void VisitConcreteElementA(ConcreteElementA& element) { 17 | cout << "ConcreteVisitor2.VisitConcreteElementA()" << endl; 18 | element.OperationA(); 19 | } 20 | virtual void VisitConcreteElementB(ConcreteElementB& element) { 21 | cout << "ConcreteVisitor2.VisitConcreteElementB()" << endl; 22 | element.OperationB(); 23 | } 24 | }; 25 | 26 | #endif // __CONCRETEVISITOR1_H 27 | -------------------------------------------------------------------------------- /13_visitor/ex1/Visitor/Element.h: -------------------------------------------------------------------------------- 1 | #ifndef __ELEMENT_H 2 | #define __ELEMENT_H 3 | 4 | #include "Visitor.h" 5 | 6 | /* apstraktni element 7 | nudi metod Accept koji ce da nasledjuju svi konkretni elementi 8 | a putem kojeg se prosledjuju posetioci */ 9 | class Element { 10 | public: 11 | virtual ~Element() {} 12 | virtual void Accept(Visitor& visitor) = 0; 13 | }; 14 | 15 | #endif // __ELEMENT_H 16 | -------------------------------------------------------------------------------- /13_visitor/ex1/Visitor/ObjectStructure.h: -------------------------------------------------------------------------------- 1 | #ifndef __OBJECTSTRUCTURE_H 2 | #define __OBJECTSTRUCTURE_H 3 | 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | #include "Element.h" 9 | 10 | /* objektna struktura / kompozicija 11 | moze da se sastoji od vise elemenata i moguce je 12 | pozvati novu operaciju na svim elementima strukture prosledjujuci 13 | posetioca/visitora putem metoda Accept koji kasnije 14 | prosledi posetioca svim elementima strukture */ 15 | class ObjectStructure { 16 | public: 17 | typedef vector::iterator ElementIter; 18 | 19 | virtual ~ObjectStructure() { 20 | for (ElementIter it = m_elements.begin(); it != m_elements.end(); ++it) { 21 | delete *it; 22 | } 23 | m_elements.clear(); 24 | } 25 | virtual void Accept(Visitor& visitor) { 26 | cout << "ObjectStructure.Accept()" << endl; 27 | for (ElementIter it = m_elements.begin(); it != m_elements.end(); ++it) { 28 | (*it)->Accept(visitor); 29 | } 30 | } 31 | virtual void AddElement(Element* element) { 32 | m_elements.push_back(element); 33 | } 34 | protected: 35 | vector m_elements; 36 | }; 37 | #endif // __OBJECTSTRUCTURE_H 38 | -------------------------------------------------------------------------------- /13_visitor/ex1/Visitor/Visitor.h: -------------------------------------------------------------------------------- 1 | #ifndef __VISITOR_H 2 | #define __VISITOR_H 3 | 4 | class ConcreteElementA; 5 | class ConcreteElementB; 6 | 7 | /* apstraktni posetilac / visitor 8 | nudi virtuelne metode da "poseti" konkretne elemente A i B, 9 | u sustini ti metodi, implementirani kasnije u konkrentim posetiocima, 10 | ne vrse nikakvo "posecivanje" tih konkretnih elemenata, vec im 11 | daju nove operacije, 12 | umesto da nove operacije budu dodane u hijerarhije elemenata koje nasledjuju 13 | apstraktnu klasu element i implementirane u svakoj nasledjenoj klasi 14 | mi nove operacije toj hijerarhiji mozemo ovako spolja/eksterno da dodelimo 15 | koristeci obrazac posetioc/visitor 16 | svaka ta nova operacije je koncepcijski objedinjena u jednom konkretnom posetiocu 17 | ovaj obrazac je koristan kada je hijerarhija objekata/elemenata stabila i nema 18 | prosirivanja a imamo slucaj da se cesto dodaju nove operacije 19 | kada se dodaje novi element u hijerarhiju klasa onda svi posetioci moraju da 20 | implementiraju novi metod za taj prirodani konkretan element */ 21 | class Visitor { 22 | public: 23 | virtual ~Visitor() {} 24 | virtual void VisitConcreteElementA(ConcreteElementA& element) = 0; 25 | virtual void VisitConcreteElementB(ConcreteElementB& element) = 0; 26 | }; 27 | 28 | #endif // __VISITOR_H 29 | -------------------------------------------------------------------------------- /13_visitor/ex1/Visitor/VisitorMain.cpp: -------------------------------------------------------------------------------- 1 | #include "ConcreteVisitor1.h" 2 | #include "ConcreteVisitor2.h" 3 | #include "ObjectStructure.h" 4 | 5 | /* kreiramo strukturu elemenata, dodamo joj dva konkretna elementa, 6 | kreiramo dva razlicita konkretna posetioca i prosledimo ih strukturi 7 | na ekranu ce biti odstampan tok izvrsavanja */ 8 | int main() { 9 | ObjectStructure structure; 10 | structure.AddElement(new ConcreteElementA()); 11 | structure.AddElement(new ConcreteElementB()); 12 | 13 | Visitor* v1 = new ConcreteVisitor1(); 14 | Visitor* v2 = new ConcreteVisitor2(); 15 | 16 | structure.Accept(*v1); 17 | structure.Accept(*v2); 18 | 19 | delete v1; 20 | delete v2; 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /1_streams/imenik1.txt: -------------------------------------------------------------------------------- 1 | ime1 prezime1 , 0123/12312312-12312 2 | ime2 prezime1 , 0123/12312312-12312 ime5 prezime1 , 0123/12312312-12312 ime7 prezime1 , 0123/12312312-12312 3 | ime3 prezime1 , 0123/12312312-12312 ime6 prezime1 , 0123/12312312-12312 4 | ime4 prezime1 , 0123/12312312-12312 5 | -------------------------------------------------------------------------------- /1_streams/imenik2.txt: -------------------------------------------------------------------------------- 1 | bime1 prezime1 , 0123/12312312-12312 2 | bime2 prezime1 , 0123/12312312-12312 bime5 prezime1 , 0123/12312312-12312 bime7 prezime1 , 0123/12312312-12312 3 | bime3 prezime1 , 0123/12312312-12312 bime6 prezime1 , 0123/12312312-12312 4 | bime4 prezime1 , 0123/12312312-12312 5 | -------------------------------------------------------------------------------- /1_streams/imenik3.txt: -------------------------------------------------------------------------------- 1 | aime1 prezime1 , 0123/12312312-12312 2 | aime2 prezime1 , 0123/12312312-12312 aime5 prezime1 , 0123/12312312-12312 aime7 prezime1 , 0123/12312312-12312 3 | aime3 prezime1 , 0123/12312312-12312 aime6 prezime1 , 0123/12312312-12312 4 | aime4 prezime1 , 0123/12312312-12312 5 | -------------------------------------------------------------------------------- /1_streams/matrica.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UNOS: dimenzije n, m 3 | UCITAJE SE MATRICA n x m 4 | U IZLAZU SE STAMPA MATRICA GDE SVAKO POLJE MATRICE SE STAMPA NA 3 DECIMALE SA 8 MESTA ZA STAMPU 5 | PRIMER 6 | ULAZ 7 | 2 2 1.234567 0.123456 0.234567 5.1234567 8 | IZLAZ 9 | 1.234 0.123 10 | 0.234 5.123 11 | 12 | cout << fixed << setw(8) << setprecision(3) 13 | U DATOM RESENJU KORISTI SE I ZNAK '.' ZA POPUNJAVANJE PRAZNINA 14 | */ 15 | 16 | #include 17 | #include 18 | 19 | using namespace std; 20 | 21 | int main() { 22 | int nr, nc; 23 | cin >> nr >> nc; 24 | float a [nr] [nc]; 25 | for (int r = 0; r < nr; r ++) { 26 | for (int c = 0; c < nc; c ++) cin >> a [r] [c]; 27 | } 28 | for (int r = 0; r < nr; r ++) { 29 | for (int c = 0; c < nc; c ++) { 30 | cout.fill('.'); 31 | cout << fixed << setw(8) << setprecision(3) << a [r] [c]; 32 | } 33 | cout << endl; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /1_streams/stringstream_primer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | IZ FILE-A SE UCITAVAJU REDOVI KARAKTERA 3 | NA PRIMER (ulaz.txt) 4 | 1 , 2 , 33, 55, 66 5 | , 53 34, 43 53, 66, 22 6 | 7 | UCITAVATI U NEKU STRINGU SA GETLINE SA ',' DO EOF 8 | A ONDA KORISTECI ISTRINGSTREAM IZ TOGA IZVUCI BROJEVE 9 | I UPISATI IH U OSTRINGSTREAM 10 | I NA KRAJU STAMPATI SADRZAJ OSTRINGSTREAM OBJEKTA 11 | U FILE izlaz.txt 12 | 13 | NAPOMENA: ovo nije uradjeno na bas optimalan nacin vec vise sluzi kao 14 | primer primene naucenog 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | int main() { 26 | ifstream in("ulaz.txt"); 27 | if (!in.is_open()) { 28 | cout << "Greska prilikom otvaranja file-a ulaz.txt" << endl; 29 | exit(1); 30 | } 31 | string line, all = ""; 32 | while (!in.eof()) { 33 | getline(in, line, ','); 34 | all += " " + line; 35 | } 36 | istringstream ins(all); 37 | ostringstream outs; 38 | int tmp; 39 | while (ins >> tmp) { 40 | outs << tmp << ' '; 41 | } 42 | cout << "Pisem rezultate u izlazni file izlaz.txt" << endl; 43 | ofstream out("izlaz.txt"); 44 | out << outs.str() << endl; 45 | out.close(); 46 | in.close(); 47 | } 48 | -------------------------------------------------------------------------------- /1_streams/test1.txt: -------------------------------------------------------------------------------- 1 | 9 23094 2390 209 40293 2 | 2 394 203 023 023 3 | 2 309 4023 023 042 4 | 2 304 230 5 | -------------------------------------------------------------------------------- /1_streams/test2.txt: -------------------------------------------------------------------------------- 1 | 9 23094 2390 209 40293 2 | 2 394 203 023 023 3 | 2 309 4023 023 042 4 | 2 304 230 5 | -------------------------------------------------------------------------------- /1_streams/ulaz.txt: -------------------------------------------------------------------------------- 1 | 1 , 2 , 33, 55, 66 2 | , 53 34, 43 53, 66, 22 3 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/AbstractFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef __ABSTRACT_FACTORY_H 2 | #define __ABSTRACT_FACTORY_H 3 | 4 | #include "AbstractProductA.h" 5 | #include "AbstractProductB.h" 6 | 7 | class AbstractFactory { 8 | public: 9 | virtual AbstractProductA* createProductA() = 0; 10 | virtual AbstractProductB* createProductB() = 0; 11 | }; 12 | #endif // __ABSTRACT_FACTORY_H 13 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/AbstractFactoryMain.cpp: -------------------------------------------------------------------------------- 1 | #include "ConcreteFactory1.h" 2 | #include "ConcreteFactory2.h" 3 | 4 | int main() { 5 | AbstractFactory* factory1 = new ConcreteFactory1(); 6 | AbstractProductA* productA1 = factory1->createProductA(); 7 | AbstractProductB* productB1 = factory1->createProductB(); 8 | productA1->use(); 9 | productB1->use(); 10 | 11 | AbstractFactory* factory2 = new ConcreteFactory2(); 12 | AbstractProductA* productA2 = factory2->createProductA(); 13 | AbstractProductB* productB2 = factory2->createProductB(); 14 | productA2->use(); 15 | productB2->use(); 16 | 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/AbstractProductA.h: -------------------------------------------------------------------------------- 1 | #ifndef __ABSTRACT_PRODUCTA_H 2 | #define __ABSTRACT_PRODUCTA_H 3 | 4 | class AbstractProductA { 5 | public: 6 | virtual void use() = 0; 7 | }; 8 | #endif // __ABSTRACT_PRODUCTA_H 9 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/AbstractProductB.h: -------------------------------------------------------------------------------- 1 | #ifndef __ABSTRACT_PRODUCTB_H 2 | #define __ABSTRACT_PRODUCTB_H 3 | 4 | class AbstractProductB { 5 | public: 6 | virtual void use() = 0; 7 | }; 8 | #endif // __ABSTRACT_PRODUCTB_H 9 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/ConcreteFactory1.cpp: -------------------------------------------------------------------------------- 1 | #include "ConcreteFactory1.h" 2 | #include "ConcreteProductA1.h" 3 | #include "ConcreteProductB1.h" 4 | 5 | AbstractProductA* ConcreteFactory1::createProductA() { 6 | return new ConcreteProductA1(); 7 | } 8 | 9 | AbstractProductB* ConcreteFactory1::createProductB() { 10 | return new ConcreteProductB1(); 11 | } 12 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/ConcreteFactory1.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETE_FACTORY1_H 2 | #define __CONCRETE_FACTORY1_H 3 | 4 | #include "AbstractFactory.h" 5 | 6 | class ConcreteFactory1: public AbstractFactory { 7 | public: 8 | AbstractProductA* createProductA(); 9 | AbstractProductB* createProductB(); 10 | }; 11 | #endif // __CONCRETE_FACTORY1_H 12 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/ConcreteFactory2.cpp: -------------------------------------------------------------------------------- 1 | #include "ConcreteFactory2.h" 2 | #include "ConcreteProductA2.h" 3 | #include "ConcreteProductB2.h" 4 | 5 | AbstractProductA* ConcreteFactory2::createProductA() { 6 | return new ConcreteProductA2(); 7 | } 8 | 9 | AbstractProductB* ConcreteFactory2::createProductB() { 10 | return new ConcreteProductB2(); 11 | } 12 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/ConcreteFactory2.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETE_FACTORY2_H 2 | #define __CONCRETE_FACTORY2_H 3 | 4 | #include "AbstractFactory.h" 5 | 6 | class ConcreteFactory2: public AbstractFactory { 7 | public: 8 | AbstractProductA* createProductA(); 9 | AbstractProductB* createProductB(); 10 | }; 11 | #endif // __CONCRETE_FACTORY2_H 12 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/ConcreteProductA1.cpp: -------------------------------------------------------------------------------- 1 | #include "ConcreteProductA1.h" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | void ConcreteProductA1::use() { 8 | cout << "Koristim proizvod A1" << endl; 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/ConcreteProductA1.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETE_PRODUCTA1_H 2 | #define __CONCRETE_PRODUCTA1_H 3 | 4 | #include "AbstractProductA.h" 5 | 6 | class ConcreteProductA1: public AbstractProductA { 7 | public: 8 | void use(); 9 | }; 10 | #endif // __CONCRETE_PRODUCTA1_H 11 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/ConcreteProductA2.cpp: -------------------------------------------------------------------------------- 1 | #include "ConcreteProductA2.h" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | void ConcreteProductA2::use() { 8 | cout << "Koristim proizvod A2" << endl; 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/ConcreteProductA2.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETE_PRODUCTA2_H 2 | #define __CONCRETE_PRODUCTA2_H 3 | 4 | #include "AbstractProductA.h" 5 | 6 | class ConcreteProductA2: public AbstractProductA { 7 | public: 8 | void use(); 9 | }; 10 | #endif // __CONCRETE_PRODUCTA2_H 11 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/ConcreteProductB1.cpp: -------------------------------------------------------------------------------- 1 | #include "ConcreteProductB1.h" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | void ConcreteProductB1::use() { 8 | cout << "Koristim proizvod B1" << endl; 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/ConcreteProductB1.h: -------------------------------------------------------------------------------- 1 | #include "AbstractProductB.h" 2 | 3 | class ConcreteProductB1: public AbstractProductB { 4 | public: 5 | void use(); 6 | }; 7 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/ConcreteProductB2.cpp: -------------------------------------------------------------------------------- 1 | #include "ConcreteProductB2.h" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | void ConcreteProductB2::use() { 8 | cout << "Koristim proizvod B2" << endl; 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/ConcreteProductB2.h: -------------------------------------------------------------------------------- 1 | #include "AbstractProductB.h" 2 | 3 | class ConcreteProductB2: public AbstractProductB { 4 | public: 5 | void use(); 6 | }; 7 | -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/bin/Debug/AbstractFactory.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/2_abstractbuilder/abstract_factory/ex1/AbstractFactory/bin/Debug/AbstractFactory.exe -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/obj/Debug/AbstractFactoryMain.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/2_abstractbuilder/abstract_factory/ex1/AbstractFactory/obj/Debug/AbstractFactoryMain.o -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/obj/Debug/ConcreteFactory1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/2_abstractbuilder/abstract_factory/ex1/AbstractFactory/obj/Debug/ConcreteFactory1.o -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/obj/Debug/ConcreteFactory2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/2_abstractbuilder/abstract_factory/ex1/AbstractFactory/obj/Debug/ConcreteFactory2.o -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/obj/Debug/ConcreteProductA1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/2_abstractbuilder/abstract_factory/ex1/AbstractFactory/obj/Debug/ConcreteProductA1.o -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/obj/Debug/ConcreteProductA2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/2_abstractbuilder/abstract_factory/ex1/AbstractFactory/obj/Debug/ConcreteProductA2.o -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/obj/Debug/ConcreteProductB1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/2_abstractbuilder/abstract_factory/ex1/AbstractFactory/obj/Debug/ConcreteProductB1.o -------------------------------------------------------------------------------- /2_abstractbuilder/abstract_factory/ex1/AbstractFactory/obj/Debug/ConcreteProductB2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/2_abstractbuilder/abstract_factory/ex1/AbstractFactory/obj/Debug/ConcreteProductB2.o -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex1/Builder/Builder.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 45 | 46 | -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex1/Builder/Builder.depend: -------------------------------------------------------------------------------- 1 | # depslib dependency file v1.0 2 | 1447247379 source:e:\predavanja\oop2\postavljenje_lekcije\2_abstract_builder\builder\ex0\builder\buildermain.cpp 3 | "Director.h" 4 | "ConcreteBuilder.h" 5 | 6 | 1447247339 e:\predavanja\oop2\postavljenje_lekcije\2_abstract_builder\builder\ex0\builder\director.h 7 | "Builder.h" 8 | "Product.h" 9 | 10 | 1447247279 e:\predavanja\oop2\postavljenje_lekcije\2_abstract_builder\builder\ex0\builder\builder.h 11 | "Product.h" 12 | 13 | 1447247321 e:\predavanja\oop2\postavljenje_lekcije\2_abstract_builder\builder\ex0\builder\product.h 14 | 15 | 16 | 1447247414 e:\predavanja\oop2\postavljenje_lekcije\2_abstract_builder\builder\ex0\builder\concretebuilder.h 17 | 18 | -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex1/Builder/Builder.h: -------------------------------------------------------------------------------- 1 | #ifndef __BUILDER_H 2 | #define __BUILDER_H 3 | 4 | #include "Product.h" 5 | 6 | /* apstraktan graditelj / abstract builder */ 7 | class Builder { 8 | public: 9 | virtual void buildPart() = 0; 10 | virtual Product* getProduct() = 0; 11 | }; 12 | #endif // __BUILDER_H 13 | -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex1/Builder/Builder.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex1/Builder/BuilderMain.cpp: -------------------------------------------------------------------------------- 1 | #include "Director.h" 2 | #include "ConcreteBuilder.h" 3 | 4 | /* koriscenje sablona, kreiramo konkretnog graditelja (concrete builder), prosledimo ga upravljacu (director) 5 | pomocu kojeg kreiramo prouzvod */ 6 | int main() { 7 | Builder *builder = new ConcreteBuilder(); 8 | Director *director = new Director(builder); 9 | Product *product = director->construct(); 10 | product->use(); 11 | 12 | delete builder; 13 | delete director; 14 | delete product; 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex1/Builder/ConcreteBuilder.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETE_BUILDER_H 2 | #define __CONCRETE_BUILDER_H 3 | 4 | /* konkretan graditelj / concrete builder, ima mogucnost da konstruise proizvod deo po deo */ 5 | class ConcreteBuilder: public Builder { 6 | private: 7 | Product *product; 8 | public: 9 | ConcreteBuilder() { 10 | product = new Product(); 11 | } 12 | void buildPart() { 13 | product->setPart(5); 14 | } 15 | Product *getProduct() { 16 | return product; 17 | } 18 | }; 19 | #endif // __CONCRETE_BUILDER_H 20 | 21 | -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex1/Builder/Director.h: -------------------------------------------------------------------------------- 1 | #ifndef __DIRECTOR_H 2 | #define __DIRECTOR_H 3 | 4 | #include "Builder.h" 5 | #include "Product.h" 6 | 7 | /* upravljac / director koji koristi graditelja (builder) da napravi proizvod postavljajuci deo po deo */ 8 | class Director { 9 | private: 10 | Builder *builder; 11 | public: 12 | Director(Builder *builder) { 13 | this->builder = builder; 14 | } 15 | Product* construct() { 16 | builder->buildPart(); 17 | return builder->getProduct(); 18 | } 19 | }; 20 | 21 | #endif // __DIRECTOR_H 22 | -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex1/Builder/Product.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRODUCT_H 2 | #define __PRODUCT_H 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | /* konkretan proizvod */ 9 | class Product { 10 | private: 11 | int part; 12 | public: 13 | void setPart(int part) { this->part = part; } 14 | void use() { cout << "Koristim Proizvod koji ima deo #" << part << endl ; } 15 | }; 16 | #endif // __PRODUCT_H 17 | -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex1/Builder/bin/Debug/Builder.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/2_abstractbuilder/builder/ex1/Builder/bin/Debug/Builder.exe -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex1/Builder/obj/Debug/BuilderMain.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/2_abstractbuilder/builder/ex1/Builder/obj/Debug/BuilderMain.o -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex2/GraditeljProizvoda/Graditelj.h: -------------------------------------------------------------------------------- 1 | #ifndef __GRADITELJ_H 2 | #define __GRADITELJ_H 3 | 4 | #include "Proizvod.h" 5 | 6 | /* graditelj / builder proizvoda */ 7 | class Graditelj { 8 | private: 9 | Proizvod proizvod; 10 | public: 11 | static const int DEFAULT_INT_SVOJSTVO = 5; 12 | static const float DEFAULT_FLOAT_SVOJSTVO = 1.4141f; 13 | static const char DEFAULT_CHAR_SVOJSTVO = 'X'; 14 | 15 | Graditelj() { 16 | proizvod = Proizvod(DEFAULT_INT_SVOJSTVO,DEFAULT_FLOAT_SVOJSTVO,DEFAULT_CHAR_SVOJSTVO); 17 | } 18 | 19 | Graditelj& postaviIntSvojstvo(const int intSvojstvo) { 20 | proizvod.postaviIntSvojstvo(intSvojstvo); 21 | return *this; 22 | } 23 | Graditelj& postaviFloatSvojstvo(const float floatSvojstvo) { 24 | proizvod.postaviFloatSvojstvo(floatSvojstvo); 25 | return *this; 26 | } 27 | Graditelj& postaviCharSvojstvo(const char charSvojstvo) { 28 | proizvod.postaviCharSvojstvo(charSvojstvo); 29 | return *this; 30 | } 31 | 32 | Graditelj& postaviProizvod() { 33 | proizvod = Proizvod(11, 0.12345, 'Y'); 34 | return *this; 35 | } 36 | 37 | Proizvod napraviProizvod() { 38 | return proizvod; 39 | } 40 | }; 41 | 42 | #endif // __GRADITELJ_H 43 | -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex2/GraditeljProizvoda/GraditeljProizvoda.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 44 | 45 | -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex2/GraditeljProizvoda/GraditeljProizvoda.depend: -------------------------------------------------------------------------------- 1 | # depslib dependency file v1.0 2 | 1447254723 source:e:\predavanja\oop2\postavljenje_lekcije\2_abstract_builder\builder\ex1\graditeljproizvoda\proizvod.cpp 3 | "Proizvod.h" 4 | 5 | 6 | 1447255290 e:\predavanja\oop2\postavljenje_lekcije\2_abstract_builder\builder\ex1\graditeljproizvoda\proizvod.h 7 | 8 | 1447254924 source:e:\predavanja\oop2\postavljenje_lekcije\2_abstract_builder\builder\ex1\graditeljproizvoda\proizvodmain.cpp 9 | "Proizvod.h" 10 | "Graditelj.h" 11 | 12 | 1447255366 e:\predavanja\oop2\postavljenje_lekcije\2_abstract_builder\builder\ex1\graditeljproizvoda\graditelj.h 13 | "Proizvod.h" 14 | 15 | -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex2/GraditeljProizvoda/GraditeljProizvoda.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex2/GraditeljProizvoda/Proizvod.cpp: -------------------------------------------------------------------------------- 1 | #include "Proizvod.h" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | void Proizvod::prikazi() { 8 | cout << "" << endl; 9 | cout << "\tintSvojstvo: " << this->intSvojstvo << endl; 10 | cout << "\tfloatSvojstvo: " << this->floatSvojstvo << endl; 11 | cout << "\tcharSvojstvo: " << this->charSvojstvo << endl; 12 | } 13 | 14 | void Proizvod::uradiNesto() { 15 | cout << "Proizvod > Radim nesto..." << endl; 16 | } 17 | 18 | void Proizvod::uradiNestoDrugo() { 19 | cout << "Proizvod > Radim nesto drugo..." << endl; 20 | } 21 | -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex2/GraditeljProizvoda/Proizvod.h: -------------------------------------------------------------------------------- 1 | #ifndef __PROIZVOD_H 2 | #define __PROIZVOD_H 3 | 4 | /* konkretan proizvod */ 5 | class Proizvod { 6 | private: 7 | /* interna svojstva proizvoda, npr. delovi proizvoda */ 8 | int intSvojstvo; 9 | float floatSvojstvo; 10 | char charSvojstvo; 11 | public: 12 | /* konstruktor proizvoda */ 13 | Proizvod() {} 14 | Proizvod(const int intSvojstvo, const float floatSvojstvo, const char charSvojstvo) : 15 | intSvojstvo(intSvojstvo), 16 | floatSvojstvo(floatSvojstvo), 17 | charSvojstvo(charSvojstvo) {} 18 | void postaviIntSvojstvo(int intSvojstvo) { this->intSvojstvo = intSvojstvo; } 19 | void postaviFloatSvojstvo(float floatSvojstvo) { this->floatSvojstvo = floatSvojstvo; } 20 | void postaviCharSvojstvo(char charSvojstvo) { this->charSvojstvo = charSvojstvo; } 21 | void prikazi(); 22 | void uradiNesto(); 23 | void uradiNestoDrugo(); 24 | }; 25 | 26 | 27 | 28 | #endif // __PROIZVOD_H 29 | -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex2/GraditeljProizvoda/ProizvodMain.cpp: -------------------------------------------------------------------------------- 1 | #include "Proizvod.h" 2 | #include "Graditelj.h" 3 | 4 | /* koriscenje graditelja */ 5 | int main(){ 6 | /* kombinovano koriscenje za postavljanje svojstava */ 7 | Proizvod proizvod1 = Graditelj().postaviIntSvojstvo(2) 8 | .postaviFloatSvojstvo(0.5f) 9 | .postaviCharSvojstvo('x') 10 | .napraviProizvod(); 11 | proizvod1.prikazi(); 12 | 13 | /* drugi vid koriscenja, napravimo jedan proizvod, posle napravimo izmene putem graditelja i vratimo 14 | drugi, izmenjeni proizvod */ 15 | Graditelj graditelj; 16 | graditelj.postaviProizvod(); 17 | Proizvod proizvod2 = graditelj.napraviProizvod(); 18 | graditelj.postaviCharSvojstvo('*'); 19 | Proizvod proizvod3 = graditelj.napraviProizvod(); 20 | proizvod2.prikazi(); 21 | proizvod3.prikazi(); 22 | } 23 | -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex2/GraditeljProizvoda/bin/Debug/GraditeljProizvoda.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/2_abstractbuilder/builder/ex2/GraditeljProizvoda/bin/Debug/GraditeljProizvoda.exe -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex2/GraditeljProizvoda/obj/Debug/Proizvod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/2_abstractbuilder/builder/ex2/GraditeljProizvoda/obj/Debug/Proizvod.o -------------------------------------------------------------------------------- /2_abstractbuilder/builder/ex2/GraditeljProizvoda/obj/Debug/ProizvodMain.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/2_abstractbuilder/builder/ex2/GraditeljProizvoda/obj/Debug/ProizvodMain.o -------------------------------------------------------------------------------- /3_factorymethodprototype/factory_method/ex1/FactoryMethod/ConcreteCreator.cpp: -------------------------------------------------------------------------------- 1 | #include "ConcreteCreator.h" 2 | #include "ConcreteProduct.h" 3 | 4 | Product* ConcreteCreator::factoryMethod() { 5 | return new ConcreteProduct(); 6 | } 7 | -------------------------------------------------------------------------------- /3_factorymethodprototype/factory_method/ex1/FactoryMethod/ConcreteCreator.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETE_CREATOR_H 2 | #define __CONCRETE_CREATOR_H 3 | 4 | #include "Creator.h" 5 | 6 | /* konkretan kreator/stvaralac koji ima fabricki metod za stvaranje konkretnog proizvoda */ 7 | class ConcreteCreator: public Creator { 8 | public: 9 | Product* factoryMethod(); 10 | }; 11 | #endif // __CONCRETE_CREATOR_H 12 | -------------------------------------------------------------------------------- /3_factorymethodprototype/factory_method/ex1/FactoryMethod/ConcreteProduct.cpp: -------------------------------------------------------------------------------- 1 | #include "ConcreteProduct.h" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | void ConcreteProduct::use() { 8 | cout << "Koristim konkretan proizvod" << endl; 9 | } 10 | -------------------------------------------------------------------------------- /3_factorymethodprototype/factory_method/ex1/FactoryMethod/ConcreteProduct.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETE_PRODUCT_H 2 | #define __CONCRETE_PRODUCT_H 3 | 4 | #include "Product.h" 5 | 6 | /* konkretan proizvod */ 7 | class ConcreteProduct: public Product { 8 | public: 9 | void use(); 10 | }; 11 | #endif // __CONCRETE_PRODUCT_H 12 | -------------------------------------------------------------------------------- /3_factorymethodprototype/factory_method/ex1/FactoryMethod/Creator.h: -------------------------------------------------------------------------------- 1 | #ifndef __CREATOR_H 2 | #define __CREATOR_H 3 | 4 | #include "Product.h" 5 | 6 | /* apstraktni kreator/stvaralac koji u ovom slucaju ima samo jedan fabricki metod 7 | koji kreira proizvode */ 8 | class Creator { 9 | public: 10 | virtual Product* factoryMethod() = 0; 11 | }; 12 | #endif // __CREATOR_H 13 | -------------------------------------------------------------------------------- /3_factorymethodprototype/factory_method/ex1/FactoryMethod/FactoryMethodMain.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "ConcreteCreator.h" 3 | #include "Product.h" 4 | 5 | /* testiranje kreatora koji je dizajniran sledeci sablon fabricki metod, 6 | pravimo konkretnog kreatora i pomocu njegovog fabrickog metoda kreiramo konkretan 7 | proizvod */ 8 | int main() { 9 | Creator *creator = new ConcreteCreator(); 10 | Product *product = creator->factoryMethod(); 11 | product->use(); 12 | 13 | delete creator; 14 | delete product; 15 | } 16 | -------------------------------------------------------------------------------- /3_factorymethodprototype/factory_method/ex1/FactoryMethod/Product.h: -------------------------------------------------------------------------------- 1 | #ifndef __PRODUCT_H 2 | #define __PRODUCT_H 3 | 4 | /* apstraktan proizvod */ 5 | class Product { 6 | public: 7 | virtual void use() = 0; 8 | }; 9 | #endif // __PRODUCT_H 10 | -------------------------------------------------------------------------------- /3_factorymethodprototype/prototype/ex1/Prototype/ConcretePrototype1.cpp: -------------------------------------------------------------------------------- 1 | #include "ConcretePrototype1.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | Prototype* ConcretePrototype1::clone() { 7 | return new ConcretePrototype1(); 8 | } 9 | 10 | void ConcretePrototype1::use() { 11 | cout << "Koristim konkretan prototip 1" << endl; 12 | } 13 | -------------------------------------------------------------------------------- /3_factorymethodprototype/prototype/ex1/Prototype/ConcretePrototype1.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETE_PROTOTYPE1_H 2 | #define __CONCRETE_PROTOTYPE1_H 3 | 4 | #include "Prototype.h" 5 | 6 | /* konkretan prototip koji implementira metod za kloniranje */ 7 | class ConcretePrototype1: public Prototype { 8 | public: 9 | Prototype* clone(); 10 | void use(); 11 | }; 12 | #endif // __CONCRETE_PROTOTYPE1_H 13 | -------------------------------------------------------------------------------- /3_factorymethodprototype/prototype/ex1/Prototype/ConcretePrototype2.cpp: -------------------------------------------------------------------------------- 1 | #include "ConcretePrototype2.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | Prototype* ConcretePrototype2::clone() { 7 | return new ConcretePrototype2(); 8 | } 9 | 10 | void ConcretePrototype2::use() { 11 | cout << "Koristim konkretan prototip 2" << endl; 12 | } 13 | -------------------------------------------------------------------------------- /3_factorymethodprototype/prototype/ex1/Prototype/ConcretePrototype2.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETE_PROTOTYPE2_H 2 | #define __CONCRETE_PROTOTYPE2_H 3 | 4 | #include "Prototype.h" 5 | 6 | /* konkretan prototip koji implementira metod za kloniranje */ 7 | class ConcretePrototype2: public Prototype { 8 | public: 9 | Prototype* clone(); 10 | void use(); 11 | }; 12 | #endif // __CONCRETE_PROTOTYPE2_H 13 | -------------------------------------------------------------------------------- /3_factorymethodprototype/prototype/ex1/Prototype/Prototype.depend: -------------------------------------------------------------------------------- 1 | # depslib dependency file v1.0 2 | 1446736310 source:e:\predavanja\oop2\lekcije\3_factorymethod_prototype\ex1\factorymethod\prototype\concreteprototype1.cpp 3 | "ConcretePrototype1.h" 4 | 5 | 6 | 1446736295 e:\predavanja\oop2\lekcije\3_factorymethod_prototype\ex1\factorymethod\prototype\concreteprototype1.h 7 | "Prototype.h" 8 | 9 | 1446735895 e:\predavanja\oop2\lekcije\3_factorymethod_prototype\ex1\factorymethod\prototype\prototype.h 10 | 11 | 1446736310 source:e:\predavanja\oop2\postavljenje_lekcije\3_factorymethodprototype\prototype\ex1\prototype\concreteprototype1.cpp 12 | "ConcretePrototype1.h" 13 | 14 | 15 | 1447266399 e:\predavanja\oop2\postavljenje_lekcije\3_factorymethodprototype\prototype\ex1\prototype\concreteprototype1.h 16 | "Prototype.h" 17 | 18 | 1447266377 e:\predavanja\oop2\postavljenje_lekcije\3_factorymethodprototype\prototype\ex1\prototype\prototype.h 19 | 20 | 1446736331 source:e:\predavanja\oop2\postavljenje_lekcije\3_factorymethodprototype\prototype\ex1\prototype\concreteprototype2.cpp 21 | "ConcretePrototype2.h" 22 | 23 | 24 | 1447266393 e:\predavanja\oop2\postavljenje_lekcije\3_factorymethodprototype\prototype\ex1\prototype\concreteprototype2.h 25 | "Prototype.h" 26 | 27 | -------------------------------------------------------------------------------- /3_factorymethodprototype/prototype/ex1/Prototype/Prototype.h: -------------------------------------------------------------------------------- 1 | #ifndef __PROTOTYPE_H 2 | #define __PROTOTYPE_H 3 | 4 | /* apstraktan prototip koji ima metod za kloniranje objekta */ 5 | class Prototype { 6 | public: 7 | virtual Prototype* clone() = 0; 8 | virtual void use() = 0; 9 | }; 10 | #endif // __PROTOTYPE_H 11 | -------------------------------------------------------------------------------- /3_factorymethodprototype/prototype/ex1/Prototype/PrototypeMain.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ConcretePrototype1.h" 3 | #include "ConcretePrototype2.h" 4 | 5 | using namespace std; 6 | 7 | /* primer upotrebe sablona prototip, 8 | kreiramo dva konkretna proizvoda (direktnim pozivom njihovih konstruktora) koji ce da imaju 9 | tip apstraktnog proizvoda a zatim kreiramo njihove kopije pozivajuci metod za kloniranje, clone */ 10 | int main() { 11 | Prototype *prototype1 = new ConcretePrototype1(); 12 | Prototype *prototype2 = new ConcretePrototype2(); 13 | cout << "Koristim originalne prototipove: " << endl; 14 | prototype1->use(); 15 | prototype2->use(); 16 | 17 | Prototype *clonePrototype1 = prototype1->clone(); 18 | Prototype *clonePrototype2 = prototype2->clone(); 19 | 20 | cout << "Koristim klonirane prototipove: " << endl; 21 | clonePrototype1->use(); 22 | clonePrototype2->use(); 23 | 24 | delete prototype1; 25 | delete prototype2; 26 | delete clonePrototype1; 27 | delete clonePrototype2; 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /4_singletonadapter/adapter/ex1/ClassAdapter/Adaptee.h: -------------------------------------------------------------------------------- 1 | #ifndef __ADAPTEE_H 2 | #define __ADAPTEE_H 3 | 4 | #include 5 | 6 | /* klasa koju treba da adaptiramo interfejsu Target, 7 | poseduje metode koji ne odgovaraju metodima Target, 8 | zato nam je potreban adapter koji ce nam omoguciti da ovu klasu 9 | Adaptee koristimo kao vidljiv interfejs Target, 10 | implementacija metoda iz Target koju radi Adapter se vrsi 11 | pomocu dostupnih metoda iz Adaptee */ 12 | class Adaptee { 13 | public: 14 | void SpecificRequest() { 15 | std::cout << "Adaptee.SpecificRequest() called" << std::endl; 16 | } 17 | }; 18 | #endif // __ADAPTEE_H 19 | 20 | -------------------------------------------------------------------------------- /4_singletonadapter/adapter/ex1/ClassAdapter/Adapter.h: -------------------------------------------------------------------------------- 1 | #ifndef __ADAPTER_H 2 | #define __ADAPTER_H 3 | 4 | #include "Target.h" 5 | #include "Adaptee.h" 6 | #include 7 | 8 | /* klasa koja vrsi prilagodjavanje klase Adaptee ciljanom interfejsu, 9 | apstraktnoj klasi Target, 10 | vrsi implementaciju svih metoda u interfejsu Target pomocu 11 | nasledjenih metoda klase koju adaptira Adaptee 12 | u ovom slucaju treba da implementira metod Request iz interfejsa 13 | Target a to radi pozivom nasledjenog metoda SpecificRequest iz 14 | klase Adaptee 15 | nasledjivanje iz Target je public/javno 16 | dok je nasledjivanje Adaptee privatno/private jer ne zelimo 17 | da nasledjene klase od Adapter imaju pristup metodima/poljima 18 | iz Adaptee */ 19 | class Adapter: public Target, private Adaptee { 20 | public: 21 | void Request() { 22 | std::cout << "Adapter.Request() called, is using inherited SpecificRequest to complete Request" << std::endl; 23 | SpecificRequest(); 24 | } 25 | }; 26 | 27 | #endif // __ADAPTER_H 28 | -------------------------------------------------------------------------------- /4_singletonadapter/adapter/ex1/ClassAdapter/AdapterMain.cpp: -------------------------------------------------------------------------------- 1 | #include "Adapter.h" 2 | 3 | /* kreiramo instancu klase Adapter i pozivamo 4 | adaptirani metod Request */ 5 | int main() { 6 | Target *target = new Adapter(); 7 | target->Request(); 8 | 9 | delete target; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /4_singletonadapter/adapter/ex1/ClassAdapter/ClassAdapter.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 45 | 46 | -------------------------------------------------------------------------------- /4_singletonadapter/adapter/ex1/ClassAdapter/ClassAdapter.depend: -------------------------------------------------------------------------------- 1 | # depslib dependency file v1.0 2 | 1447339440 source:d:\___predavanja\oop2\lekcije\4_singleton_adapter_bridge\adapter\ex1\classadapter\adaptermain.cpp 3 | "Adapter.h" 4 | 5 | 1447339464 d:\___predavanja\oop2\lekcije\4_singleton_adapter_bridge\adapter\ex1\classadapter\adapter.h 6 | "Target.h" 7 | "Adaptee.h" 8 | 9 | 10 | 1447339260 d:\___predavanja\oop2\lekcije\4_singleton_adapter_bridge\adapter\ex1\classadapter\target.h 11 | 12 | 1447339454 d:\___predavanja\oop2\lekcije\4_singleton_adapter_bridge\adapter\ex1\classadapter\adaptee.h 13 | 14 | 15 | 1447624803 source:e:\predavanja\oop2\postavljenje_lekcije\4_singleton_adapter\adapter\ex1\classadapter\adaptermain.cpp 16 | "Adapter.h" 17 | 18 | 1447624838 e:\predavanja\oop2\postavljenje_lekcije\4_singleton_adapter\adapter\ex1\classadapter\adapter.h 19 | "Target.h" 20 | "Adaptee.h" 21 | 22 | 23 | 1447624534 e:\predavanja\oop2\postavljenje_lekcije\4_singleton_adapter\adapter\ex1\classadapter\target.h 24 | 25 | 1447624714 e:\predavanja\oop2\postavljenje_lekcije\4_singleton_adapter\adapter\ex1\classadapter\adaptee.h 26 | 27 | 28 | -------------------------------------------------------------------------------- /4_singletonadapter/adapter/ex1/ClassAdapter/ClassAdapter.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /4_singletonadapter/adapter/ex1/ClassAdapter/Target.h: -------------------------------------------------------------------------------- 1 | #ifndef __TARGET_H 2 | #define __TARGET_H 3 | 4 | /* ciljani interfejs koji zelimo da podrzava klasa 5 | koju treba da adaptiramo */ 6 | class Target { 7 | public: 8 | virtual void Request() = 0; 9 | }; 10 | #endif // __TARGET_H 11 | -------------------------------------------------------------------------------- /4_singletonadapter/adapter/ex2/ObjectAdapter/Adaptee.h: -------------------------------------------------------------------------------- 1 | #ifndef __ADAPTEE_H 2 | #define __ADAPTEE_H 3 | 4 | #include 5 | 6 | /* klasa koju treba da adaptiramo interfejsu Target, 7 | poseduje metode koji ne odgovaraju metodima Target, 8 | zato nam je potreban adapter koji ce nam omoguciti da ovu klasu 9 | Adaptee koristimo kao vidljiv interfejs Target, 10 | implementacija metoda iz Target koju radi Adapter se vrsi 11 | pozivom javnih metoda od objekta tipa Adaptee */ 12 | class Adaptee { 13 | public: 14 | void SpecificRequest() { 15 | std::cout << "Adaptee.SpecificRequest() called" << std::endl; 16 | } 17 | }; 18 | #endif // __ADAPTEE_H 19 | 20 | -------------------------------------------------------------------------------- /4_singletonadapter/adapter/ex2/ObjectAdapter/Adapter.h: -------------------------------------------------------------------------------- 1 | #ifndef __ADAPTER_H 2 | #define __ADAPTER_H 3 | 4 | #include "Target.h" 5 | #include "Adaptee.h" 6 | #include 7 | 8 | /* klasa koja vrsi prilagodjavanje klase Adaptee ciljanom interfejsu, 9 | apstraktnoj klasi Target, 10 | vrsi implementaciju svih metoda u interfejsu Target pomocu 11 | javnih metoda koje poziva na objektu klase Adaptee 12 | sto znaci da ima pristup instanci objekta Adaptee 13 | u ovom slucaju treba da implementira metod Request iz interfejsa 14 | Target a to radi pozivom metoda SpecificRequest od objekta tipa Adaptee */ 15 | class Adapter: public Target { 16 | private: 17 | Adaptee *adaptee; 18 | public: 19 | Adapter() { 20 | adaptee = new Adaptee(); 21 | } 22 | ~Adapter() { delete adaptee; } 23 | void Request() { 24 | std::cout << "Adapter.Request() called, is using SpecificRequest method of Adaptee object to complete Request" << std::endl; 25 | adaptee->SpecificRequest(); 26 | } 27 | }; 28 | 29 | #endif // __ADAPTER_H 30 | -------------------------------------------------------------------------------- /4_singletonadapter/adapter/ex2/ObjectAdapter/AdapterMain.cpp: -------------------------------------------------------------------------------- 1 | #include "Adapter.h" 2 | 3 | /* kreiramo instancu klase Adapter i pozivamo 4 | adaptirani metod Request */ 5 | int main() { 6 | Target *target = new Adapter(); 7 | target->Request(); 8 | 9 | delete target; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /4_singletonadapter/adapter/ex2/ObjectAdapter/ClassAdapter.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /4_singletonadapter/adapter/ex2/ObjectAdapter/ObjectAdapter.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 44 | 45 | -------------------------------------------------------------------------------- /4_singletonadapter/adapter/ex2/ObjectAdapter/ObjectAdapter.depend: -------------------------------------------------------------------------------- 1 | # depslib dependency file v1.0 2 | 1447339439 source:e:\predavanja\oop2\lekcije\4_singleton_adapter_bridge\adapter\ex2\objectadapter\adaptermain.cpp 3 | "Adapter.h" 4 | 5 | 1447339640 e:\predavanja\oop2\lekcije\4_singleton_adapter_bridge\adapter\ex2\objectadapter\adapter.h 6 | "Target.h" 7 | "Adaptee.h" 8 | 9 | 10 | 1447339259 e:\predavanja\oop2\lekcije\4_singleton_adapter_bridge\adapter\ex2\objectadapter\target.h 11 | 12 | 1447339575 e:\predavanja\oop2\lekcije\4_singleton_adapter_bridge\adapter\ex2\objectadapter\adaptee.h 13 | 14 | 15 | 1447625063 source:e:\predavanja\oop2\postavljenje_lekcije\4_singleton_adapter\adapter\ex2\objectadapter\adaptermain.cpp 16 | "Adapter.h" 17 | 18 | 1447625402 e:\predavanja\oop2\postavljenje_lekcije\4_singleton_adapter\adapter\ex2\objectadapter\adapter.h 19 | "Target.h" 20 | "Adaptee.h" 21 | 22 | 23 | 1447625051 e:\predavanja\oop2\postavljenje_lekcije\4_singleton_adapter\adapter\ex2\objectadapter\target.h 24 | 25 | 1447624923 e:\predavanja\oop2\postavljenje_lekcije\4_singleton_adapter\adapter\ex2\objectadapter\adaptee.h 26 | 27 | 28 | -------------------------------------------------------------------------------- /4_singletonadapter/adapter/ex2/ObjectAdapter/ObjectAdapter.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /4_singletonadapter/adapter/ex2/ObjectAdapter/Target.h: -------------------------------------------------------------------------------- 1 | #ifndef __TARGET_H 2 | #define __TARGET_H 3 | 4 | /* ciljani interfejs koji zelimo da podrzava klasa 5 | koju treba da adaptiramo */ 6 | class Target { 7 | public: 8 | virtual void Request() = 0; 9 | }; 10 | #endif // __TARGET_H 11 | -------------------------------------------------------------------------------- /4_singletonadapter/singleton/ex1/Singleton/Singleton.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 42 | 43 | -------------------------------------------------------------------------------- /4_singletonadapter/singleton/ex1/Singleton/Singleton.depend: -------------------------------------------------------------------------------- 1 | # depslib dependency file v1.0 2 | 1447321771 source:e:\predavanja\oop2\lekcije\4_singleton_adapter_bridge\singleton\ex1\singleton\singletonmain.cpp 3 | "Singleton.h" 4 | 5 | 1447321953 e:\predavanja\oop2\lekcije\4_singleton_adapter_bridge\singleton\ex1\singleton\singleton.h 6 | 7 | 8 | 1447344874 source:d:\___predavanja\oop2\lekcije\4_singleton_adapter_bridge\singleton\ex1\singleton\singletonmain.cpp 9 | "Singleton.h" 10 | 11 | 1447344859 d:\___predavanja\oop2\lekcije\4_singleton_adapter_bridge\singleton\ex1\singleton\singleton.h 12 | 13 | 14 | 1447620064 source:e:\predavanja\oop2\postavljenje_lekcije\4_singleton_adapter\singleton\ex1\singleton\singletonmain.cpp 15 | "Singleton.h" 16 | 17 | 1447620433 e:\predavanja\oop2\postavljenje_lekcije\4_singleton_adapter\singleton\ex1\singleton\singleton.h 18 | 19 | 20 | -------------------------------------------------------------------------------- /4_singletonadapter/singleton/ex1/Singleton/Singleton.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /4_singletonadapter/singleton/ex1/Singleton/SingletonMain.cpp: -------------------------------------------------------------------------------- 1 | #include "Singleton.h" 2 | 3 | /* primer koriscenja unikat / Singleton sablona */ 4 | int main() { 5 | Singleton* singletonInstance = Singleton::getInstance(); 6 | singletonInstance->use(); 7 | 8 | Singleton::getInstance()->use(); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /4_singletonadapter/singleton/ex1/Singleton/bin/Debug/Singleton.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/4_singletonadapter/singleton/ex1/Singleton/bin/Debug/Singleton.exe -------------------------------------------------------------------------------- /4_singletonadapter/singleton/ex1/Singleton/obj/Debug/SingletonMain.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/4_singletonadapter/singleton/ex1/Singleton/obj/Debug/SingletonMain.o -------------------------------------------------------------------------------- /4_singletonadapter/singleton/ex5/FileSystem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | /* singleton sablon, jedinstven primerak kreiramo putem loklane staticke promenljive unutar 7 | funkcije getInstance, kontruktori su privatni */ 8 | class FileSystem { 9 | public: 10 | static FileSystem& getInstance() { 11 | static FileSystem s_fileSystem; 12 | return s_fileSystem; 13 | } 14 | void writeFile(string file) { 15 | cout << "Pisem file: '" << file << "'" << endl; 16 | } 17 | string readFile() { 18 | return "Procitani file"; 19 | } 20 | private: 21 | FileSystem() { } 22 | FileSystem(const FileSystem& fileSystem) { } 23 | FileSystem& operator=(const FileSystem& fileSystem) { } 24 | }; 25 | 26 | int main() { 27 | FileSystem &fileSystem = FileSystem::getInstance(); 28 | fileSystem.writeFile("test file"); 29 | cout << FileSystem::getInstance().readFile() << endl; 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /5_bridgecomposite/bridge/ex1/Bridge/Abstraction.h: -------------------------------------------------------------------------------- 1 | #ifndef __ABSTRACTION_H 2 | #define __ABSTRACTION_H 3 | 4 | #include "Implementor.h" 5 | 6 | /* apstrakcija koja definise interfejs koji ima razlicite operacije koje su izvrsene koriscenjem 7 | implementacije koja je odvojena od same apstrakcije 8 | metodi apstrakcije su ostvareni pozivajuci konkretne implementacije metoda unutar objekta implementatora 9 | sadrzanog unutar apstrakcije, 10 | na ovaj nacin ostvareno je razdvajanje same apstrakcije / interfejsa metoda koje cemo da koristimo 11 | i njihove implementacije 12 | unutar destruktora apstrakcije mozemo a i ne moramo da unistimo i objekat implementor, 13 | zavisno od nacina koriscenja implementora 14 | u ovom slucaju cemo samostalno da unistimo objekat implementor */ 15 | class Abstraction { 16 | protected: 17 | Implementor *m_implementor; 18 | public: 19 | Abstraction(Implementor *implementor): m_implementor(implementor) {} 20 | virtual ~Abstraction() {} 21 | virtual void operation() { 22 | m_implementor->operationImpl(); 23 | } 24 | 25 | void setImplementor(Implementor *implementor) { 26 | m_implementor = implementor; 27 | } 28 | }; 29 | #endif // __ABSTRACTION_H 30 | -------------------------------------------------------------------------------- /5_bridgecomposite/bridge/ex1/Bridge/ConcreteImplementorA.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETE_IMPLEMENTORA_H 2 | #define __CONCRETE_IMPLEMENTORA_H 3 | 4 | #include 5 | 6 | #include "Implementor.h" 7 | 8 | /* konkretna implementacija A */ 9 | class ConcreteImplementorA: public Implementor { 10 | public: 11 | ConcreteImplementorA() {} 12 | virtual ~ConcreteImplementorA() {} 13 | void operationImpl() { 14 | std::cout << "Izvrsavam metod operationImpl() unutar konkretnog implementatora A" << std::endl; 15 | } 16 | }; 17 | #endif // __CONCRETE_IMPLEMENTORA_H 18 | -------------------------------------------------------------------------------- /5_bridgecomposite/bridge/ex1/Bridge/ConcreteImplementorB.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETE_IMPLEMENTORB_H 2 | #define __CONCRETE_IMPLEMENTORB_H 3 | 4 | #include 5 | 6 | #include "Implementor.h" 7 | 8 | /* konkretna implementacija B */ 9 | class ConcreteImplementorB: public Implementor { 10 | public: 11 | ConcreteImplementorB() {} 12 | virtual ~ConcreteImplementorB() {} 13 | void operationImpl() { 14 | std::cout << "Izvrsavam metod operationImpl() unutar konkretnog implementatora B" << std::endl; 15 | } 16 | }; 17 | #endif // __CONCRETE_IMPLEMENTORB_H 18 | -------------------------------------------------------------------------------- /5_bridgecomposite/bridge/ex1/Bridge/Implementor.h: -------------------------------------------------------------------------------- 1 | #ifndef __IMPLEMENTOR_H 2 | #define __IMPLEMENTOR_H 3 | 4 | /* interfejs konkretne implementacije */ 5 | class Implementor { 6 | public: 7 | Implementor() {} 8 | virtual ~Implementor() {} 9 | virtual void operationImpl() = 0; 10 | }; 11 | 12 | #endif // __IMPLEMENTOR_H 13 | -------------------------------------------------------------------------------- /5_bridgecomposite/bridge/ex1/Bridge/RefinedAbstraction.h: -------------------------------------------------------------------------------- 1 | #ifndef __REFINED_ABSTRACTION_H 2 | #define __REFINED_ABSTRACTION_H 3 | 4 | #include "Abstraction.h" 5 | 6 | /* prosirena apstrakcija, moze da override-uje/nadjaca metode iz osnovne klase Apstrakcija 7 | pa na drugaciji nacin da koristi metode unutar implementatora 8 | u nasem slucaju nismo dali drugaciju implementaciju metoda operation() */ 9 | class RefinedAbstraction: public Abstraction { 10 | public: 11 | RefinedAbstraction(Implementor *implementor) : Abstraction(implementor) {} 12 | virtual ~RefinedAbstraction() {} 13 | }; 14 | #endif // __REFINED_ABSTRACTION_H 15 | -------------------------------------------------------------------------------- /5_bridgecomposite/composite/ex1/CompositePattern/Component.h: -------------------------------------------------------------------------------- 1 | #ifndef __COMPONENT_H 2 | #define __COMPONENT_H 3 | 4 | /* komponenta koja ima razlicite operacije 5 | moguce je imati metode za dodelu, uklanjanje potkomponenti 6 | vec u ovoj apstraktnoj klasi komponente a ne u izvedenoj Composite 7 | ali u tom slucaju bi i proste komponente/listovi Leaf nasledili iste 8 | metode i morali bi da ih nadjacamo/override-ujemo i upravljamo/handle-ujemo 9 | taj slucaj ili sa izuzecima/exception ili na neki drugi nacin jer ne smemo 10 | da dozvolimo dodavanje ili uklanjanje potkomponenti od prostih komponenti 11 | drugi nacin je da sve komponente tretiramo kao slozene i omogucimo im 12 | da mogu da dobiju potkomponente, u tom slucaju bi se kao prosta komponenta 13 | tretirao onaj koji nema potkomponente, ali bi mu tip bio jednak kako i 14 | onima koji imaju potkomponente 15 | u nasem slucaju metode za dodavanje/uklanjanje potkomponenti nalaze se 16 | samo u Composite */ 17 | class Component { 18 | public: 19 | virtual void operation() = 0; 20 | virtual ~Component() {} 21 | }; 22 | 23 | #endif // __COMPONENT_H 24 | -------------------------------------------------------------------------------- /5_bridgecomposite/composite/ex1/CompositePattern/CompositeMain.cpp: -------------------------------------------------------------------------------- 1 | #include "Composite.h" 2 | #include "Leaf.h" 3 | 4 | int main() { 5 | Composite *root = new Composite(); 6 | Composite *leftChild = new Composite(); 7 | Leaf *rightChild = new Leaf(); 8 | Leaf *leftLeftChild = new Leaf(); 9 | Leaf *leftRightChild = new Leaf(); 10 | leftChild->add(leftLeftChild); 11 | leftChild->add(leftRightChild); 12 | root->add(leftChild); 13 | root->add(rightChild); 14 | /* kada pozovemo opereciju rekurzivno ce biti pozvana operacija na svim potkomponentama */ 15 | root->operation(); 16 | 17 | cout << "removing left child" << endl; 18 | root->remove(leftChild); 19 | root->operation(); 20 | 21 | /* u ovom slucaju cemo mi rucno da eliminisemo sve komponente koje smo kreirali, 22 | ovaj kod ce da prijavljuje gresku ukoliko imamo destruktora za Composite koji 23 | unistava i potkomponente/decu, u tom slucaju dovoljno je unistiti samo koren 24 | svake od slozenih komponenti */ 25 | delete root; 26 | delete leftChild; 27 | delete rightChild; 28 | delete leftLeftChild; 29 | delete leftRightChild; 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /5_bridgecomposite/composite/ex1/CompositePattern/CompositePattern.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /5_bridgecomposite/composite/ex1/CompositePattern/Leaf.h: -------------------------------------------------------------------------------- 1 | #ifndef __LEAF_H 2 | #define __LEAF_H 3 | 4 | class Leaf: public Composite { 5 | public: 6 | virtual ~Leaf() {} 7 | virtual void operation() { 8 | cout << "Leaf.operation()" << endl; 9 | } 10 | }; 11 | #endif // __LEAF_H 12 | -------------------------------------------------------------------------------- /6_facadedecorator/decorator/ex1/Decorator/Component.h: -------------------------------------------------------------------------------- 1 | #ifndef __COMPONENT_H 2 | #define __COMPONENT_H 3 | 4 | /* apstraktna komponenta, napravicemo nekoliko izvedenih 5 | komponenti ali odredjena svojstva cemo da izdvojimo 6 | u zasebne klase - dekoratere/dopune koje mozemo 7 | po potrebi (i tokom izvrsavanja/runtime) da dodamo 8 | na nase konkretne komponente bez da pravimo instance 9 | neke druge potklase 10 | u ovom slucaju komponenta ima samo jednu operaciju 11 | koja moze da se ostvari na vise nacina 12 | svaki od tih nacina sa svojom nekom posebnom karakteristikom 13 | mozemo da imamo u nekoj od dopuna/dekoratera */ 14 | class Component { 15 | public: 16 | virtual void operation() = 0; 17 | virtual ~Component() { } 18 | }; 19 | #endif // __COMPONENT_H 20 | -------------------------------------------------------------------------------- /6_facadedecorator/decorator/ex1/Decorator/ConcreteComponent.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETE_COMPONENT_H 2 | #define __CONCRETE_COMPONENT_H 3 | 4 | #include 5 | 6 | #include "Component.h" 7 | 8 | /* konkretna komponenta, implementira operaciju koju nasledjuje */ 9 | class ConcreteComponent: public Component { 10 | public: 11 | virtual void operation() { 12 | std::cout << "ConcreteComponent.operation() executed" << std::endl; 13 | } 14 | virtual ~ConcreteComponent() { } 15 | }; 16 | #endif // __CONCRETE_COMPONENT_H 17 | -------------------------------------------------------------------------------- /6_facadedecorator/decorator/ex1/Decorator/ConcreteDecorator.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETE_DECORATOR_H 2 | #define __CONCRETE_DECORATOR_H 3 | 4 | #include 5 | 6 | #include "Decorator.h" 7 | 8 | /* konkretna dopuna/dekorator 9 | implementira jedno svojstvo / nacin na koji ce biti ostvarena operacija 10 | na ovaj nacin imamo i odvojenost razlicitih implementacija od objekta 11 | konkretni dekorateri operaciju (u ovom slucaju operation()) implementiraju 12 | tako sto pozivaju metod operation() na objektu koji obogacuju ali uz to 13 | imaju i neku dodatnu implementaciju koja predstavlja to svojstvo koje 14 | konkretna dopuna nudi */ 15 | class ConcreteDecorator: public Decorator { 16 | public: 17 | virtual void operation() { 18 | std::cout << "ConcreteDecorator.operation()" << std::endl; 19 | m_component->operation(); 20 | } 21 | ConcreteDecorator(Component *component) : Decorator(component) {} 22 | virtual ~ConcreteDecorator() { } 23 | }; 24 | #endif // __CONCRETE_DECORATOR_H 25 | -------------------------------------------------------------------------------- /6_facadedecorator/decorator/ex1/Decorator/Decorator.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /6_facadedecorator/decorator/ex1/Decorator/DecoratorMain.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "ConcreteComponent.h" 4 | #include "ConcreteDecorator.h" 5 | 6 | 7 | int main() { 8 | /* kreiramo komponentu component koja je objekat tipa ConcreteDecorator cijem 9 | konstruktoru je prosledjen objekat tipa ConcreteComponent */ 10 | std::unique_ptr component(new ConcreteDecorator(new ConcreteComponent())); 11 | /* pozivamo operaciju na komponenti koja je u stvari omotac/wrapper oko konkretnog 12 | objekta, pozivamo metod konkretnog dekoratera koji ce unutar sebe da pozove i metod 13 | objekta tipa ConcreteComponent */ 14 | component->operation(); 15 | /* kreiramo drugi objekat tipa ConcreteComponent i pozivamo operaciju */ 16 | Component *component2 = new ConcreteComponent(); 17 | component2->operation(); 18 | /* kreiramo 'obogacenu' komponentu sa novim svojstvom koje nudi ConcreteDecorator 19 | dopuna, konstruktoru prosledjujemo objekat koji zelimo da obogatimo */ 20 | Component *enhancedComponent = new ConcreteDecorator(component2); 21 | /* i pozivamo operaciju na obogacenom objektu */ 22 | enhancedComponent->operation(); 23 | 24 | delete enhancedComponent; 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /6_facadedecorator/decorator/ex3/ZivotinjaDecorator.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/6_facadedecorator/decorator/ex3/ZivotinjaDecorator.exe -------------------------------------------------------------------------------- /6_facadedecorator/decorator/ex3/ZivotinjaDecorator.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/6_facadedecorator/decorator/ex3/ZivotinjaDecorator.o -------------------------------------------------------------------------------- /6_facadedecorator/facade/ex1/Facade/Class1.h: -------------------------------------------------------------------------------- 1 | #ifndef __CLASS1_H 2 | #define __CLASS1_H 3 | 4 | #include 5 | 6 | /* klasa koja predstavlja neki kompleksan podsistem */ 7 | class Class1 { 8 | public: 9 | virtual void do1() { 10 | std::cout << "Class1.do1()" << std::endl; 11 | } 12 | }; 13 | #endif // __CLASS1_H 14 | -------------------------------------------------------------------------------- /6_facadedecorator/facade/ex1/Facade/Class2.h: -------------------------------------------------------------------------------- 1 | #ifndef __CLASS2_H 2 | #define __CLASS2_H 3 | 4 | #include 5 | 6 | /* klasa koja predstavlja neki kompleksan podsistem */ 7 | class Class2 { 8 | public: 9 | virtual void do2() { 10 | std::cout << "Class2.do2()" << std::endl; 11 | } 12 | }; 13 | #endif // __CLASS2_H 14 | -------------------------------------------------------------------------------- /6_facadedecorator/facade/ex1/Facade/Class3.h: -------------------------------------------------------------------------------- 1 | #ifndef __CLASS3_H 2 | #define __CLASS3_H 3 | 4 | #include 5 | 6 | /* klasa koja predstavlja neki kompleksan podsistem */ 7 | class Class3 { 8 | public: 9 | virtual void do3() { 10 | std::cout << "Class3.do3()" << std::endl; 11 | } 12 | }; 13 | #endif // __CLASS3_H 14 | -------------------------------------------------------------------------------- /6_facadedecorator/facade/ex1/Facade/Facade.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 46 | 47 | -------------------------------------------------------------------------------- /6_facadedecorator/facade/ex1/Facade/Facade.depend: -------------------------------------------------------------------------------- 1 | # depslib dependency file v1.0 2 | 1448550576 source:d:\___predavanja\oop2\lekcije\6_facade_decorator\facade\ex1\facade\facademain.cpp 3 | "Facade.h" 4 | 5 | 1448550580 d:\___predavanja\oop2\lekcije\6_facade_decorator\facade\ex1\facade\facade.h 6 | "Class1.h" 7 | "Class2.h" 8 | "Class3.h" 9 | 10 | 11 | 1448550464 d:\___predavanja\oop2\lekcije\6_facade_decorator\facade\ex1\facade\class1.h 12 | 13 | 14 | 1448550482 d:\___predavanja\oop2\lekcije\6_facade_decorator\facade\ex1\facade\class2.h 15 | 16 | 17 | 1448550502 d:\___predavanja\oop2\lekcije\6_facade_decorator\facade\ex1\facade\class3.h 18 | 19 | 20 | -------------------------------------------------------------------------------- /6_facadedecorator/facade/ex1/Facade/Facade.h: -------------------------------------------------------------------------------- 1 | #ifndef __FACADE_H 2 | #define __FACADE_H 3 | 4 | #include 5 | 6 | #include "Class1.h" 7 | #include "Class2.h" 8 | #include "Class3.h" 9 | 10 | /* umesto da klijent sam koristi 'haoticnu' kombinaciju 11 | podsistema i time poveca sanse da napravi gresku, 12 | napravimo interfejs fasada kojim cemo da klijentu 13 | napravimo dostupno nekoliko metoda 14 | koji su potrebni klijentu a za realizaciju tih metoda 15 | fasada ce koristiti podsisteme 16 | na ovaj nacin stepen slobode koriscenja podsistema 17 | je limitiran za klijenta, dostupno mu je samo sto 18 | API fasada nudi, naravno uvek je moguce prosiriti 19 | kolekciju metoda koju nudi fasada kada za to postoji 20 | potreba od strane klijenta */ 21 | class Facade { 22 | public: 23 | virtual void doComplexWork() { 24 | Class1 c1; 25 | Class2 c2; 26 | Class3 c3; 27 | std::cout << "Facade.doComplexWork()" << std::endl; 28 | c1.do1(); 29 | c2.do2(); 30 | c3.do3(); 31 | } 32 | }; 33 | #endif // __FACADE_H 34 | -------------------------------------------------------------------------------- /6_facadedecorator/facade/ex1/Facade/Facade.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /6_facadedecorator/facade/ex1/Facade/FacadeMain.cpp: -------------------------------------------------------------------------------- 1 | #include "Facade.h" 2 | 3 | /* klijent sada ima jednostavno koriscenje APIja koji nudi fasada 4 | na raspolaganju 5 | ne mora da brine o gomili stvari o kojima je trebao kada je 6 | samostalno trebao da ostvari operaciju koriscenjem razlicitih 7 | podsistema, ceo taj problem je prepusten fasadi koja nudi 8 | sada limitiran nacin koriscenja podsistema, ali ono sto je 9 | klijentu potrebno */ 10 | int main() { 11 | Facade facade; 12 | facade.doComplexWork(); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /7_flyweightproxy/flyweight/ex1/Flyweight/ConcreteFlyweight.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETE_FLYWEIGHT_H 2 | #define __CONCRETE_FLYWEIGHT_H 3 | 4 | #include 5 | 6 | #include "Flyweight.h" 7 | /* konkretna muva koja je deljiva 8 | kod konstrukcije muva / flyweight objekata neophodno je uspesno odvojiti interno state / intrinsic state 9 | koje moze biti deljivo i nezavisno od konteksta u kojem se konkretne muve koriste, 10 | i spoljasnje stanje koje zavisi od konteksta u kojem se ta muva deli i koje svaki korisnik konkretne muve 11 | to spoljasnje stanje moze da prosledi objektu konkretne muve */ 12 | class ConcreteFlyweight: public Flyweight { 13 | public: 14 | ConcreteFlyweight(const string& intrinsicState) : Flyweight(intrinsicState) {} 15 | virtual ~ConcreteFlyweight() {} 16 | virtual void Operation(const string& extrinsicState) { 17 | std::cout << "ConcreteFlyweight.Operation(" << extrinsicState << "), intrinsicState = " << GetIntrinsicState() << std::endl; 18 | } 19 | }; 20 | 21 | #endif // __CONCRETE_FLYWEIGHT_H 22 | -------------------------------------------------------------------------------- /7_flyweightproxy/flyweight/ex1/Flyweight/Flyweight.h: -------------------------------------------------------------------------------- 1 | #ifndef __FLYWEIGHT_H 2 | #define __FLYWEIGHT_H 3 | 4 | /* apstraktna klasa za Flyweight/muva objekte 5 | svaki objekat tog tipa ima unustrasnje (instrinsic) stanje koje ne zavisi od konteksta (okruzenja) 6 | u kome se objekat nalazi, i spoljasnje (extrinsic) koje zavisi od konteksta (okruzenja) u kome se objekat koristi 7 | u nasem slucaju spoljasnje stanje prosledjujemo preko parametara extrinsicState metoda Operation */ 8 | class Flyweight { 9 | public: 10 | virtual ~Flyweight() {} 11 | virtual void Operation(const string& extrinsicState) = 0; 12 | virtual string GetIntrinsicState() { return m_intrinsicState; } 13 | protected: 14 | Flyweight(string intrinsicState) : m_intrinsicState(intrinsicState) { } 15 | private: 16 | string m_intrinsicState; 17 | }; 18 | 19 | #endif // __FLYWEIGHT_H 20 | -------------------------------------------------------------------------------- /7_flyweightproxy/flyweight/ex1/Flyweight/Flyweight.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /7_flyweightproxy/flyweight/ex1/Flyweight/FlyweightFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef __FLYWEIGHTFACTORY_H 2 | #define __FLYWEIGHTFACTORY_H 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | #include "ConcreteFlyweight.h" 9 | /* flyweight / fabrika muva 10 | klijent koristi fabriku da dobije objekte tipa ConcreteFlyweight (konkretne muve)*/ 11 | class FlyweightFactory { 12 | public: 13 | FlyweightFactory() {} 14 | ~FlyweightFactory() { 15 | while (m_keyToFlyweight.begin() != m_keyToFlyweight.end()) { 16 | if (m_keyToFlyweight.begin()->second != NULL) { 17 | delete m_keyToFlyweight.begin()->second; 18 | } 19 | m_keyToFlyweight.erase(m_keyToFlyweight.begin()); 20 | } 21 | } 22 | /* metod za dobijanje konkretne muve koju moze da deli vise klijenata */ 23 | Flyweight* GetFlyweight(string key) { 24 | if (m_keyToFlyweight.find(key) == m_keyToFlyweight.end()) { 25 | m_keyToFlyweight[key] = new ConcreteFlyweight(key); 26 | } 27 | return m_keyToFlyweight[key]; 28 | } 29 | private: 30 | map m_keyToFlyweight; 31 | }; 32 | 33 | #endif // __FLYWEIGHTFACTORY_H 34 | -------------------------------------------------------------------------------- /7_flyweightproxy/flyweight/ex1/Flyweight/FlyweightMain.cpp: -------------------------------------------------------------------------------- 1 | #include "FlyweightFactory.h" 2 | #include "UnsharedConcreteFlyweight.h" 3 | 4 | /* testiramo koriscenje muva uzorka, 5 | koristimo fabriku muva da kreiramo instance muva tipa koji zelimo prosledjujuci 6 | fabrickom metodu fabrike muva kljuc po kome cemo da biramo tip instance muve koja nam treba 7 | kada pozivamo operaciju na instanci muve prosledjujemo i spoljasnje stanje / kontekst u kojem 8 | se ta konkretna muva koristi 9 | primer poseduje i instancu muve koja nije deljiva / UnsharedConcreteFlyweight */ 10 | int main() { 11 | FlyweightFactory flyweightFactory; 12 | 13 | Flyweight *flyweight1 = flyweightFactory.GetFlyweight("Flyweight 1"); 14 | Flyweight *flyweight2 = flyweightFactory.GetFlyweight("Flyweight 2"); 15 | cout << "Executing Operation() on Flyweight 1" << endl; 16 | flyweight1->Operation("Extrinsic State 1"); 17 | cout << "Executing Operation() on Flyweight 2" << endl; 18 | flyweight2->Operation("Extrinsic State 2"); 19 | 20 | UnsharedConcreteFlyweight *unsharedFlyweight = new UnsharedConcreteFlyweight("Unshared Flyweight"); 21 | cout << "Executing Operation() on Unshared Flyweight" << endl; 22 | unsharedFlyweight->Operation("Extrinsic State Unshared"); 23 | 24 | delete unsharedFlyweight; 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /7_flyweightproxy/flyweight/ex1/Flyweight/UnsharedConcreteFlyweight.h: -------------------------------------------------------------------------------- 1 | #ifndef __UNSHAREDCONCRETEFLYWEIGHT_H 2 | #define __UNSHAREDCONCRETEFLYWEIGHT_H 3 | 4 | #include 5 | 6 | #include "Flyweight.h" 7 | /* konkretne muva koja nije deljiva */ 8 | class UnsharedConcreteFlyweight: public Flyweight { 9 | public: 10 | UnsharedConcreteFlyweight(const string& intrinsicState): Flyweight(intrinsicState) {} 11 | void Operation(const string& extrinsicState) { 12 | std::cout << "UnsharedConcreteFlyweight.Operation(" << extrinsicState << "), intrinsicState = " << GetIntrinsicState() << std::endl; 13 | } 14 | }; 15 | 16 | #endif // __UNSHAREDCONCRETEFLYWEIGHT_H 17 | 18 | -------------------------------------------------------------------------------- /7_flyweightproxy/proxy/ex1/Proxy/Proxy.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 45 | 46 | -------------------------------------------------------------------------------- /7_flyweightproxy/proxy/ex1/Proxy/Proxy.depend: -------------------------------------------------------------------------------- 1 | # depslib dependency file v1.0 2 | 1449154603 source:e:\predavanja\oop2\lekcije\7_flyweight_proxy\proxy\ex1\proxy\proxymain.cpp 3 | "Proxy.h" 4 | "RealSubject.h" 5 | 6 | 1449154616 e:\predavanja\oop2\lekcije\7_flyweight_proxy\proxy\ex1\proxy\proxy.h 7 | 8 | "RealSubject.h" 9 | 10 | 1449154028 e:\predavanja\oop2\lekcije\7_flyweight_proxy\proxy\ex1\proxy\subject.h 11 | 12 | 1449154022 e:\predavanja\oop2\lekcije\7_flyweight_proxy\proxy\ex1\proxy\realsubject.h 13 | 14 | "Subject.h" 15 | 16 | 1449154603 source:c:\users\sidejan\google drive\oop2\vezbe_sredjene\za slanje\7_flyweightproxy\proxy\ex1\proxy\proxymain.cpp 17 | "Proxy.h" 18 | "RealSubject.h" 19 | 20 | 1449840943 c:\users\sidejan\google drive\oop2\vezbe_sredjene\za slanje\7_flyweightproxy\proxy\ex1\proxy\proxy.h 21 | 22 | "RealSubject.h" 23 | 24 | 1449840949 c:\users\sidejan\google drive\oop2\vezbe_sredjene\za slanje\7_flyweightproxy\proxy\ex1\proxy\realsubject.h 25 | 26 | "Subject.h" 27 | 28 | 1449840955 c:\users\sidejan\google drive\oop2\vezbe_sredjene\za slanje\7_flyweightproxy\proxy\ex1\proxy\subject.h 29 | 30 | -------------------------------------------------------------------------------- /7_flyweightproxy/proxy/ex1/Proxy/Proxy.h: -------------------------------------------------------------------------------- 1 | #ifndef __PROXY_H 2 | #define __PROXY_H 3 | 4 | #include 5 | using namespace std; 6 | 7 | #include "RealSubject.h" 8 | 9 | /* zastupnik/proxy za realni subjekt, klijent vidi realni objekat preko ovog 'omotaca'/wrappera za RealSubject i poziva 10 | metod Request na proxiju, proxy ovaj metod implementira tako sto vrsi cache-iranje podataka, ili proveru pristupa 11 | ili racunanje nekog medju rezultata pre nego sto pozove konkretan metod Request na pravom objektu tipa RealSubject 12 | za koga je posredni. Zato je neophodno da proxy ima interno ima referencu na objekat tipa RealSubject ciju instancu 13 | cemo da napravimo tek kada nam je neophodna da bi se konkretan metod Request pozvao na njemu */ 14 | class Proxy: public Subject { 15 | public: 16 | Proxy(): m_subject(NULL) {} 17 | virtual ~Proxy() { if (m_subject != NULL) delete m_subject; } 18 | virtual void Request() { 19 | cout << "Proxy.Request()" << endl; 20 | if (m_subject == NULL) { 21 | m_subject = new RealSubject(); 22 | } 23 | m_subject->Request(); 24 | } 25 | protected: 26 | Subject* m_subject; 27 | }; 28 | #endif // __PROXY_H 29 | -------------------------------------------------------------------------------- /7_flyweightproxy/proxy/ex1/Proxy/Proxy.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /7_flyweightproxy/proxy/ex1/Proxy/ProxyMain.cpp: -------------------------------------------------------------------------------- 1 | #include "Proxy.h" 2 | #include "RealSubject.h" 3 | 4 | int main() { 5 | Subject* realSubject = new RealSubject(); 6 | Subject* proxy = new Proxy(); 7 | cout << "Calling method Request through the proxy: " << endl; 8 | proxy->Request(); 9 | cout << "Calling method Request through the real subject: " << endl; 10 | realSubject->Request(); 11 | 12 | delete proxy; 13 | delete realSubject; 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /7_flyweightproxy/proxy/ex1/Proxy/RealSubject.h: -------------------------------------------------------------------------------- 1 | #ifndef __REALSUBJECT_H 2 | #define __REALSUBJECT_H 3 | 4 | #include 5 | using namespace std; 6 | 7 | #include "Subject.h" 8 | 9 | /* realni subjekt koji ima neki metod (u ovom slucaju npr. Request()) cije konkretno pozivanje zelimo da odlozimo sto je vise moguce 10 | jer je 'skupo' u smislu resursa (bilo vremenskih, memorijskih itd), ili pak zelimo da kontrolisemo pristup ovom metodu itd 11 | zato klijenti ovaj metod pozivaju preko zastupnika/proxy-ja za realnog subjekta */ 12 | class RealSubject: public Subject { 13 | public: 14 | virtual ~RealSubject() {} 15 | virtual void Request() { 16 | cout << "RealSubject.Request()" << endl; 17 | } 18 | }; 19 | #endif // __REALSUBJECT_H 20 | -------------------------------------------------------------------------------- /7_flyweightproxy/proxy/ex1/Proxy/Subject.h: -------------------------------------------------------------------------------- 1 | #ifndef __SUBJECT_H 2 | #define __SUBJECT_H 3 | 4 | /* zajednicki interfejs realnog subjekta (klasa RealSubject) za koga pravimo zastupnika/proxy i 5 | zastupnika/proxy-ja (Proxy klasa) */ 6 | class Subject { 7 | public: 8 | virtual ~Subject() {} 9 | virtual void Request() = 0; 10 | }; 11 | 12 | #endif // __SUBJECT_H 13 | -------------------------------------------------------------------------------- /7_flyweightproxy/proxy/ex1/Proxy/bin/Debug/Proxy.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/7_flyweightproxy/proxy/ex1/Proxy/bin/Debug/Proxy.exe -------------------------------------------------------------------------------- /7_flyweightproxy/proxy/ex1/Proxy/obj/Debug/ProxyMain.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/7_flyweightproxy/proxy/ex1/Proxy/obj/Debug/ProxyMain.o -------------------------------------------------------------------------------- /8_chainofresponsibilitycommand/8_chainofresponsibilitycommand/chain_of_responsibility/ex1/ChainOfResponsibility/ChainOfResponsibility.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /8_chainofresponsibilitycommand/8_chainofresponsibilitycommand/chain_of_responsibility/ex1/ChainOfResponsibility/ChainOfResponsibilityMain.cpp: -------------------------------------------------------------------------------- 1 | #include "ConcreteHandler1.h" 2 | #include "ConcreteHandler2.h" 3 | 4 | /* kreiramo lanac razresavaca, rucno ih dodajuci jedan na drugi, 5 | ovo dodavanje i kreiranje lanca moze da se uradi na vise nacina, 6 | jedan je da se poput lancane liste dodaje novi element na pocetak/kraj liste 7 | i da cuvamo samo razresavac koji je koren lanca i na njemu pozivamo metod 8 | za dodavanje novog razresavaca u listu 9 | nakon kreiranja lanca prvom u lancu prosledjujemo zahtev koji ce 10 | da nadje odgovarajuci razresavac koji ce da razresi zahtev */ 11 | int main() { 12 | Handler* handler1 = new ConcreteHandler1(); 13 | Handler* handler2a = new ConcreteHandler2(); 14 | Handler* handler2b = new ConcreteHandler2(); 15 | handler2b->SetSuccessor(handler2a); 16 | handler2a->SetSuccessor(handler1); 17 | 18 | Request request; 19 | handler2b->HandleRequest(request); 20 | 21 | delete handler1; 22 | delete handler2a; 23 | delete handler2b; 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /8_chainofresponsibilitycommand/8_chainofresponsibilitycommand/chain_of_responsibility/ex1/ChainOfResponsibility/ConcreteHandler1.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETEHANDLER1_H 2 | #define __CONCRETEHANDLER1_H 3 | 4 | #include "Handler.h" 5 | 6 | /* konkretni razresavac 1, implementira metod za razresenje zahteva, 7 | HandleRequest tako sto se pita da li je u mogucnosti da razresi trenutni 8 | zahtev, ako nije u stanju da razresi zahtev zove HandleRequest od nasledjene 9 | klase (Handler) koja ce da se pobrine da prosledi zahtev narednom 10 | razresavacu u lancu */ 11 | class ConcreteHandler1: public Handler { 12 | public: 13 | ConcreteHandler1() {} 14 | virtual ~ConcreteHandler1() {} 15 | void HandleRequest(const Request& request) { 16 | if (CanHandle(request)) { 17 | cout << "ConcreteHandler1 handled the request." << endl; 18 | } else { 19 | Handler::HandleRequest(request); 20 | } 21 | } 22 | virtual bool CanHandle(const Request& request) { 23 | // uslovi da ovaj handler moze da razresi 24 | // trenutni zahtev 25 | // ovde prosto stavljamo true za ConcreteHandler1 26 | return true; 27 | } 28 | }; 29 | 30 | #endif // __CONCRETEHANDLER1_H 31 | -------------------------------------------------------------------------------- /8_chainofresponsibilitycommand/8_chainofresponsibilitycommand/chain_of_responsibility/ex1/ChainOfResponsibility/ConcreteHandler2.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETEHANDLER2_H 2 | #define __CONCRETEHANDLER2_H 3 | 4 | #include "Handler.h" 5 | 6 | /* konkretni razresavac 2 */ 7 | class ConcreteHandler2: public Handler { 8 | public: 9 | ConcreteHandler2() {} 10 | virtual ~ConcreteHandler2() {} 11 | void HandleRequest(const Request& request) { 12 | if (CanHandle(request)) { 13 | cout << "ConcreteHandler2 handled the request." << endl; 14 | } else { 15 | Handler::HandleRequest(request); 16 | } 17 | } 18 | virtual bool CanHandle(const Request& request) { 19 | // uslovi da ovaj handler moze da razresi 20 | // trenutni zahtev 21 | // ovde prosto stavljamo false za ConcreteHandler2 22 | return false; 23 | } 24 | }; 25 | 26 | #endif // __CONCRETEHANDLER2_H 27 | 28 | -------------------------------------------------------------------------------- /8_chainofresponsibilitycommand/8_chainofresponsibilitycommand/chain_of_responsibility/ex1/ChainOfResponsibility/Request.h: -------------------------------------------------------------------------------- 1 | #ifndef __REQUEST_H 2 | #define __REQUEST_H 3 | 4 | /* simulacija zahteva koji razresavaci treba da razrese */ 5 | class Request { 6 | public: 7 | Request() {} 8 | ~Request() {} 9 | }; 10 | 11 | #endif // __REQUEST_H 12 | -------------------------------------------------------------------------------- /8_chainofresponsibilitycommand/8_chainofresponsibilitycommand/command/ex1/Command/Command.h: -------------------------------------------------------------------------------- 1 | #ifndef __COMMAND_H 2 | #define __COMMAND_H 3 | 4 | #include "Receiver.h" 5 | 6 | /* apstraktna klasa koja je interfejs Komande 7 | nudi samo metode Execute koji se poziva na Komandi od strane 8 | pokretaca akcije/Invoker-a, 9 | moguce je menjati primaoca akcije/Receiver-a putem metoda 10 | SetReceiver (npr. dodeliti komandi dokument nad kojim ce 11 | da vrsi neke konkretne operacije) */ 12 | class Command { 13 | public: 14 | Command() {} 15 | virtual ~Command() {} 16 | virtual void Execute() = 0; 17 | virtual void SetReceiver(Receiver* receiver) = 0; 18 | protected: 19 | }; 20 | 21 | #endif // __COMMAND_H 22 | -------------------------------------------------------------------------------- /8_chainofresponsibilitycommand/8_chainofresponsibilitycommand/command/ex1/Command/Command.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /8_chainofresponsibilitycommand/8_chainofresponsibilitycommand/command/ex1/Command/CommandMain.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "ConcreteCommand.h" 3 | #include "Invoker.h" 4 | 5 | /* kreiramo primaoca akcije / Receiver, i konkretnu komandu kojoj 6 | postavimo primaoca akcije 7 | zatim kreiramo dva razlicita pokretaca akcije / Invoker kojima 8 | dodelimo istu konkretnu komandu i nakon toga izazovemo akciju 9 | na pokretacima akcije / Invoker-ima pozivom metoda Invoke() 10 | u oba slucaja isti efekat na primaocu ce biti ostvaren jer je 11 | koriscena ista konkretna komanda */ 12 | int main() { 13 | Receiver* receiver = new Receiver(); 14 | Command* command = new ConcreteCommand(); 15 | command->SetReceiver(receiver); 16 | 17 | Invoker* invokerA = new Invoker(); 18 | Invoker* invokerB = new Invoker(); 19 | invokerA->SetCommand(command); 20 | invokerB->SetCommand(command); 21 | cout << "Invoking object invokerA" << endl; 22 | invokerA->Invoke(); 23 | cout << "Invoking object invokerB" << endl; 24 | invokerB->Invoke(); 25 | 26 | delete invokerA; 27 | delete invokerB; 28 | delete command; 29 | delete receiver; 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /8_chainofresponsibilitycommand/8_chainofresponsibilitycommand/command/ex1/Command/ConcreteCommand.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETECOMMAND_H 2 | #define __CONCRETECOMMAND_H 3 | 4 | #include "Command.h" 5 | 6 | /* konkretna komanda, nudi implementaciju metoda Execute 7 | koji pokretac/Invoker poziva na dodeljenoj instanci Komande 8 | metod Execute je implementiran tako da se izvrsi modifikacija 9 | primaoca akcije/Receiver-a putem pozivanja metoda Action 10 | na primaocu akcije, 11 | to znaci da Komanda poseduje referencu na primaoca akcije/Receiver-a 12 | (npr. to moze da bude neki Dokument) */ 13 | class ConcreteCommand: public Command { 14 | public: 15 | ConcreteCommand() {} 16 | virtual ~ConcreteCommand() {} 17 | virtual void Execute() { 18 | cout << "Executing command" << endl; 19 | m_receiver->Action(); 20 | } 21 | 22 | virtual void SetReceiver(Receiver* receiver) { 23 | m_receiver = receiver; 24 | } 25 | protected: 26 | Receiver* m_receiver; 27 | }; 28 | 29 | #endif // __CONCRETECOMMAND_H 30 | -------------------------------------------------------------------------------- /8_chainofresponsibilitycommand/8_chainofresponsibilitycommand/command/ex1/Command/Invoker.h: -------------------------------------------------------------------------------- 1 | #ifndef __INVOKER_H 2 | #define __INVOKER_H 3 | 4 | /* pokretac komande, zahteva od komande da izvrsi neku operaciju, 5 | to moze da bude element menija (pa se prilikom klika menija poziva 6 | komanda da izvrsi neku operaciju) a ista ta komanda moze da bude 7 | dodeljena nekom drugom pokretacu (npr. dugme koje radi istu tu operaciju 8 | prilikom klika) 9 | metod Invoke()/pokreni izaziva izvrsavanje akcije koju definise komanda, 10 | konkretna komanda se dodeljuje svakom pokretacu, moze preko konstruktora, 11 | a moze i preko posebnog metoda u nasem slucaju SetCommand 12 | pa tako u toku izvrsavanja/run-time mozemo da menjamo konkretnu komandu 13 | dodeljenu nekom pokretacu/Invoker-u */ 14 | class Invoker { 15 | public: 16 | Invoker() {} 17 | virtual ~Invoker() {} 18 | virtual void Invoke() { 19 | if (m_command != NULL) { 20 | m_command->Execute(); 21 | } 22 | } 23 | virtual void SetCommand(Command* command) { 24 | m_command = command; 25 | } 26 | protected: 27 | Command* m_command; 28 | }; 29 | 30 | #endif // __INVOKER_H 31 | -------------------------------------------------------------------------------- /8_chainofresponsibilitycommand/8_chainofresponsibilitycommand/command/ex1/Command/Receiver.h: -------------------------------------------------------------------------------- 1 | #ifndef __RECEIVER_H 2 | #define __RECEIVER_H 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | /* primalac akcija/nad kojim se realizuju konkretni efekti komande, 9 | to moze da bude neki dokument (npr. komanda za paste ce na dokumentu da doda kopirani 10 | tekst) 11 | u nasem slucaju primalac/receiver ima metod za manipulaciju nad njim to je Action() */ 12 | class Receiver { 13 | public: 14 | Receiver() { } 15 | virtual ~Receiver() {} 16 | virtual void Action () { 17 | cout << "Receiver.Action()" << endl; 18 | } 19 | }; 20 | 21 | #endif // __RECEIVER_H 22 | -------------------------------------------------------------------------------- /9_interpreteriterator/interpreter/ex1/Interpreter/AbstractExpression.h: -------------------------------------------------------------------------------- 1 | #ifndef __ABSTRACTEXPRESSION_H 2 | #define __ABSTRACTEXPRESSION_H 3 | 4 | #include "Context.h" 5 | 6 | /* apstraktna klasa svih izraza koji mogu da se nadju u gramatici, 7 | ako se radi o gramatici za aritmeticke izraze mozemo da imamo: 8 | E->E+E 9 | E->E*E 10 | E->(E) 11 | E->ime_promenljive (u ovom slucaju ime_promenljive bi bio naziv neke promenljive/terminalni izraz) 12 | E->vrednost (u ovom slucaju vrednost je neka konstanta) 13 | i za vrednost i za naziv promenljivih bi bili dati neki regularni izrazi koji definisu 14 | kako se generise vrednost (realan broj) ili naziv promenljive */ 15 | class AbstractExpression { 16 | public: 17 | virtual ~AbstractExpression() {} 18 | virtual void Interpret(Context* context) = 0; 19 | }; 20 | 21 | #endif // __ABSTRACTEXPRESSION_H 22 | -------------------------------------------------------------------------------- /9_interpreteriterator/interpreter/ex1/Interpreter/Context.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONTEXT_H 2 | #define __CONTEXT_H 3 | 4 | /* globalni kontekst koji interpretator moze da koristi prilikom interpretacije jezika */ 5 | class Context { 6 | }; 7 | #endif // __CONTEXT_H 8 | -------------------------------------------------------------------------------- /9_interpreteriterator/interpreter/ex1/Interpreter/Interpreter.depend: -------------------------------------------------------------------------------- 1 | # depslib dependency file v1.0 2 | 1450298771 source:c:\users\sidejan\google drive\oop2\vezbe_sredjene\9_interpreter_iterator_mediator\interpreter\ex1\interpreter\interpretermain.cpp 3 | 4 | "TerminalExpression.h" 5 | "NonterminalExpression.h" 6 | 7 | 1450298792 c:\users\sidejan\google drive\oop2\vezbe_sredjene\9_interpreter_iterator_mediator\interpreter\ex1\interpreter\terminalexpression.h 8 | 9 | "AbstractExpression.h" 10 | 11 | 1450298810 c:\users\sidejan\google drive\oop2\vezbe_sredjene\9_interpreter_iterator_mediator\interpreter\ex1\interpreter\abstractexpression.h 12 | "Context.h" 13 | 14 | 1450296212 c:\users\sidejan\google drive\oop2\vezbe_sredjene\9_interpreter_iterator_mediator\interpreter\ex1\interpreter\context.h 15 | 16 | 1450298788 c:\users\sidejan\google drive\oop2\vezbe_sredjene\9_interpreter_iterator_mediator\interpreter\ex1\interpreter\nonterminalexpression.h 17 | 18 | "AbstractExpression.h" 19 | 20 | -------------------------------------------------------------------------------- /9_interpreteriterator/interpreter/ex1/Interpreter/Interpreter.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /9_interpreteriterator/interpreter/ex1/Interpreter/InterpreterMain.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "TerminalExpression.h" 4 | #include "NonterminalExpression.h" 5 | 6 | using namespace std; 7 | 8 | /* kreiramo niz konkretnih izraza, niz neterminalnih i terminalnih izraza i nakon 9 | toga pozovemo metod Interpret da interpretira svaki od izraza koristeci globalni kontekst */ 10 | int main() { 11 | Context* context = new Context(); 12 | vector expressions; 13 | 14 | expressions.push_back(new NonterminalExpression()); 15 | expressions.push_back(new TerminalExpression()); 16 | expressions.push_back(new TerminalExpression()); 17 | expressions.push_back(new NonterminalExpression()); 18 | expressions.push_back(new TerminalExpression()); 19 | 20 | for (vector::iterator it = expressions.begin(); it != expressions.end(); ++it) { 21 | (*it)->Interpret(context); 22 | } 23 | 24 | for (vector::iterator it = expressions.begin(); it != expressions.end(); ++it) { 25 | delete *it; 26 | } 27 | 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /9_interpreteriterator/interpreter/ex1/Interpreter/NonterminalExpression.h: -------------------------------------------------------------------------------- 1 | #ifndef __NONTERMINALEXPRESSION_H 2 | #define __NONTERMINALEXPRESSION_H 3 | 4 | #include 5 | #include "AbstractExpression.h" 6 | 7 | using namespace std; 8 | 9 | /* neterminalni izraz iz gramatike koju ce interpereter da interpretira 10 | koji je izvedena klasa abstraktnog izraza / AbstractExpression 11 | nudi implementaciju metoda Interpret pomocu kojeg interpretira neki jezik 12 | neterminalni izrazi su oni koji se dalje izvode u gramatici sve do terminalnih izraza 13 | koristi se globalno dostupan kontekst context da bi se izraz interpretirao 14 | npr. kontekst moze da sadrzi trenutne vrednosti nekih promenljivih i trenutni deo 15 | reci iz jezika koji treba da se interpretira */ 16 | class NonterminalExpression: public AbstractExpression { 17 | public: 18 | virtual void Interpret(Context* context) { 19 | cout << "NonterminalExpression.Interpret()" << endl; 20 | } 21 | }; 22 | 23 | #endif // __NONTERMINALEXPRESSION_H 24 | 25 | -------------------------------------------------------------------------------- /9_interpreteriterator/interpreter/ex1/Interpreter/TerminalExpression.h: -------------------------------------------------------------------------------- 1 | #ifndef __TERMINALEXPRESSION_H 2 | #define __TERMINALEXPRESSION_H 3 | 4 | #include 5 | #include "AbstractExpression.h" 6 | 7 | using namespace std; 8 | 9 | /* terminalni izraz iz gramatike koju ce interpereter da interpretira 10 | koji je izvedena klasa abstraktnog izraza / AbstractExpression 11 | nudi implementaciju metoda Interpret pomocu kojeg interpretira neki jezik 12 | terminalni izrazi su oni koji se vise ne izvode u gramatici 13 | koristi se globalno dostupan kontekst context da bi se izraz interpretirao 14 | npr. kontekst moze da sadrzi trenutne vrednosti nekih promenljivih i trenutni deo 15 | reci iz jezika koji treba da se interpretira */ 16 | class TerminalExpression: public AbstractExpression { 17 | public: 18 | virtual void Interpret(Context* context) { 19 | cout << "TerminalExpression.Interpret()" << endl; 20 | } 21 | }; 22 | 23 | #endif // __TERMINALEXPRESSION_H 24 | -------------------------------------------------------------------------------- /9_interpreteriterator/iterator/ex1/Iterator/ConcreteAggregate.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETEAGGREGATE_H 2 | #define __CONCRETEAGGREGATE_H 3 | 4 | #include "ConcreteIterator.h" 5 | 6 | /* konkretni agregat, cuva kolekciju u vektoru 7 | GetData jednostavno vraca referencu na vektor, 8 | a CreateIterator kreira konkretan iterator ConcreteIterator */ 9 | class ConcreteAggregate: public Aggregate { 10 | public: 11 | virtual Iterator* CreateIterator() { 12 | return new ConcreteIterator(this); 13 | } 14 | virtual vector& GetData() { 15 | return m_data; 16 | } 17 | virtual void Add(int v) { 18 | m_data.push_back(v); 19 | } 20 | protected: 21 | vector m_data; 22 | }; 23 | 24 | #endif // __CONCRETEAGGREGATE_H 25 | -------------------------------------------------------------------------------- /9_interpreteriterator/iterator/ex1/Iterator/ConcreteIterator.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONCRETEITERATOR_H 2 | #define __CONCRETEITERATOR_H 3 | 4 | #include "Iterator.h" 5 | 6 | /* konkretan iterator, implementira sve metode nasledjene 7 | iz klase Iterator koristeci internu strukturu agregata 8 | koju dobija pozivajuci zasticeni metod GetData */ 9 | class ConcreteIterator: public Iterator { 10 | public: 11 | ConcreteIterator(Aggregate* aggregate): Iterator(aggregate) { 12 | m_current = 0; 13 | } 14 | virtual ~ConcreteIterator() {} 15 | virtual void First() { 16 | m_current = 0; 17 | } 18 | virtual void Next() { 19 | if ((unsigned int) m_current < GetData().size()) m_current++; 20 | else throw("Next() Out of bounds."); 21 | } 22 | virtual bool IsDone() { 23 | return (unsigned int) m_current >= GetData().size(); 24 | } 25 | virtual int CurrentItem() { 26 | if ((unsigned int) m_current < GetData().size()) 27 | return GetData()[m_current]; 28 | throw("CurrentItem() Out of bounds."); 29 | } 30 | private: 31 | int m_current; 32 | }; 33 | 34 | #endif // __CONCRETEITERATOR_H 35 | -------------------------------------------------------------------------------- /9_interpreteriterator/iterator/ex1/Iterator/Iterator.h: -------------------------------------------------------------------------------- 1 | #ifndef __ITERATOR_H 2 | #define __ITERATOR_H 3 | 4 | #include 5 | 6 | #include "Aggregate.h" 7 | 8 | using namespace std; 9 | 10 | /* apstraktna klasa koju nasledjuju svi iteratori, 11 | ima zasticeni metod GetData koji nasleduju svi konkretni iteratori 12 | a koji im sluzi da pristupe internim podacima agregata i tako mogu 13 | da realizuju obilazak kolekcije 14 | interfejs iteratora nudi neke klasicne metode za iterator: 15 | First - da se postavi iterator (ili jos kursor) na pocetak agregata, 16 | Next - da predje na naredni element, IsDone - da pitamo da li smo 17 | dosli do kraja kolekcije, CurrentItem - vraca trenutni element 18 | kolekcije na kojoj se iterator/kursor nalazi */ 19 | class Iterator { 20 | public: 21 | Iterator(Aggregate* const aggregate): m_aggregate(aggregate) {} 22 | virtual ~Iterator() {} 23 | virtual void First() = 0; 24 | virtual void Next() = 0; 25 | virtual bool IsDone() = 0; 26 | virtual int CurrentItem() = 0; 27 | protected: 28 | Aggregate* m_aggregate; 29 | virtual vector& GetData() { 30 | return m_aggregate->GetData(); 31 | } 32 | }; 33 | #endif // __ITERATOR_H 34 | -------------------------------------------------------------------------------- /9_interpreteriterator/iterator/ex1/Iterator/Iterator.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /9_interpreteriterator/iterator/ex1/Iterator/IteratorMain.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "ConcreteAggregate.h" 4 | #include "Iterator.h" 5 | 6 | using namespace std; 7 | 8 | /* kreiramo konkretnu kolekciju, dodamo nekoliko elemenata, 9 | zatim kreiramo iterator koristeci fabricki metod CreateIterator 10 | i onda pokazemo elegantnost koriscenja iteratora u petlji for */ 11 | int main() { 12 | Aggregate *v = new ConcreteAggregate(); 13 | v->Add(2); 14 | v->Add(3); 15 | v->Add(5); 16 | 17 | Iterator* it; 18 | try { 19 | for (it = v->CreateIterator(); !it->IsDone(); it->Next()) { 20 | cout << it->CurrentItem() << endl; 21 | } 22 | } catch (const char* e) { 23 | cout << "ERROR: " << e << endl; 24 | } 25 | if (it != NULL) delete it; 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Alexander Shvets Design Patterns Explained Simply.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/Alexander Shvets Design Patterns Explained Simply.pdf -------------------------------------------------------------------------------- /Design Patterns, Elements of Reusable Object-Oriented Software.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/Design Patterns, Elements of Reusable Object-Oriented Software.pdf -------------------------------------------------------------------------------- /Lecture notes/01. Abstract factory/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Abstraktan proizovd 6 | class IModel{ 7 | public: 8 | virtual void crtaj() = 0; 9 | }; 10 | 11 | // Konkretan proizvod 1 12 | class Kocka : public IModel{ 13 | public: 14 | void crtaj(){cout << "Ja sam Kocka" <napraviKocku(); 51 | 52 | // Iz proizvoda pozivamo konkretan metod (koji je predefinisan u svakom proizvodu); 53 | kocka->crtaj(); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Lecture notes/04. Prototype/README.md: -------------------------------------------------------------------------------- 1 | # Prototip 2 | Služi za kreiranje kopija konkretnih objekta, uzimajuću u obzir preformanse. 3 | 4 | ## Klase 5 | Sastoji se od minimalno 1 apstrakne klase i kokretnih objekta. 6 | 7 | ### Apstaktan prototip (interface) 8 | Apstraktna klasa koja sadži *čisto virtualnu funkciju* **clone()**. 9 | 10 | Konkretno u ovom primeru naši prototipovi imaju neko polje `broj`, pošto je to privatana promenjiva potrebni su nam geteri i seteri. 11 | 12 | #### Primer: 13 | ``` 14 | class ApstraktPrototip{ 15 | private: 16 | int broj; 17 | public: 18 | virtual ApstraktPrototip* clone()=0; 19 | 20 | int getBroj(){ return broj; } 21 | void setBroj(int i){ broj = i; } 22 | } 23 | ``` 24 | 25 | ### Konkretan prototip (objekat) 26 | Kokretan objekat koji se izvodi iz **apstraknog prototipa**. Unutar ove klase redefinišemo **clone()** funkciju. 27 | 28 | #### Primer: 29 | ``` 30 | class Prototip1 : public ApstraktPrototip{ 31 | public: 32 | virtual ApstraktPrototip* clone(){ 33 | ApstraktPrototip* tmp = new Prototip1(); 34 | tmp->setBroj( getBroj() ); 35 | return tmp; 36 | } 37 | } 38 | ``` 39 | 40 | ## Diagram 41 | 42 | ![UML Diagram](https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Prototype_UML.svg/1200px-Prototype_UML.svg.png) 43 | -------------------------------------------------------------------------------- /Lecture notes/05. Singleton/README.md: -------------------------------------------------------------------------------- 1 | # Singleton 2 | Singleton je objekat koji se krejira samo **jedno** i nikad više. Obezbeđuje globalan pristup *(globalan objekat)*. 3 | 4 | Mogući je *lazi load* (lenje učitavnje) objekta. 5 | 6 | **Često ga koriste *apstraktna fabrika*, *graditelj* i *prototip*.** 7 | 8 | ## Klase 9 | 10 | Jedna konkretna klasa koja ima `private` ili `protected`: 11 | * Konstruktor 12 | * Konstruktor kopije 13 | * Operator dodele vrednosti 14 | 15 | ``` 16 | class Singleton{ 17 | public: 18 | static Singleton& get() { 19 | static Singleton single; 20 | return single; 21 | } 22 | 23 | private: 24 | Singleton() {} 25 | Singleton(const Singleton& drugi) {} 26 | Singleton& operator=(const Singleton& drugi) {} 27 | } 28 | ``` 29 | 30 | Objekat se poziva u ovom primeru metodom `get`. 31 | 32 | ``` 33 | Singleton& s = Singleton::get(); 34 | ``` 35 | 36 | ## Diagram: 37 | 38 | ![UML Diagram](https://sourcemaking.com/files/v2/content/patterns/singleton1.svg) 39 | 40 | ## Napomena: 41 | 42 | Moguca je i implementacija sa if-om unutar statičke metode 43 | 44 | ``` 45 | static Singleton& get() { 46 | if(single == null){ 47 | single = new Singleton(); 48 | } 49 | return single; 50 | ``` 51 | 52 | Ova verzija je takođe isprava ali nije **thread safe** u verziji C++03. Tako da 53 | bolje koristiti onaj primer odozogo. 54 | -------------------------------------------------------------------------------- /Lecture notes/06. Adapter/AdapterClass.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Imamo neki interfejst NoviOblik gde imamo metod nacrtajNovi() 3 | I imamo neku klasu StariOblik oblik sa metodom nactaj() 4 | 1. Treba napraviti adapter koji prilagodjava klasu StariOblik interfejsu NoviOblik 5 | 2. Uraditi Preko Nasledjivanje klasa 6 | */ 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | // Interface novi oblik (Target) 13 | class NoviOblik{ 14 | public: 15 | virtual void nacrajNovi()=0; 16 | }; 17 | 18 | // Adaptee (ono sto treba da se prebaci u Target) 19 | class StariOblik{ 20 | public: 21 | void nacrtaj(){ 22 | cout << "Crtam StariOblik" << endl; 23 | } 24 | }; 25 | 26 | 27 | // Konkretan novi oblik 28 | class Oblik : public NoviOblik{ 29 | public: 30 | void nacrajNovi(){ 31 | cout << "Crtam konkretan NoviOblik -> Oblik" << endl; 32 | } 33 | }; 34 | 35 | class Adapter: private StariOblik, public NoviOblik{ 36 | public: 37 | void nacrajNovi(){ 38 | this->nacrtaj(); 39 | } 40 | }; 41 | 42 | void crtanje(NoviOblik* n){ 43 | n->nacrajNovi(); 44 | }; 45 | 46 | int main(){ 47 | NoviOblik* o = new Adapter; 48 | NoviOblik* k = new Oblik; 49 | crtanje(o); 50 | crtanje(k); 51 | } 52 | -------------------------------------------------------------------------------- /Lecture notes/06. Adapter/AdapterObject.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | using namespace std; 5 | 6 | // Interface novi oblik (Target) 7 | class NoviOblik{ 8 | public: 9 | virtual void nacrajNovi()=0; 10 | }; 11 | 12 | // Adaptee (ono sto treba da se prebaci u Target) 13 | class StariOblik{ 14 | public: 15 | void nacrtaj(){ 16 | cout << "Crtam StariOblik" << endl; 17 | } 18 | }; 19 | 20 | // Konkretan novi oblik 21 | class Oblik : public NoviOblik{ 22 | public: 23 | void nacrajNovi(){ 24 | cout << "Crtam konkretan NoviOblik -> Oblik" << endl; 25 | } 26 | }; 27 | 28 | class Adapter: public NoviOblik{ 29 | private: 30 | StariOblik *stari; 31 | public: 32 | Adapter(StariOblik* s): stari(s){}; 33 | void nacrajNovi(){ 34 | stari->nacrtaj(); 35 | } 36 | }; 37 | 38 | void crtanje(NoviOblik* n){ 39 | n->nacrajNovi(); 40 | }; 41 | 42 | int main(){ 43 | StariOblik* s = new StariOblik(); 44 | NoviOblik* o = new Adapter(s); 45 | NoviOblik* k = new Oblik; 46 | crtanje(o); 47 | crtanje(k); 48 | } 49 | -------------------------------------------------------------------------------- /Lecture notes/06. Adapter/primer_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | //Jedan primer gde hocemo da prilagodimo uticnice. 7 | //Hocemo evropski prikljucak da ukljucimo u ameriku 8 | 9 | 10 | // Ovo je ono sto znamo 11 | class EuropeProd { 12 | public: 13 | void ukljuci220() { cout << "Ukljucen uredjaj 220v" << endl; } 14 | }; 15 | 16 | 17 | // Novi interface sa kojim treba da radi 18 | class USAProd { 19 | public: 20 | virtual void ukljuci() = 0; 21 | }; 22 | 23 | 24 | // Adapter, nasledjuje novi interface 25 | class Adapter :public USAProd { 26 | private: 27 | EuropeProd *evropa; 28 | public: 29 | Adapter(EuropeProd* e) : evropa(e){} 30 | void ukljuci() { 31 | evropa->ukljuci220(); 32 | } 33 | }; 34 | 35 | class Uticnica : public USAProd { 36 | public: 37 | void ukljuci() { cout << "Ukljucen uredjaj 110v" << endl; } 38 | }; 39 | 40 | int main() { 41 | USAProd* u = new Uticnica(); // Normalna uticnica 42 | USAProd* e = new Adapter(new EuropeProd()); 43 | 44 | 45 | u->ukljuci(); 46 | e->ukljuci(); 47 | } -------------------------------------------------------------------------------- /Lecture notes/08. Composite/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // srand, rand 4 | #include // time 5 | using namespace std; 6 | 7 | // Komponenta 8 | class Vojnik { 9 | public: 10 | virtual void napadni() = 0; 11 | virtual void odbrani() = 0; 12 | }; 13 | 14 | // List 15 | class Pesak : public Vojnik { 16 | public: 17 | void napadni() { cout << "Pesak napada" << endl; } 18 | void odbrani() { cout << "Pesak se brani" << endl; } 19 | }; 20 | 21 | // List 2 22 | class Strelac : public Vojnik { 23 | public: 24 | void napadni() { cout << "Strelac napada" << endl; } 25 | void odbrani() { cout << "Strelac se brani" << endl; } 26 | }; 27 | 28 | // Komponenta 29 | class Vod : public Vojnik { 30 | protected: 31 | vector vojska; // Ovde idu listovi 32 | public: 33 | void regrutuj(Vojnik* v) { 34 | vojska.push_back(v); 35 | } 36 | void napadni() { 37 | for(int i=0; i < vojska.size(); i++) 38 | vojska[i]->napadni(); 39 | } 40 | 41 | void odbrani() { 42 | for(int i=0; i < vojska.size(); i++) 43 | vojska[i]->odbrani(); 44 | } 45 | }; 46 | 47 | int main() { 48 | srand (time(NULL)); 49 | 50 | Vod* vod1 = new Vod(); 51 | 52 | for(int i = 0; i < 10; i++) { 53 | if(rand() % 2) 54 | vod1->regrutuj(new Strelac()); 55 | else 56 | vod1->regrutuj(new Pesak()); 57 | } 58 | 59 | vod1->napadni(); 60 | vod1->odbrani(); 61 | } -------------------------------------------------------------------------------- /Lecture notes/09. Decorator/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Komponenta 6 | class Polje { 7 | public: 8 | virtual void crtaj() = 0; 9 | }; 10 | 11 | // Subjekat koji treba da se adaptira 12 | class Input : public Polje { 13 | void crtaj() { 14 | cout << "Iscrtavam Input" << endl; 15 | } 16 | }; 17 | 18 | // Dekorater 19 | class Dekorater : public Polje { 20 | private: 21 | Polje* ptrPolje; 22 | public: 23 | Dekorater(Polje* _ptr): ptrPolje(_ptr){} 24 | void crtaj() { ptrPolje->crtaj(); } 25 | }; 26 | 27 | // Konkretan dekorater 28 | class InputIcon : public Dekorater { 29 | string icon; 30 | public: 31 | InputIcon(Polje* _p, string _i = "icon_user"): Dekorater(_p), icon(_i) {} 32 | void crtaj() { 33 | Dekorater::crtaj(); 34 | cout << " sa ikonicom " << icon << endl; 35 | } 36 | }; 37 | 38 | int main() { 39 | Polje* input = new Input(); 40 | input->crtaj(); 41 | 42 | Polje* ikonica = new InputIcon(input); 43 | ikonica->crtaj(); 44 | } -------------------------------------------------------------------------------- /Lecture notes/11. Flyweight/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // IMuva 7 | class Slovo { 8 | public: 9 | virtual void prikazi(int) = 0; // Extrinsic state 10 | virtual ~Slovo() {} 11 | protected: 12 | Slovo(char _simbol): simbol(_simbol){} 13 | // Intrinsic state 14 | char simbol; 15 | }; 16 | 17 | // Konkretna muva 18 | class SlovoA : public Slovo { 19 | public: 20 | SlovoA(): Slovo('A'){} 21 | void prikazi(int font) { 22 | cout << "Prikazujem slovo '" << simbol << "' font-size: " << font << "px" << endl; 23 | } 24 | }; 25 | 26 | class SlovoB : public Slovo { 27 | public: 28 | SlovoB(): Slovo('B'){} 29 | void prikazi(int font) { 30 | cout << "Prikazujem slovo '" << simbol << "' font-size: " << font << "px" << endl; 31 | } 32 | }; 33 | 34 | // Fabrika muva 35 | class Fabrika { 36 | public: 37 | Slovo* uzmiSlovo(char _slovo) { 38 | if(slova.find(_slovo) == slova.end()) { 39 | switch(_slovo) { 40 | case 'A': slova[_slovo] = new SlovoA(); 41 | case 'B': slova[_slovo] = new SlovoB(); 42 | } 43 | } 44 | return slova[_slovo]; 45 | } 46 | protected: 47 | map slova; 48 | }; 49 | 50 | int main() { 51 | Fabrika* azbuka = new Fabrika(); 52 | Slovo* slovo = azbuka->uzmiSlovo('A'); 53 | 54 | slovo->prikazi(36); 55 | } -------------------------------------------------------------------------------- /Partha Kuchana - Software Architecture Design Patterns in Java (2004).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/Partha Kuchana - Software Architecture Design Patterns in Java (2004).pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Design Patterns 2 | Currated list of reference materials and code examples of the design patterns in C++. 3 | 4 | -------------------------------------------------------------------------------- /SimplePatterns/abstractfactory.cpp: -------------------------------------------------------------------------------- 1 | #include "abstractfactory.h" 2 | 3 | template 4 | inline void Factory::Register(int id, Creator* ptrToCreator) { 5 | auto it = creatorRegister.find(id); 6 | if (it != creatorRegister.end()) { 7 | delete it->second; 8 | it->second = ptrToCreator; 9 | } else { 10 | creatorRegister[id] = ptrToCreator; 11 | } 12 | } 13 | 14 | template 15 | inline T * Factory::Create(int id) { return creatorRegister[id]->Create(); } 16 | 17 | template 18 | inline Factory::~Factory() { 19 | for (auto& it : creatorRegister) { 20 | delete it.second; 21 | } 22 | } 23 | 24 | void abstractfactory_test() { 25 | Factory temp; 26 | temp.Register(0, new DerivedCreator); 27 | temp.Register(1, new DerivedCreator); 28 | 29 | AbsProduct* product = 0; 30 | 31 | product = temp.Create(0); 32 | cout << "Product_A: " << product->Get() << endl; 33 | delete product; 34 | 35 | product = temp.Create(1); 36 | cout << "Product_B: " << product->Get() << endl; 37 | delete product; 38 | } -------------------------------------------------------------------------------- /SimplePatterns/abstractfactory.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class AbsProduct { 8 | public: 9 | virtual ~AbsProduct() {} 10 | virtual int Get() = 0; 11 | }; 12 | 13 | class Product_A : public AbsProduct { 14 | public: 15 | int Get() { return 525; } 16 | }; 17 | 18 | class Product_B : public AbsProduct { 19 | public: 20 | int Get() { return 6487; } 21 | }; 22 | 23 | template 24 | class Creator { 25 | public: 26 | virtual ~Creator() {} 27 | virtual T* Create() = 0; 28 | }; 29 | 30 | template 31 | class DerivedCreator : public Creator { 32 | public: 33 | virtual Base* Create() { return new Derived; } 34 | }; 35 | 36 | template 37 | class Factory { 38 | public: 39 | void Register(int id, Creator* ptrToCreator); 40 | T* Create(int id); 41 | ~Factory(); 42 | private: 43 | map*> creatorRegister; 44 | }; 45 | 46 | void abstractfactory_test(); 47 | -------------------------------------------------------------------------------- /SimplePatterns/adapter.cpp: -------------------------------------------------------------------------------- 1 | #include "adapter.h" 2 | 3 | #include 4 | 5 | void adapter_test() { 6 | vector printeri; 7 | printeri.push_back(new PrintableAdapter(new Printer())); 8 | printeri.push_back(new PrintableAdapter(new Fax())); 9 | printeri.push_back(new CompliantPrinter()); 10 | 11 | for (auto printer : printeri) printer->print("Hello, world!"); 12 | 13 | for (auto printer : printeri) delete printer; 14 | } 15 | 16 | void Printer::print() const { 17 | cout << "Print internal PrintJob" << endl; 18 | } 19 | 20 | void Fax::fax(const char * str) const { 21 | cout << "Fax & Print " << str << endl; 22 | } 23 | 24 | void CompliantPrinter::print(const char * str) const { 25 | cout << str << endl; 26 | } 27 | 28 | template 29 | inline void PrintableAdapter::print(const char * str) const { 30 | adapt(ptr, str); 31 | } 32 | 33 | template 34 | template 35 | void PrintableAdapter::adapt(U * t, const char * str) { 36 | t->print(str); 37 | } 38 | 39 | template 40 | void PrintableAdapter::adapt(Printer * t, const char *) { 41 | t->print(); 42 | } 43 | 44 | template 45 | void PrintableAdapter::adapt(Fax * t, const char * str) { 46 | t->fax(str); 47 | } 48 | -------------------------------------------------------------------------------- /SimplePatterns/adapter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | using namespace std; 5 | 6 | /* Lazin cuveni primer */ 7 | 8 | struct IPrintable { 9 | virtual void print(const char*) const = 0; 10 | virtual ~IPrintable() {} 11 | }; 12 | 13 | struct Printer { 14 | void print() const; 15 | }; 16 | 17 | struct Fax { 18 | void fax(const char* str) const; 19 | }; 20 | 21 | struct CompliantPrinter : public IPrintable { 22 | virtual void print(const char* str) const; 23 | }; 24 | 25 | template 26 | class PrintableAdapter : public IPrintable { 27 | public: 28 | PrintableAdapter(T* ptr) : ptr(ptr) {} 29 | virtual void print(const char* str) const; 30 | virtual ~PrintableAdapter() { delete ptr; } 31 | private: 32 | template 33 | static void adapt(U* t, const char* str); 34 | 35 | static void adapt(Printer* t, const char*); 36 | 37 | static void adapt(Fax* t, const char* str); 38 | 39 | T* ptr; 40 | }; 41 | 42 | void adapter_test(); 43 | -------------------------------------------------------------------------------- /SimplePatterns/builder.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | /* Racunar, kaci mu se deo po deo */ 9 | class Racunar { 10 | public: 11 | void setKomponenta(const string& deo); 12 | void start(); 13 | protected: 14 | vector komponente; 15 | }; 16 | 17 | /* Interfejs graditelja racunara */ 18 | class GraditeljRacunara { 19 | public: 20 | virtual void zapocniGradnju() = 0; 21 | virtual void napraviProcesor() = 0; 22 | virtual void napraviMaticnu() = 0; 23 | virtual void napraviMonitor() = 0; 24 | virtual Racunar* vratiRezultat() = 0; 25 | }; 26 | 27 | /* Primer kako treba da izgleda direktor gradnje */ 28 | 29 | class Direktor { 30 | public: 31 | Direktor() : ptr(0) {} 32 | ~Direktor(); 33 | void setGraditeljRacunara(GraditeljRacunara* ptr); 34 | Racunar* getRacunar(); 35 | private: 36 | GraditeljRacunara* ptr; 37 | }; 38 | 39 | /* Jedan konkretni graditelj koji implementira interfejs */ 40 | class DesktopGraditelj : public GraditeljRacunara { 41 | public: 42 | DesktopGraditelj() : ptr(0) {} 43 | virtual void zapocniGradnju(); 44 | virtual void napraviProcesor(); 45 | virtual void napraviMaticnu(); 46 | virtual void napraviMonitor(); 47 | virtual Racunar* vratiRezultat(); 48 | protected: 49 | Racunar* ptr; 50 | }; 51 | 52 | /* Mrzi me da pravim drugog graditelja, you get the idea */ 53 | 54 | void builder_test(); -------------------------------------------------------------------------------- /SimplePatterns/chainofresponsibility.cpp: -------------------------------------------------------------------------------- 1 | #include "chainofresponsibility.h" 2 | 3 | template 4 | void RadnikL::ObradiUpit(Upit upit) { 5 | if (upit.NivoUpita() <= nivo) { 6 | /* Trenutni objekat u lancu JESTE sposoban da da odgovor */ 7 | cout << "Odgovor na pitanje: " << upit.StaJeUpit().c_str() 8 | << " je dao " << Ime().c_str() << endl; 9 | return; 10 | } 11 | /* Trenutni objekat u lancu NIJE sposoban da da odgovor */ 12 | cout << "Osoba " << Ime().c_str() 13 | << " ne moze da obradi trenutni upit. Upit se salje na dalju obradu: " 14 | << (PtrToSef()->Ime()).c_str() << endl; 15 | /* Zahtev se salje na dalju obradu sledecem objektu u lancu odgovornosti */ 16 | PtrToSef()->ObradiUpit(upit); 17 | } 18 | 19 | 20 | void chainofresponsibility_test() { 21 | RadnikL rad("Petar Djuric"); 22 | RadnikL sup("Vojislav Mitrovic"); 23 | RadnikL sef("Miroslav Stevanovic"); 24 | rad.PostaviSefa(&sup); 25 | sup.PostaviSefa(&sef); 26 | sef.PostaviSefa(0); 27 | 28 | Upit q1("Mozes li da zavrsis ovaj zadatak?", NIZAK); 29 | Upit q2("Da li tim moze da zavrsi zadatak na vreme?", SREDNJI); 30 | Upit q3("Da li odeljenje proizvodnje moze da zavrsi zadatak?", VISOK); 31 | 32 | rad.ObradiUpit(q1); 33 | rad.ObradiUpit(q2); 34 | rad.ObradiUpit(q3); 35 | } -------------------------------------------------------------------------------- /SimplePatterns/chainofresponsibility.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | using namespace std; 5 | 6 | enum ENivoOdgovornosti { NIZAK = 1, SREDNJI, VISOK }; 7 | 8 | class Upit { 9 | private: 10 | ENivoOdgovornosti m_nivo; 11 | string m_upit; 12 | public: 13 | Upit(string upit, ENivoOdgovornosti nivo) : m_upit(upit), m_nivo(nivo) {} 14 | ENivoOdgovornosti NivoUpita() const { return m_nivo; } 15 | string StaJeUpit() const { return m_upit; } 16 | }; 17 | 18 | class IOsoba { 19 | private: 20 | string m_ime; 21 | IOsoba* m_sef; 22 | public: 23 | IOsoba(string ime) :m_ime(ime) {}; 24 | 25 | virtual void ObradiUpit(Upit upit) = 0; 26 | virtual string Ime() const { return m_ime; } 27 | virtual IOsoba* PtrToSef() const { return m_sef; } 28 | virtual void PostaviSefa(IOsoba* sef) { m_sef = sef; } 29 | 30 | /* Objekat nije odogovoran za unistavanje sledeceg clana u lancu odgovornosti */ 31 | virtual ~IOsoba() {} 32 | }; 33 | 34 | template 35 | class RadnikL : public IOsoba { 36 | public: 37 | RadnikL(string ime) : IOsoba(ime) {} 38 | virtual void ObradiUpit(Upit upit); 39 | }; 40 | 41 | void chainofresponsibility_test(); -------------------------------------------------------------------------------- /SimplePatterns/composite.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | /* Komponenta (interfejs) */ 9 | class Komponenta { 10 | public: 11 | Komponenta(string ime, double plata) : m_Ime(ime), m_plata(plata) {} 12 | virtual void getPlata(int level) const = 0; 13 | protected: 14 | string m_Ime; 15 | double m_plata; 16 | }; 17 | 18 | /* List hijerarhije - obican radnik */ 19 | class Radnik : public Komponenta { 20 | public: 21 | Radnik(string ime, double plata) : Komponenta(ime, plata) {} 22 | void getPlata(int level) const; 23 | }; 24 | 25 | /* Kompozit */ 26 | class Menadzer : public Komponenta { 27 | public: 28 | Menadzer(string ime, double plata) : Komponenta(ime, plata) {} 29 | void add(Komponenta *cmp); 30 | void getPlata(int nivo) const; 31 | private: 32 | list m_tim; 33 | }; 34 | 35 | void composite_test(); -------------------------------------------------------------------------------- /SimplePatterns/facade.cpp: -------------------------------------------------------------------------------- 1 | #include "facade.h" 2 | 3 | #include 4 | using namespace std; 5 | 6 | void facade_test() { 7 | cout << "Pozivam ostale funkcije..." << endl; 8 | Facade f; 9 | f.Operation1(); 10 | f.Operation2(); 11 | } 12 | 13 | string SubsystemA::OperationA1() { 14 | return "Subsystem A, Method A1\n"; 15 | } 16 | 17 | string SubsystemA::OperationA2() { 18 | return "Subsystem A, Method A2\n"; 19 | } 20 | 21 | string SubsystemB::OperationB1() { 22 | return "Subsystem B, Method B1\n"; 23 | } 24 | 25 | string SubsystemB::OperationB2() { 26 | return "Subsystem B, Method B2\n"; 27 | } 28 | 29 | string SubsystemC::OperationC1() { 30 | return "Subsystem C, Method C1\n"; 31 | } 32 | 33 | string SubsystemC::OperationC2() { 34 | return "Subsystem C, Method C2\n"; 35 | } 36 | 37 | void Facade::Operation1() { 38 | cout << ("Operation 1\n" + 39 | a->OperationA1() + 40 | a->OperationA2() + 41 | b->OperationB1()); 42 | } 43 | 44 | void Facade::Operation2() { 45 | cout << ("Operation 2\n" + 46 | b->OperationB2() + 47 | c->OperationC1() + 48 | c->OperationC2()); 49 | } 50 | -------------------------------------------------------------------------------- /SimplePatterns/facade.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | /* Jedan veoma cheesy primer uzet direktno sa wikipedije */ 8 | 9 | struct SubsystemA { 10 | string OperationA1(); 11 | string OperationA2(); 12 | }; 13 | 14 | struct SubsystemB { 15 | string OperationB1(); 16 | string OperationB2(); 17 | }; 18 | 19 | struct SubsystemC { 20 | string OperationC1(); 21 | string OperationC2(); 22 | }; 23 | 24 | class Facade { 25 | SubsystemA* a; 26 | SubsystemB* b; 27 | SubsystemC* c; 28 | public: 29 | Facade() : a(new SubsystemA()), b(new SubsystemB()), c(new SubsystemC()) 30 | {} 31 | void Operation1(); 32 | void Operation2(); 33 | }; 34 | 35 | void facade_test(); -------------------------------------------------------------------------------- /SimplePatterns/factorymethod.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class INalivPero { 8 | public: 9 | virtual string Pisi() = 0; 10 | }; 11 | 12 | class NalivPeroZlatno : public INalivPero { 13 | public: 14 | string Pisi(); 15 | }; 16 | 17 | class NalivPeroCelicno : public INalivPero{ 18 | public: 19 | string Pisi(); 20 | }; 21 | 22 | template 23 | class Fabrika { 24 | public: 25 | /* Funkcija koja sluzi za registrovanje fabrickih metoda za 26 | razlicite tipove Tizveden koji moraju da nasledjuju tip T */ 27 | template 28 | void registrujTip(int IDTipa); 29 | 30 | /* Javna funkcija koja je zaduzena za pravljenje objekta na osnovu 31 | identifikatora njegovog tipa IDTipa. Preko ove funkcije Klijent trazi 32 | od fabrike da pozove odgovarajucu fabricki metod za tip koji je on 33 | identifikovao kljucem, tj. Identifikatorom IDTipa */ 34 | T* napravi(int IDTipa); 35 | 36 | private: 37 | typedef T* (*funkcija)(); 38 | 39 | map fabrickiMetod; 40 | 41 | /* Staticki sablon Fabrickog Metoda, koji nije deo interfejsa vec 42 | predstavlja detalj implementacije sablona Fabrike. Instancira se 43 | prilikom registrovanja za razlicite tipove objekata, koji moraju 44 | da budu izvedeni iz T */ 45 | template 46 | static T* FabrickiMetod(); 47 | }; 48 | 49 | void factorymethod_test(); 50 | -------------------------------------------------------------------------------- /SimplePatterns/flyweight.cpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "flyweight.h" 4 | 5 | StringFlyweight::operator string& () const { 6 | return FlyweightFactory::strs[id]; 7 | } 8 | 9 | string FlyweightFactory::strs[256]; 10 | 11 | uint8_t FlyweightFactory::cnt = 0; 12 | 13 | void flyweight_test() { 14 | auto x = FlyweightFactory::add("Ja"); 15 | auto y = FlyweightFactory::add("Sam"); 16 | auto z = FlyweightFactory::add("Ivan"); 17 | 18 | for (string& i : { x, y, z }) { 19 | cout << i << endl; 20 | } 21 | } 22 | 23 | StringFlyweight FlyweightFactory::add(const string & str) { 24 | strs[cnt] = str; 25 | return StringFlyweight{ cnt++ }; 26 | } 27 | -------------------------------------------------------------------------------- /SimplePatterns/flyweight.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | /* Tiny string handle - only 1 byte */ 9 | 10 | struct StringFlyweight { 11 | uint8_t id; 12 | operator string& () const; 13 | }; 14 | 15 | class FlyweightFactory { 16 | static string strs[256]; 17 | static uint8_t cnt; 18 | public: 19 | static StringFlyweight add(const string& str); 20 | friend struct StringFlyweight; 21 | }; 22 | 23 | void flyweight_test(); -------------------------------------------------------------------------------- /SimplePatterns/interpreter.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andjelatodorovic/DesignPatterns/260ed4ff701da4cafbf182056e9bfc68363bfc70/SimplePatterns/interpreter.h -------------------------------------------------------------------------------- /SimplePatterns/main.cpp: -------------------------------------------------------------------------------- 1 | #include "abstractfactory.h" 2 | #include "singleton.h" 3 | #include "factorymethod.h" 4 | #include "prototype.h" 5 | #include "adapter.h" 6 | #include "builder.h" 7 | #include "bridge.h" 8 | #include "composite.h" 9 | #include "decorator.h" 10 | #include "facade.h" 11 | #include "proxy.h" 12 | #include "flyweight.h" 13 | #include "chainofresponsibility.h" 14 | #include "command.h" 15 | #include "observer.h" 16 | #include "mediator.h" 17 | #include "visitor.h" 18 | #include "memento.h" 19 | #include "state.h" 20 | #include "strategy.h" 21 | #include "templatemethod.h" 22 | #include "iterator.h" 23 | #include "interpreter.h" 24 | 25 | int main() { 26 | abstractfactory_test(); 27 | singleton_test(); 28 | factorymethod_test(); 29 | prototype_test(); 30 | adapter_test(); 31 | builder_test(); 32 | bridge_test(); 33 | composite_test(); 34 | decorator_test(); 35 | facade_test(); 36 | proxy_test(); 37 | flyweight_test(); 38 | chainofresponsibility_test(); 39 | command_test(); 40 | observer_test(); 41 | mediator_test(); 42 | visitor_test(); 43 | memento_test(); 44 | state_test(); 45 | strategy_test(); 46 | templatemethod_test(); 47 | iterator_test(); 48 | interpreter_test(); 49 | 50 | system("pause"); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /SimplePatterns/mediator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | /* Interfejs posrednika */ 8 | class IPosrednik { 9 | public: 10 | virtual void PosaljiPoruku(size_t IDSaradnika, const string& message) = 0; 11 | virtual ~IPosrednik() {} 12 | }; 13 | 14 | /* Klasa objekata koji ce komunicirati preko posrednika */ 15 | class Saradnik { 16 | IPosrednik* m_ptrPosrednik; 17 | size_t m_ID; 18 | string m_ime; 19 | public: 20 | Saradnik(size_t ID, string ime) : m_ptrPosrednik(0), 21 | m_ID(ID), m_ime(ime) {} 22 | size_t getID() const { return m_ID; } 23 | /* Funkcija kojom se objekat vezuje za posrednika */ 24 | void RegistrujPosrednika(IPosrednik &p) { m_ptrPosrednik = &p; } 25 | /* Akcije koje mogu da se izvrsavaju nad objektima klase Saradnik */ 26 | void PrimiPoruku(const string &poruka); 27 | void PosaljiPoruku(size_t ID, const string &poruka); 28 | }; 29 | 30 | /* Konkretna klasa posrednika */ 31 | class Posrednik : public IPosrednik { 32 | Saradnik **ptrSaradnik; 33 | public: 34 | Posrednik(size_t num); 35 | /* Funkcija kojom se posrednik vezuje za saradnika */ 36 | void RegistrujSaradnika(Saradnik& s); 37 | /* Funkcija kojom se implementira komunikacija sa objektima izmedju kojih posredje */ 38 | void PosaljiPoruku(size_t IDSaradnika, const string& poruka); 39 | virtual ~Posrednik() { delete[] ptrSaradnik; } 40 | }; 41 | 42 | void mediator_test(); -------------------------------------------------------------------------------- /SimplePatterns/proxy.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | using namespace std; 5 | 6 | template 7 | class SmartPtr { 8 | T* ptr; 9 | size_t* br; 10 | void detach(); 11 | public: 12 | SmartPtr(T* ptr) : ptr(ptr), br(new size_t(1)) {} 13 | SmartPtr(const SmartPtr& other) : ptr(ptr), br(br) { ++*br; } 14 | SmartPtr& operator= (const SmartPtr& other); 15 | T* operator-> () { return ptr; } 16 | T& operator* () { return *ptr; } 17 | const T* operator-> () const { return ptr; } 18 | const T& operator* () const { return *ptr; } 19 | ~SmartPtr() { detach(); } 20 | }; 21 | 22 | struct Proxyn { 23 | Proxyn(int x); 24 | ~Proxyn(); 25 | }; 26 | 27 | void proxy_test(); 28 | 29 | -------------------------------------------------------------------------------- /SimplePatterns/singleton.cpp: -------------------------------------------------------------------------------- 1 | #include "singleton.h" 2 | #include 3 | 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | Singleton& Singleton::Instance() { 9 | //lokalna staticka promenljiva 10 | this_thread::sleep_for(500ms); 11 | static Singleton inst; 12 | return inst; 13 | } 14 | 15 | void Singleton::Operation() { 16 | cout << "Ja sam dobar! Moja adresa: " << this << endl; 17 | } 18 | 19 | LosSingleton& LosSingleton::Instance() { 20 | if (ptr == 0) { 21 | this_thread::sleep_for(500ms); 22 | ptr = new LosSingleton; 23 | } 24 | return *ptr; 25 | } 26 | 27 | void LosSingleton::Operation() { 28 | cout << "Ja sam los! Moja adresa: " << this << endl; 29 | } 30 | 31 | LosSingleton* LosSingleton::ptr = 0; 32 | 33 | void koristi_los_singleton() { 34 | LosSingleton::Instance().Operation(); 35 | } 36 | 37 | void koristi_dobar_singleton() { 38 | Singleton::Instance().Operation(); 39 | } 40 | 41 | void singleton_test() { 42 | 43 | thread t1(koristi_los_singleton), t2(koristi_los_singleton); 44 | 45 | thread t3(koristi_dobar_singleton), t4(koristi_dobar_singleton); 46 | 47 | t1.join(); 48 | t2.join(); 49 | t3.join(); 50 | t4.join(); 51 | } -------------------------------------------------------------------------------- /SimplePatterns/singleton.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Singleton { 4 | public: 5 | static Singleton& Instance(); 6 | virtual void Operation(); 7 | protected: 8 | Singleton() {} 9 | ~Singleton() {} 10 | private: 11 | Singleton(const Singleton&); 12 | Singleton& operator=(const Singleton&) = default; 13 | }; 14 | 15 | class LosSingleton { 16 | public: 17 | static LosSingleton& Instance(); 18 | virtual void Operation(); 19 | protected: 20 | LosSingleton() {} 21 | ~LosSingleton() {} 22 | private: 23 | LosSingleton(const LosSingleton&); 24 | LosSingleton& operator=(const LosSingleton&) = default; 25 | static LosSingleton* ptr; 26 | }; 27 | 28 | void singleton_test(); 29 | -------------------------------------------------------------------------------- /SimplePatterns/state.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | /* Morbidna adaptacija Banetovog primera */ 4 | 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | class ICovek { 11 | public: 12 | virtual void dajEbolu() = 0; 13 | virtual void cekaj() = 0; 14 | virtual void dajLek() = 0; 15 | virtual ~ICovek() {} 16 | }; 17 | 18 | class ZdravCovek : public ICovek { 19 | public: 20 | virtual void dajEbolu(); 21 | virtual void cekaj(); 22 | virtual void dajLek(); 23 | }; 24 | 25 | class CovekSaEbolom : public ICovek { 26 | public: 27 | virtual void dajEbolu(); 28 | virtual void cekaj(); 29 | virtual void dajLek(); 30 | }; 31 | 32 | class MrtavCovek : public ICovek { 33 | public: 34 | virtual void dajEbolu(); 35 | virtual void cekaj(); 36 | virtual void dajLek(); 37 | }; 38 | 39 | class Covek : public ICovek { 40 | // stanje coveka 41 | unique_ptr stanje; 42 | public: 43 | virtual void dajEbolu(); 44 | virtual void cekaj(); 45 | virtual void dajLek(); 46 | Covek() : stanje(unique_ptr(new ZdravCovek)) {} 47 | }; 48 | 49 | void state_test(); -------------------------------------------------------------------------------- /SimplePatterns/strategy.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | class IAlgoritamSortiranja { 10 | public: 11 | virtual void sortiraj(vector& niz) = 0; 12 | }; 13 | 14 | class Sorter { 15 | unique_ptr algo; 16 | vector niz; 17 | public: 18 | void dodajBroj(size_t x); 19 | vector dajSortirani(); 20 | void postaviSorter(IAlgoritamSortiranja* algo); 21 | }; 22 | 23 | class STLSort : public IAlgoritamSortiranja { 24 | public: 25 | virtual void sortiraj(vector& niz); 26 | }; 27 | 28 | class CountingSort : public IAlgoritamSortiranja { 29 | public: 30 | virtual void sortiraj(vector& niz); 31 | }; 32 | 33 | void strategy_test(); -------------------------------------------------------------------------------- /SimplePatterns/templatemethod.cpp: -------------------------------------------------------------------------------- 1 | #include "templatemethod.h" 2 | 3 | void Ispit::odradiIspit() { 4 | ispuniPredispitneObaveze(); 5 | poloziPismeniIspit(); 6 | poloziUsmeniIspit(); 7 | } 8 | 9 | inline void IspitIzOOP2::ispuniPredispitneObaveze() { 10 | cout << "Polazem prvi kolokvijum iz OOP2." << endl; 11 | cout << "Polazem drugi kolokvijum iz OOP2." << endl; 12 | } 13 | 14 | inline void IspitIzOOP2::poloziPismeniIspit() { 15 | cout << "Polozio sam oba kolokvijuma, ne moram da radim pismeni ispit." << endl; 16 | } 17 | 18 | inline void IspitIzOOP2::poloziUsmeniIspit() { 19 | cout << "Polazem usmeni ispit na papiru kod Baneta." << endl; 20 | } 21 | 22 | inline void IspitIzUBP::ispuniPredispitneObaveze() { 23 | cout << "Polazem prvi kolokvijum u Access-u." << endl; 24 | cout << "Polazem drugi kolokvijum u MySQL-u." << endl; 25 | cout << "Radim projekat." << endl; 26 | } 27 | 28 | inline void IspitIzUBP::poloziPismeniIspit() { 29 | cout << "Polazem pismeni ispit (Access + MySQL)." << endl; 30 | } 31 | 32 | inline void IspitIzUBP::poloziUsmeniIspit() { 33 | cout << "Profesor me nije oslobodio, radim zavrsni test." << endl; 34 | } 35 | 36 | void templatemethod_test() { 37 | IspitIzOOP2 kodBaneta; 38 | IspitIzUBP kodTasica; 39 | 40 | kodBaneta.odradiIspit(); 41 | kodTasica.odradiIspit(); 42 | } -------------------------------------------------------------------------------- /SimplePatterns/templatemethod.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | /* Jako cheesy primer, ali lepo ilustruje poentu */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | class Ispit { 9 | protected: 10 | virtual void ispuniPredispitneObaveze() = 0; 11 | virtual void poloziPismeniIspit() = 0; 12 | virtual void poloziUsmeniIspit() = 0; 13 | public: 14 | virtual ~Ispit() {} 15 | void odradiIspit(); 16 | }; 17 | 18 | class IspitIzOOP2 : public Ispit { 19 | protected: 20 | virtual void ispuniPredispitneObaveze(); 21 | virtual void poloziPismeniIspit(); 22 | virtual void poloziUsmeniIspit(); 23 | }; 24 | 25 | class IspitIzUBP : public Ispit { 26 | protected: 27 | virtual void ispuniPredispitneObaveze(); 28 | virtual void poloziPismeniIspit(); 29 | virtual void poloziUsmeniIspit(); 30 | }; 31 | 32 | /* Implementirati primer sa sablonskim operatorima poredjenja */ 33 | 34 | void templatemethod_test(); -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-slate -------------------------------------------------------------------------------- /stl/algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | typedef vector VInt; 8 | typedef VInt::iterator VIntIter; 9 | 10 | 11 | void odstampaj(vector &v) { 12 | /* 13 | for (int i = 0; i < v.size(); i++) { 14 | cout << v[i] << " "; 15 | } 16 | */ 17 | for (VIntIter it = v.begin(); it != v.end(); ++it) { 18 | cout << *it << " "; 19 | } 20 | cout << endl; 21 | } 22 | 23 | int main() { 24 | vector v; 25 | v.push_back(1); 26 | v.push_back(2); 27 | v.push_back(3); 28 | 29 | v.pop_back(); 30 | v.insert(v.begin() + 1, 5); 31 | v.insert(v.begin() + 1, 6); 32 | v.insert(v.begin() + 1, 7); 33 | v.insert(v.begin() + 1, 8); 34 | odstampaj(v); 35 | vector::iterator it = find(v.begin(), v.end(), 5); 36 | if (it == v.end()) cout << "Nema elementa u nizu" << endl; 37 | else cout << "Pozicija " << (it - v.begin()) << endl; 38 | sort(v.begin(), v.end()); 39 | reverse(v.begin(), v.end()); 40 | odstampaj(v); 41 | 42 | return 0; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /stl/lista.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void prikazi(list L) { 7 | for (list::iterator it = L.begin();it != L.end(); ++it) { 8 | cout << *it << " "; 9 | } 10 | cout << endl; 11 | } 12 | 13 | int main() { 14 | list L; 15 | L.push_front(1); 16 | L.push_front(2); 17 | L.push_front(3); 18 | L.push_back(4); 19 | L.push_back(5); 20 | L.push_back(6); 21 | list P; 22 | P.push_front(1); 23 | P.push_front(2); 24 | P.push_front(3); 25 | P.push_back(4); 26 | P.push_back(5); 27 | P.push_back(6); 28 | L.sort(); 29 | P.sort(); 30 | prikazi(L); 31 | prikazi(P); 32 | L.merge(P); 33 | prikazi(L); 34 | L.remove(3); 35 | prikazi(L); 36 | L.unique(); 37 | prikazi(L); 38 | L.reverse(); 39 | prikazi(L); 40 | L.pop_back(); 41 | L.pop_front(); 42 | prikazi(L); 43 | L.sort(); 44 | prikazi(L); 45 | } 46 | -------------------------------------------------------------------------------- /stl/mapa.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef map MojaMapa; 7 | typedef MojaMapa::iterator MapaIter; 8 | 9 | void prikazi(map mapa) { 10 | for (MapaIter it = mapa.begin(); it != mapa.end(); ++it) { 11 | cout << "(" << it->first << "," << it->second << ") "; 12 | } 13 | cout << endl; 14 | } 15 | 16 | int main() { 17 | map so; 18 | so["Pera"] = 5; 19 | so["Zika"] = 4; 20 | so["Mika"] = 2; 21 | so["Aca"] = 4; 22 | prikazi(so); 23 | /* 24 | so.erase("Mika"); 25 | prikazi(so); 26 | so.erase(so.begin(), ++(++so.begin())); 27 | prikazi(so); 28 | */ 29 | MapaIter it = so.lower_bound("Aca"); 30 | cout << it->first << "," << it->second << endl; 31 | 32 | if (so.find("Aca") != so.end()) { 33 | cout << so["Aca"] << endl; 34 | } else { 35 | cout << "Nema Ace u mapi" << endl; 36 | } 37 | so.clear(); 38 | cout << so.size() << endl; 39 | 40 | cout << so.empty() << endl; 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /stl/queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | queue q; 8 | q.push(1); 9 | q.push(2); 10 | q.push(3); 11 | q.push(4); 12 | cout << q.size() << "," << q.empty() << endl; 13 | cout << q.front() << "," << q.back() << endl; 14 | q.pop(); 15 | cout << q.front() << endl; 16 | 17 | priority_queue pq; 18 | pq.push(4); 19 | pq.push(7); 20 | pq.push(1); 21 | pq.push(3); 22 | pq.push(8); 23 | cout << pq.top() << endl; 24 | pq.pop(); 25 | cout << pq.top() << endl; 26 | pq.pop(); 27 | cout << pq.top() << endl; 28 | pq.pop(); 29 | cout << pq.top() << endl; 30 | pq.pop(); 31 | } 32 | -------------------------------------------------------------------------------- /stl/smart_pointers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class C { 7 | public: 8 | string m_a; 9 | C(string a): m_a(a) { cout << "Konstruktor " << a << endl;} 10 | virtual ~C() { cout << "Brisem " << m_a << endl; } 11 | }; 12 | 13 | int main() { 14 | shared_ptr sc = make_shared("A"); 15 | shared_ptr pc = (shared_ptr) new C("B"); 16 | pc = make_shared("C"); 17 | cout << "X" << endl; 18 | shared_ptr t = sc; 19 | } 20 | -------------------------------------------------------------------------------- /stl/stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | stack s; 8 | s.push(1); 9 | s.push(2); 10 | s.push(3); 11 | s.push(4); 12 | cout << s.size() << "," << s.empty() << endl; 13 | cout << s.top() << endl; 14 | s.pop(); 15 | cout << s.top() << endl; 16 | } 17 | -------------------------------------------------------------------------------- /stl/vector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | typedef vector VInt; 8 | typedef VInt::iterator VIntIter; 9 | 10 | 11 | void odstampaj(vector &v) { 12 | /* 13 | for (int i = 0; i < v.size(); i++) { 14 | cout << v[i] << " "; 15 | } 16 | */ 17 | for (VIntIter it = v.begin(); it != v.end(); ++it) { 18 | cout << *it << " "; 19 | } 20 | cout << endl; 21 | } 22 | 23 | int main() { 24 | vector v; 25 | v.push_back(1); 26 | v.push_back(2); 27 | v.push_back(3); 28 | 29 | v.pop_back(); 30 | odstampaj(v); 31 | v.insert(v.begin() + 1, 5); 32 | v.insert(v.begin() + 1, 6); 33 | v.insert(v.begin() + 1, 7); 34 | v.insert(v.begin() + 1, 8); 35 | odstampaj(v); 36 | v.erase(v.begin() + 3); 37 | odstampaj(v); 38 | v.erase(v.begin()+1, v.begin()+3); 39 | odstampaj(v); 40 | cout << v.front() << "," << v.back() << endl; 41 | v.clear(); 42 | odstampaj(v); 43 | v.push_back(10); 44 | odstampaj(v); 45 | 46 | 47 | return 0; 48 | } 49 | 50 | --------------------------------------------------------------------------------