├── .gitignore ├── courses └── guru │ ├── js │ ├── .gitignore │ ├── patterns │ │ ├── command │ │ │ ├── invokers │ │ │ │ ├── index.js │ │ │ │ └── invoker1.js │ │ │ ├── receivers │ │ │ │ ├── index.js │ │ │ │ └── receiver1.js │ │ │ ├── commands │ │ │ │ ├── index.js │ │ │ │ ├── simple.js │ │ │ │ └── complex.js │ │ │ └── index.js │ │ ├── singleton │ │ │ ├── index.js │ │ │ └── singleton.js │ │ ├── iterator │ │ │ ├── index.js │ │ │ └── collection1 │ │ │ │ └── index.js │ │ ├── mediator │ │ │ ├── mediators │ │ │ │ ├── index.js │ │ │ │ └── mediator1.js │ │ │ ├── components │ │ │ │ ├── index.js │ │ │ │ ├── component.js │ │ │ │ ├── component1.js │ │ │ │ └── component2.js │ │ │ └── index.js │ │ ├── observer │ │ │ ├── subjects │ │ │ │ ├── index.js │ │ │ │ └── subject1.js │ │ │ ├── observers │ │ │ │ ├── index.js │ │ │ │ ├── observer1.js │ │ │ │ └── observer2.js │ │ │ └── index.js │ │ ├── simple-factory │ │ │ ├── index.js │ │ │ ├── factory.js │ │ │ └── product.js │ │ ├── flyweight │ │ │ ├── index.js │ │ │ └── flyweight.js │ │ ├── proxy │ │ │ ├── index.js │ │ │ ├── subject.js │ │ │ └── proxy.js │ │ ├── template-method │ │ │ ├── index.js │ │ │ └── operations │ │ │ │ ├── index.js │ │ │ │ ├── operation1.js │ │ │ │ ├── operation2.js │ │ │ │ └── operation.js │ │ ├── abstract-factory │ │ │ ├── index.js │ │ │ ├── products │ │ │ │ ├── productA1.js │ │ │ │ ├── productA2.js │ │ │ │ ├── index.js │ │ │ │ ├── productB1.js │ │ │ │ └── productB2.js │ │ │ ├── factory1 │ │ │ │ └── index.js │ │ │ └── factory2 │ │ │ │ └── index.js │ │ ├── prototype │ │ │ ├── index.js │ │ │ ├── component.js │ │ │ └── prototype.js │ │ ├── state │ │ │ ├── states │ │ │ │ ├── index.js │ │ │ │ ├── state.js │ │ │ │ ├── state1.js │ │ │ │ └── state2.js │ │ │ ├── index.js │ │ │ └── context.js │ │ ├── factory-method │ │ │ ├── index.js │ │ │ ├── creator1 │ │ │ │ ├── product1.js │ │ │ │ └── creator1.js │ │ │ ├── creator2 │ │ │ │ ├── product2.js │ │ │ │ └── creator2.js │ │ │ └── creator.js │ │ ├── memento │ │ │ ├── index.js │ │ │ ├── memento.js │ │ │ └── caretaker.js │ │ ├── visitor │ │ │ ├── visitors │ │ │ │ ├── index.js │ │ │ │ ├── visitor1.js │ │ │ │ └── visitor2.js │ │ │ ├── components │ │ │ │ ├── index.js │ │ │ │ ├── component1.js │ │ │ │ └── component2.js │ │ │ └── index.js │ │ ├── strategy │ │ │ ├── strategies │ │ │ │ ├── index.js │ │ │ │ ├── strategy1.js │ │ │ │ └── strategy2.js │ │ │ ├── index.js │ │ │ └── context.js │ │ ├── adapter │ │ │ ├── index.js │ │ │ ├── target.js │ │ │ ├── adaptee.js │ │ │ └── adapter.js │ │ ├── facade │ │ │ ├── subsystems │ │ │ │ ├── index.js │ │ │ │ ├── subsystem1.js │ │ │ │ └── subsystem2.js │ │ │ ├── index.js │ │ │ └── facade.js │ │ ├── decorator │ │ │ ├── components │ │ │ │ ├── index.js │ │ │ │ ├── componentA.js │ │ │ │ └── componentB.js │ │ │ ├── decorators │ │ │ │ ├── index.js │ │ │ │ ├── decorator.js │ │ │ │ ├── decoratorA.js │ │ │ │ └── decoratorB.js │ │ │ └── index.js │ │ ├── composite │ │ │ ├── index.js │ │ │ └── components │ │ │ │ ├── component-leaf.js │ │ │ │ └── component.js │ │ ├── builder │ │ │ ├── builders │ │ │ │ ├── product1.js │ │ │ │ ├── product2.js │ │ │ │ ├── builder1.js │ │ │ │ └── builder2.js │ │ │ ├── index.js │ │ │ └── director.js │ │ ├── chain-of-responsibility │ │ │ ├── index.js │ │ │ └── handlers │ │ │ │ ├── handler.js │ │ │ │ ├── handler1.js │ │ │ │ ├── handler2.js │ │ │ │ └── handler3.js │ │ └── bridge │ │ │ ├── implementations │ │ │ ├── implementationA.js │ │ │ └── implementationB.js │ │ │ ├── index.js │ │ │ └── abstractions │ │ │ ├── abstraction-extended.js │ │ │ └── abstraction.js │ ├── .babelrc │ ├── examples │ │ ├── state.js │ │ ├── strategy.js │ │ ├── command.js │ │ ├── proxy.js │ │ ├── mediator.js │ │ ├── facade.js │ │ ├── factory-method.js │ │ ├── simple-factory.js │ │ ├── memento.js │ │ ├── template-method.js │ │ ├── adapter.js │ │ ├── iterator.js │ │ ├── bridge.js │ │ ├── observer.js │ │ ├── decorator.js │ │ ├── visitor.js │ │ ├── abstract-factory.js │ │ ├── chain-of-responsibility.js │ │ ├── composite.js │ │ └── singleton.js │ ├── .eslintrc.js │ └── README.md │ ├── php │ └── src │ │ └── RefactoringGuru │ │ ├── Singleton │ │ ├── Structural │ │ │ └── Output.txt │ │ └── RealWorld │ │ │ └── Output.txt │ │ ├── Iterator │ │ ├── Structural │ │ │ └── Output.txt │ │ └── RealWorld │ │ │ ├── cats.csv │ │ │ └── Output.txt │ │ ├── Facade │ │ ├── Structural │ │ │ └── Output.txt │ │ └── RealWorld │ │ │ └── Output.txt │ │ ├── Decorator │ │ ├── Structural │ │ │ └── Output.txt │ │ └── RealWorld │ │ │ └── Output.txt │ │ ├── Builder │ │ ├── Structural │ │ │ └── Output.txt │ │ └── RealWorld │ │ │ └── Output.txt │ │ ├── Bridge │ │ ├── Structural │ │ │ └── Output.txt │ │ └── RealWorld │ │ │ └── Output.txt │ │ ├── Visitor │ │ ├── Structural │ │ │ └── Output.txt │ │ └── RealWorld │ │ │ └── Output.txt │ │ ├── Prototype │ │ └── Structural │ │ │ └── Output.txt │ │ ├── Strategy │ │ └── Structural │ │ │ └── Output.txt │ │ ├── Composite │ │ ├── Structural │ │ │ └── Output.txt │ │ └── RealWorld │ │ │ └── Output.txt │ │ ├── Proxy │ │ ├── Structural │ │ │ └── Output.txt │ │ └── RealWorld │ │ │ └── Output.txt │ │ ├── Flyweight │ │ ├── RealWorld │ │ │ ├── cats.csv │ │ │ └── Output.txt │ │ └── Structural │ │ │ └── Output.txt │ │ ├── Mediator │ │ ├── Structural │ │ │ └── Output.txt │ │ └── RealWorld │ │ │ ├── log.txt │ │ │ └── Output.txt │ │ ├── Adapter │ │ ├── Structural │ │ │ └── Output.txt │ │ └── RealWorld │ │ │ └── Output.txt │ │ ├── AbstractFactory │ │ ├── RealWorld │ │ │ └── Output.txt │ │ └── Structural │ │ │ └── Output.txt │ │ ├── Command │ │ ├── Structural │ │ │ └── Output.txt │ │ └── RealWorld │ │ │ └── Output.txt │ │ ├── FactoryMethod │ │ ├── Structural │ │ │ └── Output.txt │ │ └── RealWorld │ │ │ └── Output.txt │ │ ├── State │ │ └── Structural │ │ │ └── Output.txt │ │ ├── TemplateMethod │ │ ├── RealWorld │ │ │ └── Output.txt │ │ └── Structural │ │ │ └── Output.txt │ │ ├── Observer │ │ ├── RealWorld │ │ │ ├── log.txt │ │ │ └── Output.txt │ │ └── Structural │ │ │ └── Output.txt │ │ ├── ChainOfResponsibility │ │ ├── Structural │ │ │ └── Output.txt │ │ └── RealWorld │ │ │ └── Output.txt │ │ └── Memento │ │ └── Structural │ │ └── Output.txt │ ├── c# │ ├── RefactoringGuru.DesignPatterns.Singleton.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.Iterator.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.Adapter.RealWorld │ │ ├── Output.txt │ │ └── RefactoringGuru.DesignPatterns.Adapter.RealWorld.csproj │ ├── RefactoringGuru.DesignPatterns.Adapter.Structural │ │ ├── Output.txt │ │ └── RefactoringGuru.DesignPatterns.Adapter.Structural.csproj │ ├── RefactoringGuru.DesignPatterns.Facade.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.Decorator.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.Builder.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.Bridge.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.Prototype.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.FactoryMethod.Structural │ │ ├── RefactoringGuru.DesignPatterns.FactoryMethod.Structural.csproj │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.AbstractFactory.Structural │ │ ├── RefactoringGuru.DesignPatterns.AbstractFactory.Structural.csproj │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.Visitor.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.Strategy.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.Composite.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.Proxy.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.Mediator.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.Command.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.State.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.ChainOfResponsibility.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.Observer.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.TemplateMethod.Structural │ │ └── Output.txt │ ├── RefactoringGuru.DesignPatterns.Flyweight.Structural │ │ └── Output.txt │ └── RefactoringGuru.DesignPatterns.Memento.Structural │ │ └── Output.txt │ └── java │ └── src │ └── refactoring_guru │ ├── abstract_factory │ └── example │ │ ├── OutputDemo.txt │ │ ├── checkboxes │ │ ├── Checkbox.java │ │ ├── MacOSCheckbox.java │ │ └── WindowsCheckbox.java │ │ ├── buttons │ │ ├── MacOSButton.java │ │ ├── WindowsButton.java │ │ └── Button.java │ │ ├── factories │ │ ├── GUIFactory.java │ │ ├── MacOSFactory.java │ │ └── WindowsFactory.java │ │ └── app │ │ └── Application.java │ ├── factory_method │ └── example │ │ ├── OutputDemo.txt │ │ ├── OutputDemo.png │ │ ├── buttons │ │ ├── Button.java │ │ └── HtmlButton.java │ │ └── factory │ │ ├── HtmlDialog.java │ │ ├── WindowsDialog.java │ │ └── Dialog.java │ ├── builder │ └── example │ │ ├── cars │ │ └── Type.java │ │ ├── components │ │ ├── Transmission.java │ │ ├── GPSNavigator.java │ │ ├── TripComputer.java │ │ └── Engine.java │ │ ├── OutputDemo.txt │ │ └── builders │ │ └── Builder.java │ ├── adapter │ └── example │ │ ├── OutputDemo.txt │ │ ├── round │ │ ├── RoundPeg.java │ │ └── RoundHole.java │ │ ├── square │ │ └── SquarePeg.java │ │ └── adapters │ │ └── SquarePegAdapter.java │ ├── facade │ └── example │ │ ├── some_complex_media_library │ │ ├── Codec.java │ │ ├── OggCompressionCodec.java │ │ ├── MPEG4CompressionCodec.java │ │ ├── AudioMixer.java │ │ ├── BitrateReader.java │ │ ├── VideoFile.java │ │ └── CodecFactory.java │ │ ├── OutputDemo.txt │ │ ├── Demo.java │ │ └── facade │ │ └── VideoConversionFacade.java │ ├── state │ └── example │ │ ├── OutputDemo.png │ │ ├── Demo.java │ │ └── states │ │ ├── State.java │ │ ├── PlayingState.java │ │ ├── ReadyState.java │ │ └── LockedState.java │ ├── command │ └── example │ │ ├── OutputDemo.png │ │ ├── Demo.java │ │ └── commands │ │ ├── CommandHistory.java │ │ ├── CopyCommand.java │ │ ├── Command.java │ │ ├── PasteCommand.java │ │ └── CutCommand.java │ ├── memento │ └── example │ │ ├── OutputDemo.png │ │ ├── commands │ │ ├── Command.java │ │ └── ColorCommand.java │ │ ├── history │ │ └── Memento.java │ │ ├── shapes │ │ ├── Shape.java │ │ ├── Dot.java │ │ ├── Circle.java │ │ └── Rectangle.java │ │ └── Demo.java │ ├── prototype │ ├── caching │ │ └── OutputDemo.txt │ └── example │ │ ├── OutputDemo.txt │ │ └── shapes │ │ ├── Circle.java │ │ ├── Shape.java │ │ └── Rectangle.java │ ├── composite │ └── example │ │ ├── OutputDemo.png │ │ └── shapes │ │ ├── Shape.java │ │ ├── Dot.java │ │ ├── Circle.java │ │ └── Rectangle.java │ ├── flyweight │ └── example │ │ ├── OutputDemo.png │ │ ├── OutputDemo.txt │ │ ├── trees │ │ ├── Tree.java │ │ ├── TreeFactory.java │ │ └── TreeType.java │ │ └── forest │ │ └── Forest.java │ ├── mediator │ └── example │ │ ├── OutputDemo.png │ │ ├── components │ │ ├── Component.java │ │ ├── Title.java │ │ ├── TextBox.java │ │ ├── DeleteButton.java │ │ ├── SaveButton.java │ │ └── AddButton.java │ │ ├── mediator │ │ ├── Mediator.java │ │ └── Note.java │ │ └── Demo.java │ ├── decorator │ └── example │ │ ├── decorators │ │ ├── DataSource.java │ │ ├── DataSourceDecorator.java │ │ ├── EncryptionDecorator.java │ │ └── FileDataSource.java │ │ ├── OutputDemo.txt │ │ └── Demo.java │ ├── singleton │ └── example │ │ ├── non_thread_safe │ │ ├── OutputDemoMultiThread.txt │ │ ├── OutputDemoSingleThread.txt │ │ ├── DemoSingleThread.java │ │ ├── Singleton.java │ │ └── DemoMultiThread.java │ │ └── thread_safe │ │ ├── OutputDemoMultiThread.txt │ │ ├── Singleton.java │ │ └── DemoMultiThread.java │ ├── observer │ └── example │ │ ├── listeners │ │ ├── EventListener.java │ │ ├── LogOpenListener.java │ │ └── EmailNotificationListener.java │ │ ├── OutputDemo.txt │ │ ├── editor │ │ └── Editor.java │ │ └── Demo.java │ ├── bridge │ └── example │ │ ├── remotes │ │ ├── Remote.java │ │ └── AdvancedRemote.java │ │ ├── devices │ │ └── Device.java │ │ ├── OutputDemo.txt │ │ └── Demo.java │ ├── visitor │ └── example │ │ ├── shapes │ │ ├── Shape.java │ │ ├── Circle.java │ │ ├── CompoundShape.java │ │ └── Dot.java │ │ ├── visitor │ │ └── Visitor.java │ │ ├── OutputDemo.txt │ │ └── Demo.java │ ├── strategy │ └── example │ │ ├── strategies │ │ ├── PayStrategy.java │ │ └── CreditCard.java │ │ ├── OutputDemo.txt │ │ └── order │ │ └── Order.java │ ├── chain_of_responsibility │ └── example │ │ ├── OutputDemo.txt │ │ └── middleware │ │ ├── RoleCheckMiddleware.java │ │ ├── UserExistsMiddleware.java │ │ └── Middleware.java │ ├── iterator │ └── example │ │ ├── iterators │ │ └── ProfileIterator.java │ │ ├── social_networks │ │ └── SocialNetwork.java │ │ └── OutputDemo.txt │ ├── proxy │ └── example │ │ └── some_cool_media_library │ │ ├── ThirdPartyYoutubeLib.java │ │ └── Video.java │ └── template_method │ └── example │ ├── OutputDemo.txt │ └── networks │ └── Network.java ├── README-en.md └── .github └── ISSUE_TEMPLATE ├── custom.md ├── feature_request.md └── bug_report.md /.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | .cache/ 3 | 4 | -------------------------------------------------------------------------------- /courses/guru/js/.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | .idea 3 | node_modules -------------------------------------------------------------------------------- /courses/guru/js/patterns/command/invokers/index.js: -------------------------------------------------------------------------------- 1 | import { Invoker1 } from './invoker1'; 2 | 3 | export { Invoker1 }; 4 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/singleton/index.js: -------------------------------------------------------------------------------- 1 | import { Singleton } from './singleton'; 2 | 3 | export { Singleton }; 4 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/command/receivers/index.js: -------------------------------------------------------------------------------- 1 | import { Receiver1 } from './receiver1'; 2 | 3 | export { Receiver1 }; 4 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/iterator/index.js: -------------------------------------------------------------------------------- 1 | import { Collection1 } from './collection1'; 2 | 3 | export { Collection1 }; 4 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/mediator/mediators/index.js: -------------------------------------------------------------------------------- 1 | import { Mediator1 } from './mediator1'; 2 | 3 | export { Mediator1 }; 4 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/observer/subjects/index.js: -------------------------------------------------------------------------------- 1 | import { Subject1 } from './subject1'; 2 | 3 | export { Subject1 }; 4 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Singleton/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Singleton works, both variables contain the same instance. -------------------------------------------------------------------------------- /courses/guru/js/patterns/simple-factory/index.js: -------------------------------------------------------------------------------- 1 | import { ProductFactory } from './factory'; 2 | 3 | export { ProductFactory }; 4 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Singleton.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Singleton works, both variables contain the same instance. -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/abstract_factory/example/OutputDemo.txt: -------------------------------------------------------------------------------- 1 | You create WindowsButton. 2 | You created WindowsCheckbox. -------------------------------------------------------------------------------- /courses/guru/js/patterns/flyweight/index.js: -------------------------------------------------------------------------------- 1 | import { FlyweightFactory } from './flyweight-factory'; 2 | 3 | export { FlyweightFactory }; 4 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/factory_method/example/OutputDemo.txt: -------------------------------------------------------------------------------- 1 | 2 | Click! Button says - 'Hello World!' -------------------------------------------------------------------------------- /courses/guru/js/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | "@babel/plugin-proposal-class-properties" 4 | ], 5 | "presets": [ 6 | "@babel/preset-env" 7 | ] 8 | } -------------------------------------------------------------------------------- /README-en.md: -------------------------------------------------------------------------------- 1 | 2 | It is worth mentioning that many snippets of design patterns are extracted and arranged from many other great repos, which are listed is [ABOUT](). 3 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/proxy/index.js: -------------------------------------------------------------------------------- 1 | import { Subject } from './subject'; 2 | import { Proxy } from './proxy'; 3 | 4 | export { 5 | Subject, 6 | Proxy, 7 | }; 8 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/template-method/index.js: -------------------------------------------------------------------------------- 1 | import { Operation1, Operation2 } from './operations'; 2 | 3 | export { 4 | Operation1, 5 | Operation2, 6 | }; 7 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/abstract-factory/index.js: -------------------------------------------------------------------------------- 1 | import { Factory1 } from './factory1'; 2 | import { Factory2 } from './factory2'; 3 | 4 | export { Factory1, Factory2 }; 5 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/prototype/index.js: -------------------------------------------------------------------------------- 1 | import { Prototype } from './prototype'; 2 | import { Component } from './component'; 3 | 4 | export { Prototype, Component }; 5 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/state/states/index.js: -------------------------------------------------------------------------------- 1 | import { State1 } from './state1'; 2 | import { State2 } from './state2'; 3 | 4 | export { 5 | State1, 6 | State2, 7 | }; 8 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/custom.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Custom issue template 3 | about: Describe this issue template's purpose here. 4 | title: "" 5 | labels: "" 6 | assignees: "" 7 | --- 8 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/builder/example/cars/Type.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.builder.example.cars; 2 | 3 | public enum Type { 4 | CITY_CAR, SPORTS_CAR, SUV 5 | } 6 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/adapter/example/OutputDemo.txt: -------------------------------------------------------------------------------- 1 | Round peg r5 fits round hole r5. 2 | Square peg w2 fits round hole r5. 3 | Square peg w20 does not fit into round hole r5. -------------------------------------------------------------------------------- /courses/guru/js/patterns/factory-method/index.js: -------------------------------------------------------------------------------- 1 | import { Creator1 } from './creator1/creator1'; 2 | import { Creator2 } from './creator2/creator2'; 3 | 4 | export { Creator1, Creator2 }; 5 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/memento/index.js: -------------------------------------------------------------------------------- 1 | import { Originator } from './originator'; 2 | import { Caretaker } from './caretaker'; 3 | 4 | export { 5 | Originator, 6 | Caretaker, 7 | }; 8 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/visitor/visitors/index.js: -------------------------------------------------------------------------------- 1 | import { Visitor1 } from './visitor1'; 2 | import { Visitor2 } from './visitor2'; 3 | 4 | export { 5 | Visitor1, 6 | Visitor2, 7 | }; 8 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Iterator/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Straight traversal: 2 | First 3 | Second 4 | Third 5 | 6 | Reverse traversal: 7 | Third 8 | Second 9 | First 10 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Iterator.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Straight traversal: 2 | First 3 | Second 4 | Third 5 | 6 | Reverse traversal: 7 | Third 8 | Second 9 | First -------------------------------------------------------------------------------- /courses/guru/js/patterns/state/index.js: -------------------------------------------------------------------------------- 1 | import { State1, State2 } from './states'; 2 | import { Context } from './context'; 3 | 4 | export { 5 | State1, 6 | State2, 7 | Context, 8 | }; 9 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Adapter.RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | Round peg r5 fits round hole r5. 2 | Square peg w2 fits round hole r5. 3 | Square peg w20 does not fit into round hole r5. -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/facade/example/some_complex_media_library/Codec.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.facade.example.some_complex_media_library; 2 | 3 | public interface Codec { 4 | } 5 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/observer/observers/index.js: -------------------------------------------------------------------------------- 1 | import { Observer1 } from './observer1'; 2 | import { Observer2 } from './observer2'; 3 | 4 | export { 5 | Observer1, 6 | Observer2, 7 | }; 8 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/strategy/strategies/index.js: -------------------------------------------------------------------------------- 1 | import { Strategy1 } from './strategy1'; 2 | import { Strategy2 } from './strategy2'; 3 | 4 | export { 5 | Strategy1, 6 | Strategy2, 7 | }; 8 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/state/example/OutputDemo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wx-chevalier/design-pattern-examples/master/courses/guru/java/src/refactoring_guru/state/example/OutputDemo.png -------------------------------------------------------------------------------- /courses/guru/js/patterns/adapter/index.js: -------------------------------------------------------------------------------- 1 | import { Target } from './target'; 2 | import { Adaptee } from './adaptee'; 3 | import { Adapter } from './adapter'; 4 | 5 | export { Target, Adaptee, Adapter }; 6 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/facade/subsystems/index.js: -------------------------------------------------------------------------------- 1 | import { Subsystem1 } from './subsystem1'; 2 | import { Subsystem2 } from './subsystem2'; 3 | 4 | export { 5 | Subsystem1, 6 | Subsystem2, 7 | }; 8 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/mediator/components/index.js: -------------------------------------------------------------------------------- 1 | import { Component1 } from './component1'; 2 | import { Component2 } from './component2'; 3 | 4 | export { 5 | Component1, 6 | Component2, 7 | }; 8 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/visitor/components/index.js: -------------------------------------------------------------------------------- 1 | import { Component1 } from './component1'; 2 | import { Component2 } from './component2'; 3 | 4 | export { 5 | Component1, 6 | Component2, 7 | }; 8 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Adapter.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Adaptee interface is incompatible with the client. 2 | But with adapter client can call it's method. 3 | This is 'Specific request.' -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/command/example/OutputDemo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wx-chevalier/design-pattern-examples/master/courses/guru/java/src/refactoring_guru/command/example/OutputDemo.png -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/memento/example/OutputDemo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wx-chevalier/design-pattern-examples/master/courses/guru/java/src/refactoring_guru/memento/example/OutputDemo.png -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/prototype/caching/OutputDemo.txt: -------------------------------------------------------------------------------- 1 | Big green circle != Medium blue rectangle (yay!) 2 | Medium blue rectangles are two different objects (yay!) 3 | And they are identical (yay!) -------------------------------------------------------------------------------- /courses/guru/js/patterns/command/commands/index.js: -------------------------------------------------------------------------------- 1 | import { SimpleCommand } from './simple'; 2 | import { ComplexCommand } from './complex'; 3 | 4 | export { 5 | SimpleCommand, 6 | ComplexCommand, 7 | }; 8 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/decorator/components/index.js: -------------------------------------------------------------------------------- 1 | import { ComponentA } from './componentA'; 2 | import { ComponentB } from './componentB'; 3 | 4 | export { 5 | ComponentA, 6 | ComponentB, 7 | }; 8 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/decorator/decorators/index.js: -------------------------------------------------------------------------------- 1 | import { DecoratorA } from './decoratorA'; 2 | import { DecoratorB } from './decoratorB'; 3 | 4 | export { 5 | DecoratorA, 6 | DecoratorB, 7 | }; 8 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/facade/index.js: -------------------------------------------------------------------------------- 1 | import { Facade } from './facade'; 2 | import { Subsystem1, Subsystem2 } from './subsystems'; 3 | 4 | export { 5 | Facade, 6 | Subsystem1, 7 | Subsystem2, 8 | }; 9 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/strategy/index.js: -------------------------------------------------------------------------------- 1 | import { Context} from './context'; 2 | import { Strategy1, Strategy2 } from './strategies'; 3 | 4 | export { 5 | Context, 6 | Strategy1, 7 | Strategy2, 8 | }; 9 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/composite/example/OutputDemo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wx-chevalier/design-pattern-examples/master/courses/guru/java/src/refactoring_guru/composite/example/OutputDemo.png -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/flyweight/example/OutputDemo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wx-chevalier/design-pattern-examples/master/courses/guru/java/src/refactoring_guru/flyweight/example/OutputDemo.png -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/mediator/example/OutputDemo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wx-chevalier/design-pattern-examples/master/courses/guru/java/src/refactoring_guru/mediator/example/OutputDemo.png -------------------------------------------------------------------------------- /courses/guru/js/patterns/adapter/target.js: -------------------------------------------------------------------------------- 1 | // some specific interface used by client code 2 | class Target { 3 | request() { 4 | return 'TARGET: default behavior.'; 5 | } 6 | } 7 | 8 | export { Target }; 9 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/observer/index.js: -------------------------------------------------------------------------------- 1 | import { Subject1 } from './subjects'; 2 | import { Observer1, Observer2 } from './observers'; 3 | 4 | export { 5 | Subject1, 6 | Observer1, 7 | Observer2, 8 | }; 9 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/template-method/operations/index.js: -------------------------------------------------------------------------------- 1 | import { Operation1 } from './operation1'; 2 | import { Operation2 } from './operation2'; 3 | 4 | export { 5 | Operation1, 6 | Operation2, 7 | }; 8 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Singleton/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | 2018-06-04: Started! 2 | 2018-06-04: Logger has a single instance. 3 | 2018-06-04: Config singleton also works fine. 4 | 2018-06-04: Finished! 5 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/memento/example/commands/Command.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.memento.example.commands; 2 | 3 | public interface Command { 4 | String getName(); 5 | void execute(); 6 | } 7 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/factory_method/example/OutputDemo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wx-chevalier/design-pattern-examples/master/courses/guru/java/src/refactoring_guru/factory_method/example/OutputDemo.png -------------------------------------------------------------------------------- /courses/guru/js/patterns/mediator/index.js: -------------------------------------------------------------------------------- 1 | import { Component1, Component2 } from './components'; 2 | import { Mediator1 } from './mediators'; 3 | 4 | export { 5 | Component1, 6 | Component2, 7 | Mediator1, 8 | }; 9 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/factory-method/creator1/product1.js: -------------------------------------------------------------------------------- 1 | // concrete product provide own implementation 2 | class Product1 { 3 | operation() { 4 | return '{Result of ConcreteProduct1}'; 5 | } 6 | } 7 | 8 | export { Product1 }; 9 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/factory-method/creator2/product2.js: -------------------------------------------------------------------------------- 1 | // concrete product provide own implementation 2 | class Product2 { 3 | operation() { 4 | return '{Result of ConcreteProduct2}'; 5 | } 6 | } 7 | 8 | export { Product2 }; 9 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Facade/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Facade initializes subsystems: 2 | Subsystem1: Ready! 3 | Subsystem2: Get ready! 4 | Facade orders subsystems to perform the action: 5 | Subsystem1: Go! 6 | Subsystem2: Fire! 7 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Facade.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Facade initializes subsystems: 2 | Subsystem1: Ready! 3 | Subsystem2: Get ready! 4 | Facade orders subsystems to perform the action: 5 | Subsystem1: Go! 6 | Subsystem2: Fire! -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/decorator/example/decorators/DataSource.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.decorator.example.decorators; 2 | 3 | public interface DataSource { 4 | void writeData(String data); 5 | 6 | String readData(); 7 | } 8 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/composite/index.js: -------------------------------------------------------------------------------- 1 | import { ComponentLeaf } from './components/component-leaf'; 2 | import { ComponentComposite } from './components/component-composite'; 3 | 4 | export { 5 | ComponentLeaf, 6 | ComponentComposite, 7 | }; 8 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/decorator/components/componentA.js: -------------------------------------------------------------------------------- 1 | // Component interface define operations that can be altered by Decorators 2 | class ComponentA { 3 | operation() { 4 | return 'ComponentA'; 5 | } 6 | } 7 | 8 | export { ComponentA }; 9 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/decorator/components/componentB.js: -------------------------------------------------------------------------------- 1 | // Component interface define operations that can be altered by Decorators 2 | class ComponentB { 3 | operation() { 4 | return 'ComponentB'; 5 | } 6 | } 7 | 8 | export { ComponentB }; 9 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/visitor/index.js: -------------------------------------------------------------------------------- 1 | import { Component1, Component2 } from './components'; 2 | import { Visitor1, Visitor2 } from './visitors'; 3 | 4 | export { 5 | Component1, 6 | Component2, 7 | Visitor1, 8 | Visitor2, 9 | }; 10 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Decorator/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Client: I've got a simple component: 2 | RESULT: ConcreteComponent 3 | 4 | Client: Now I've got a decorated component: 5 | RESULT: ConcreteDecoratorB(ConcreteDecoratorA(ConcreteComponent)) -------------------------------------------------------------------------------- /courses/guru/js/patterns/prototype/component.js: -------------------------------------------------------------------------------- 1 | // optional helper class used to demonstrate ability to clone nested objects 2 | class Component { 3 | constructor(prototype) { 4 | this.prototype = prototype; 5 | } 6 | } 7 | 8 | export { Component }; 9 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Decorator.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Client: I get a sumple component: 2 | RESULT: ConcreteComponent 3 | 4 | Client: Now I get a decorated component: 5 | RESULT: ConcreteDecoratorB(ConcreteDecoratorA(ConcreteComponent)) -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/flyweight/example/OutputDemo.txt: -------------------------------------------------------------------------------- 1 | 1000000 trees drawn 2 | --------------------- 3 | Memory usage: 4 | Tree size (8 bytes) * 1000000 5 | + TreeTypes size (~30 bytes) * 2 6 | --------------------- 7 | Total: 7MB (instead of 36MB) -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/singleton/example/non_thread_safe/OutputDemoMultiThread.txt: -------------------------------------------------------------------------------- 1 | If you see the same value, then singleton was reused (yay!) 2 | If you see different values, then 2 singletons were created (booo!!) 3 | 4 | RESULT: 5 | 6 | FOO 7 | BAR -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/singleton/example/thread_safe/OutputDemoMultiThread.txt: -------------------------------------------------------------------------------- 1 | If you see the same value, then singleton was reused (yay!) 2 | If you see different values, then 2 singletons were created (booo!!) 3 | 4 | RESULT: 5 | 6 | BAR 7 | BAR -------------------------------------------------------------------------------- /courses/guru/js/patterns/strategy/strategies/strategy1.js: -------------------------------------------------------------------------------- 1 | // concrete algorithm 2 | // all Strategies must implement this interface 3 | class Strategy1 { 4 | doAlgorithm(data) { 5 | return data.sort(); 6 | } 7 | } 8 | 9 | export { Strategy1 }; 10 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/observer/example/listeners/EventListener.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.observer.example.listeners; 2 | 3 | import java.io.File; 4 | 5 | public interface EventListener { 6 | void update(String eventType, File file); 7 | } 8 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/singleton/example/non_thread_safe/OutputDemoSingleThread.txt: -------------------------------------------------------------------------------- 1 | If you see the same value, then singleton was reused (yay!) 2 | If you see different values, then 2 singletons were created (booo!!) 3 | 4 | RESULT: 5 | 6 | FOO 7 | FOO -------------------------------------------------------------------------------- /courses/guru/js/patterns/builder/builders/product1.js: -------------------------------------------------------------------------------- 1 | class Product1 { 2 | constructor() { 3 | this.parts = []; 4 | } 5 | 6 | listParts() { 7 | console.log(`Product parts: ${this.parts.join(', ')}\n`); 8 | } 9 | } 10 | export { Product1 }; 11 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/builder/index.js: -------------------------------------------------------------------------------- 1 | import { Director } from './director'; 2 | import { Builder1 } from './builders/builder1'; 3 | import { Builder2 } from './builders/builder2'; 4 | 5 | export { 6 | Director, 7 | Builder1, 8 | Builder2, 9 | }; 10 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/strategy/strategies/strategy2.js: -------------------------------------------------------------------------------- 1 | // concrete algorithm 2 | // all Strategies must implement this interface 3 | class Strategy2 { 4 | doAlgorithm(data) { 5 | return data.reverse(); 6 | } 7 | } 8 | 9 | export { Strategy2 }; 10 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Builder.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Standart basic product: 2 | Product parts: PartA1 3 | 4 | Standart full featured product: 5 | Product parts: PartA1, PartB1, PartC1 6 | 7 | Custom product: 8 | Product parts: PartA1, PartC1 -------------------------------------------------------------------------------- /courses/guru/js/patterns/abstract-factory/products/productA1.js: -------------------------------------------------------------------------------- 1 | // product factory 2 | class ProductA1 { 3 | // products of family A must implement this method 4 | productAMethod() { 5 | return 'Product A1'; 6 | } 7 | } 8 | 9 | export { ProductA1 }; 10 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/abstract-factory/products/productA2.js: -------------------------------------------------------------------------------- 1 | // product factory 2 | class ProductA2 { 3 | // products of family A must implement this method 4 | productAMethod() { 5 | return 'Product A2'; 6 | } 7 | } 8 | 9 | export { ProductA2 }; 10 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/builder/builders/product2.js: -------------------------------------------------------------------------------- 1 | class Product2 { 2 | constructor() { 3 | this.parts = []; 4 | } 5 | 6 | listParts() { 7 | console.log(`Product parts: ${this.parts.join(', ')}\n`); 8 | } 9 | } 10 | 11 | export { Product2 }; 12 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Builder/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Standard basic product: 2 | Product parts: PartA1 3 | 4 | Standard full featured product: 5 | Product parts: PartA1, PartB1, PartC1 6 | 7 | Custom product: 8 | Product parts: PartA1, PartC1 9 | 10 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/facade/example/some_complex_media_library/OggCompressionCodec.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.facade.example.some_complex_media_library; 2 | 3 | public class OggCompressionCodec implements Codec { 4 | public String type = "ogg"; 5 | } 6 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/observer/example/OutputDemo.txt: -------------------------------------------------------------------------------- 1 | Save to log \path\to\log\file.txt: Someone has performed open operation with the following file: test.txt 2 | Email to admin@example.com: Someone has performed save operation with the following file: test.txt 3 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Bridge.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | ExtendedAbstraction: Extended operation with: 2 | ConcreteImplementationA: The result in platform A. 3 | 4 | ExtendedAbstraction: Extended operation with: 5 | ConcreteImplementationA: The result in platform B. -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/builder/example/components/Transmission.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.builder.example.components; 2 | 3 | /** 4 | * Just another feature of a car. 5 | */ 6 | public enum Transmission { 7 | SINGLE_SPEED, MANUAL, AUTOMATIC, SEMI_AUTOMATIC 8 | } 9 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/facade/example/some_complex_media_library/MPEG4CompressionCodec.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.facade.example.some_complex_media_library; 2 | 3 | public class MPEG4CompressionCodec implements Codec { 4 | public String type = "mp4"; 5 | 6 | } 7 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Bridge/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Abstraction: Base operation with: 2 | ConcreteImplementationA: Here's the result on the platform A. 3 | 4 | ExtendedAbstraction: Extended operation with: 5 | ConcreteImplementationB: Here's the result on the platform B. 6 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Visitor/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Client code works with visitors through a common Visitor interface: 2 | A + ConcreteVisitor1 3 | B + ConcreteVisitor1 4 | 5 | Same client code can work with different visitors: 6 | A + ConcreteVisitor1 7 | B + ConcreteVisitor2 -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/factory_method/example/buttons/Button.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.factory_method.example.buttons; 2 | 3 | /** 4 | * Common interface for all buttons. 5 | */ 6 | public interface Button { 7 | void render(); 8 | void onClick(); 9 | } 10 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/chain-of-responsibility/index.js: -------------------------------------------------------------------------------- 1 | import { Handler1 } from './handlers/handler1'; 2 | import { Handler2 } from './handlers/handler2'; 3 | import { Handler3 } from './handlers/handler3'; 4 | 5 | export { 6 | Handler1, 7 | Handler2, 8 | Handler3, 9 | }; 10 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/factory-method/creator2/creator2.js: -------------------------------------------------------------------------------- 1 | import { Creator } from '../creator'; 2 | import { Product2 } from './product2'; 3 | 4 | class Creator2 extends Creator { 5 | factoryMethod() { 6 | return new Product2(); 7 | } 8 | } 9 | 10 | export { Creator2 }; 11 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Prototype/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Primitive field values have been carried over to a clone. Yay! 2 | Simple component has been cloned. Yay! 3 | Component with back reference has been cloned. Yay! 4 | Component with back reference is linked to the clone. Yay! 5 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/facade/example/OutputDemo.txt: -------------------------------------------------------------------------------- 1 | VideoConversionFacade: conversion started. 2 | CodecFactory: extracting ogg audio... 3 | BitrateReader: reading file... 4 | BitrateReader: writing file... 5 | AudioMixer: fixing audio... 6 | VideoConversionFacade: conversion completed. -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Prototype.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Primitive field values have been carried over to a clone. Yay! 2 | Simple component has been cloned. Yay! 3 | Component with back reference has been cloned. Yay! 4 | Component with back reference is linked to the clone. Yay! -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/prototype/example/OutputDemo.txt: -------------------------------------------------------------------------------- 1 | 0: Shapes are different objects (yay!) 2 | 0: And they are identical (yay!) 3 | 1: Shapes are different objects (yay!) 4 | 1: And they are identical (yay!) 5 | 2: Shapes are different objects (yay!) 6 | 2: And they are identical (yay!) -------------------------------------------------------------------------------- /courses/guru/js/patterns/command/index.js: -------------------------------------------------------------------------------- 1 | import { SimpleCommand, ComplexCommand } from './commands'; 2 | import { Invoker1 } from './invokers'; 3 | import { Receiver1 } from './receivers'; 4 | 5 | export { 6 | SimpleCommand, 7 | ComplexCommand, 8 | Invoker1, 9 | Receiver1, 10 | }; 11 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/builder/example/OutputDemo.txt: -------------------------------------------------------------------------------- 1 | Car built: 2 | SPORTS_CAR 3 | 4 | Car manual built: 5 | Type of car: SPORTS_CAR 6 | Count of seats: 2 7 | Engine: volume - 3.0; mileage - 0.0 8 | Transmission: SEMI_AUTOMATIC 9 | Trip Computer: Functional 10 | GPS Navigator: Functional -------------------------------------------------------------------------------- /courses/guru/js/patterns/decorator/index.js: -------------------------------------------------------------------------------- 1 | import { 2 | ComponentA, 3 | ComponentB, 4 | } from './components'; 5 | 6 | import { 7 | DecoratorA, 8 | DecoratorB, 9 | } from './decorators'; 10 | 11 | export { 12 | ComponentA, 13 | ComponentB, 14 | DecoratorA, 15 | DecoratorB, 16 | }; 17 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/simple-factory/factory.js: -------------------------------------------------------------------------------- 1 | import { Product } from './product'; 2 | 3 | // product factory: make product and return it 4 | class ProductFactory { 5 | static makeProduct(prop1, prop2) { 6 | return new Product(prop1, prop2); 7 | } 8 | } 9 | 10 | export { ProductFactory }; 11 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/abstract_factory/example/checkboxes/Checkbox.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.abstract_factory.example.checkboxes; 2 | 3 | /** 4 | * Checkboxes is the second product family. It has the same variants as buttons. 5 | */ 6 | public interface Checkbox { 7 | void paint(); 8 | } 9 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Builder/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | Testing MySQL query builder: 2 | SELECT name, email, password FROM users WHERE age > '18' AND age < '30' LIMIT 10, 20; 3 | 4 | Testing PostgresSQL query builder: 5 | SELECT name, email, password FROM users WHERE age > '18' AND age < '30' LIMIT 10 OFFSET 20; -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.FactoryMethod.Structural/RefactoringGuru.DesignPatterns.FactoryMethod.Structural.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | Exe 4 | netcoreapp2.0 5 | 6 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Facade/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | Fetching video metadata from youtube... 2 | Saving video file to a temporary file... 3 | Processing source video... 4 | Normalizing and resizing the video to smaller dimensions... 5 | Capturing preview image... 6 | Saving video in target formats... 7 | Done! 8 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.AbstractFactory.Structural/RefactoringGuru.DesignPatterns.AbstractFactory.Structural.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | Exe 4 | netcoreapp2.0 5 | 6 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Adapter.RealWorld/RefactoringGuru.DesignPatterns.Adapter.RealWorld.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Adapter.Structural/RefactoringGuru.DesignPatterns.Adapter.Structural.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/bridge/example/remotes/Remote.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.bridge.example.remotes; 2 | 3 | public interface Remote { 4 | void power(); 5 | 6 | void volumeDown(); 7 | 8 | void volumeUp(); 9 | 10 | void channelDown(); 11 | 12 | void channelUp(); 13 | } 14 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Visitor.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | The client code works with all visitors via the base Visitor interface: 2 | A + ConcreteVisitor1 3 | B + ConcreteVisitor1 4 | 5 | It allows the same client code to work with different types of visitors: 6 | A + ConcreteVisitor2 7 | B + ConcreteVisitor2 -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/visitor/example/shapes/Shape.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.visitor.example.shapes; 2 | 3 | import refactoring_guru.visitor.example.visitor.Visitor; 4 | 5 | public interface Shape { 6 | void move(int x, int y); 7 | void draw(); 8 | String accept(Visitor visitor); 9 | } 10 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/factory-method/creator.js: -------------------------------------------------------------------------------- 1 | // this class define methods for all products 2 | class Creator { 3 | someOperation() { 4 | const product = this.factoryMethod(); 5 | return `Creator: same creator's code has just worked with ${product.operation()}`; 6 | } 7 | } 8 | 9 | export { Creator }; 10 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/proxy/subject.js: -------------------------------------------------------------------------------- 1 | // some core business logic 2 | class Subject { 3 | // both Subject and Proxy must have this interface, 4 | // so it can be passed to Proxy instead of real Subject 5 | request() { 6 | console.log('Subject: handling request.'); 7 | } 8 | } 9 | 10 | export { Subject }; 11 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/strategy/example/strategies/PayStrategy.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.strategy.example.strategies; 2 | 3 | /** 4 | * Common interface for all strategies. 5 | */ 6 | public interface PayStrategy { 7 | boolean pay(int paymentAmount); 8 | void collectPaymentDetails(); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /courses/guru/js/examples/state.js: -------------------------------------------------------------------------------- 1 | import { Context, State1 } from '../patterns/state'; 2 | 3 | // set context with initial state 4 | const context = new Context(new State1()); 5 | 6 | // switch to other state (to state 2) 7 | context.request1(); 8 | 9 | // swith to another state (back to state 1) 10 | context.request2(); 11 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/adapter/adaptee.js: -------------------------------------------------------------------------------- 1 | // have some behavior, but its interface incompatable with existing client code 2 | // Adaptee needs some adaptation before client can use it 3 | class Adaptee { 4 | incompatableRequest() { 5 | return '.roivaheb elbatapmocni :EETPADA'; 6 | } 7 | } 8 | 9 | export { Adaptee }; 10 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Strategy/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Client: Strategy is set to normal sorting. 2 | Context: Sorting data using the strategy (not sure how it'll do it) 3 | a,b,c,d,e 4 | 5 | Client: Strategy is set to reverse sorting. 6 | Context: Sorting data using the strategy (not sure how it'll do it) 7 | e,d,c,b,a 8 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/chain_of_responsibility/example/OutputDemo.txt: -------------------------------------------------------------------------------- 1 | Enter email: admin@example.com 2 | Input password: admin_pass 3 | Hello, admin! 4 | Authorization have been successful! 5 | 6 | 7 | Enter email: user@example.com 8 | Input password: user_pass 9 | Hello, user! 10 | Authorization have been successful! -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/command/example/Demo.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.command.example; 2 | 3 | import refactoring_guru.command.example.editor.Editor; 4 | 5 | public class Demo { 6 | public static void main(String[] args) { 7 | Editor editor = new Editor(); 8 | editor.init(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/abstract-factory/products/index.js: -------------------------------------------------------------------------------- 1 | import { ProductA1 } from './productA1'; 2 | import { ProductA2 } from './productA2'; 3 | import { ProductB1 } from './productB1'; 4 | import { ProductB2 } from './productB2'; 5 | 6 | export { 7 | ProductA1, 8 | ProductA2, 9 | ProductB1, 10 | ProductB2, 11 | }; 12 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Composite/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Client: I get a simple component: 2 | RESULT: Leaf 3 | 4 | Client: Now I get a composite tree: 5 | RESULT: Branch(Branch(Leaf+Leaf)+Branch(Leaf)) 6 | 7 | Client: I can merge two components without checking their classes: 8 | RESULT: Branch(Branch(Leaf+Leaf)+Branch(Leaf)+Leaf) -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/iterator/example/iterators/ProfileIterator.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.iterator.example.iterators; 2 | 3 | import refactoring_guru.iterator.example.profile.Profile; 4 | 5 | public interface ProfileIterator { 6 | boolean hasNext(); 7 | 8 | Profile getNext(); 9 | 10 | void reset(); 11 | } -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Strategy.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Client: Strategy is set to normal sorting. 2 | Context: Sorting data using the strategy (not sure how it'll do it) 3 | a,b,c,d,e 4 | 5 | Client: Strategy is set to reverse sorting. 6 | Context: Sorting data using the strategy (not sure how it'll do it) 7 | e,d,c,b,a 8 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/observer/observers/observer1.js: -------------------------------------------------------------------------------- 1 | // all Observers must implement this method used by Subjects 2 | class Observer1 { 3 | // recieve update from subject 4 | update(subject) { 5 | if (subject.state < 5) { 6 | console.log('Observer1: react to event.'); 7 | } 8 | } 9 | } 10 | 11 | export { Observer1 }; 12 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Proxy/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Client: Executing the client code with a real subject: 2 | RealSubject: Handling request. 3 | 4 | Client: Executing the same client code with a proxy: 5 | Proxy: Checking access prior to firing a real request. 6 | RealSubject: Handling request. 7 | Proxy: Logging the time of request. 8 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Composite.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Client: I get a simple component: 2 | RESULT: LEAF 3 | 4 | Client: Now I get a composite tree: 5 | RESULT: Branch(Branch(LEAF+LEAF)+Branch(LEAF)) 6 | 7 | Client: I can merge two components without checking their classes: 8 | RESULT: Branch(Branch(LEAF+LEAF)+Branch(LEAF)+LEAF) -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/proxy/example/some_cool_media_library/ThirdPartyYoutubeLib.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.proxy.example.some_cool_media_library; 2 | 3 | import java.util.HashMap; 4 | 5 | public interface ThirdPartyYoutubeLib { 6 | HashMap popularVideos(); 7 | 8 | Video getVideo(String videoId); 9 | } 10 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/bridge/implementations/implementationA.js: -------------------------------------------------------------------------------- 1 | // all implementations must define this interface 2 | // typically implementation perform low-level operations 3 | class ImplementationA { 4 | implementationOperation() { 5 | return 'ImplementationA: result for platform A.'; 6 | } 7 | } 8 | 9 | export { ImplementationA }; 10 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/bridge/implementations/implementationB.js: -------------------------------------------------------------------------------- 1 | // all implementations must define this interface 2 | // typically implementation perform low-level operations 3 | class ImplementationB { 4 | implementationOperation() { 5 | return 'ImplementationB: result for platform B.'; 6 | } 7 | } 8 | 9 | export { ImplementationB }; 10 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Proxy.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Client: Executing the client code with a real subject: 2 | RealSubject: Handling Request. 3 | 4 | Client: Executing the same client code with a proxy: 5 | Proxy: Checking access prior to firing a real request. 6 | RealSubject: Handling Request. 7 | Proxy: Logging the time of request. -------------------------------------------------------------------------------- /courses/guru/js/patterns/state/states/state.js: -------------------------------------------------------------------------------- 1 | // base class for managing backreference to Context associated with State 2 | class State { 3 | #context = null; 4 | 5 | static get context() { 6 | return this.#context; 7 | } 8 | 9 | static set context(context) { 10 | this.#context = context; 11 | } 12 | } 13 | 14 | export { State }; 15 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/abstract-factory/factory1/index.js: -------------------------------------------------------------------------------- 1 | import { ProductA1, ProductB1 } from '../products'; 2 | 3 | // produce family of products of type 1 4 | class Factory1 { 5 | createProductA() { 6 | return new ProductA1(); 7 | } 8 | 9 | createProductB() { 10 | return new ProductB1(); 11 | } 12 | } 13 | 14 | export { Factory1 }; 15 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/abstract-factory/factory2/index.js: -------------------------------------------------------------------------------- 1 | import { ProductA2, ProductB2 } from '../products'; 2 | 3 | // produce family of products of type 2 4 | class Factory2 { 5 | createProductA() { 6 | return new ProductA2(); 7 | } 8 | 9 | createProductB() { 10 | return new ProductB2(); 11 | } 12 | } 13 | 14 | export { Factory2 }; 15 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/simple-factory/product.js: -------------------------------------------------------------------------------- 1 | // product implementation 2 | class Product { 3 | constructor(prop1, prop2) { 4 | this.prop1 = prop1; 5 | this.prop2 = prop2; 6 | } 7 | 8 | getProp1() { 9 | return this.prop1; 10 | } 11 | 12 | getProp2() { 13 | return this.prop2; 14 | } 15 | } 16 | 17 | export { Product }; 18 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Flyweight/RealWorld/cats.csv: -------------------------------------------------------------------------------- 1 | Name,Age,Owner,Breed,Image,Color,Texture,Fur,Size 2 | Steve,3,Alexander Shvets,Bengal,/cats/bengal.jpg,Brown,Stripes,Short,Medium 3 | Siri,2,Alexander Shvets,Domestic short-haired,/cats/domestic-sh.jpg,Black,Solid,Medium,Medium 4 | Fluffy,5,John Smith,Maine Coon,/cats/Maine-Coon.jpg,Gray,Stripes,Long,Large -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Iterator/RealWorld/cats.csv: -------------------------------------------------------------------------------- 1 | Name,Age,Owner,Breed,Image,Color,Texture,Fur,Size 2 | Steve,3,Alexander Shvets,Bengal,/cats/bengal.jpg,Brown,Stripes,Short,Medium 3 | Siri,2,Alexander Shvets,Domestic short-haired,/cats/domestic-sh.jpg,Black,Solid,Medium,Medium 4 | Fluffy,5,John Smith,Maine Coon,/cats/Maine-Coon.jpg,Gray,Stripes,Long,Large -------------------------------------------------------------------------------- /courses/guru/js/patterns/composite/components/component-leaf.js: -------------------------------------------------------------------------------- 1 | import { Component } from './component'; 2 | 3 | // end object of composition 4 | // can't have any children 5 | 6 | // usually these objects do actual work 7 | class ComponentLeaf extends Component { 8 | operation() { 9 | return 'Leaf'; 10 | } 11 | } 12 | 13 | export { ComponentLeaf }; 14 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/factory-method/creator1/creator1.js: -------------------------------------------------------------------------------- 1 | import { Creator } from '../creator'; 2 | import { Product1 } from './product1'; 3 | 4 | // concrete creator override 'factoryMethod' to change returned product type 5 | class Creator1 extends Creator { 6 | factoryMethod() { 7 | return new Product1(); 8 | } 9 | } 10 | 11 | export { Creator1 }; 12 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/observer/observers/observer2.js: -------------------------------------------------------------------------------- 1 | // all Observers must implement this method used by Subjects 2 | class Observer2 { 3 | // recieve update from subject 4 | update(subject) { 5 | if (subject.state === 0 || subject.state >= 4) { 6 | console.log('Observer2: react to event.'); 7 | } 8 | } 9 | } 10 | 11 | export { Observer2 }; 12 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/decorator/example/OutputDemo.txt: -------------------------------------------------------------------------------- 1 | - Input ---------------- 2 | Name,Salary 3 | John Smith,100000 4 | Steven Jobs,912000 5 | - Encoded -------------- 6 | Zkt7e1Q5eU8yUm1Qe0ZsdHJ2VXp6dDBKVnhrUHtUe0sxRUYxQkJIdjVLTVZ0dVI5Q2IwOXFISmVUMU5rcENCQmdxRlByaD4+ 7 | - Decoded -------------- 8 | Name,Salary 9 | John Smith,100000 10 | Steven Jobs,912000 -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/mediator/example/components/Component.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.mediator.example.components; 2 | 3 | import refactoring_guru.mediator.example.mediator.Mediator; 4 | 5 | /** 6 | * Common component interface. 7 | */ 8 | public interface Component { 9 | void setMediator(Mediator mediator); 10 | String getName(); 11 | } 12 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Mediator/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Client triggers operation A. 2 | Component 1 does A. 3 | Mediator reacts on A and triggers following operations: 4 | Component 2 does C. 5 | 6 | Client triggers operation D. 7 | Component 2 does D. 8 | Mediator reacts on D and triggers following operations: 9 | Component 1 does B. 10 | Component 2 does C. 11 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Mediator.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Client triggers operation A. 2 | Component 1 does A. 3 | Mediator reacts on A and triggers following operations: 4 | Component 2 does C. 5 | 6 | Client triggers operation D. 7 | Component 2 does D. 8 | Mediator reacts on D and triggers following operations: 9 | Component 1 does B. 10 | Component 2 does C. -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/facade/example/some_complex_media_library/AudioMixer.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.facade.example.some_complex_media_library; 2 | 3 | import java.io.File; 4 | 5 | public class AudioMixer { 6 | public File fix(VideoFile result){ 7 | System.out.println("AudioMixer: fixing audio..."); 8 | return new File("tmp"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/command/receivers/receiver1.js: -------------------------------------------------------------------------------- 1 | // contains some business logic 2 | // it knows how perform all kinds of operations, associated with request 3 | class Receiver1 { 4 | doSmth(a) { 5 | console.log(`Receiver1: working on (${a})`); 6 | } 7 | 8 | doSmthElse(b) { 9 | console.log(`Receiver1: also working on (${b})`); 10 | } 11 | } 12 | 13 | export { Receiver1 }; 14 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Adapter/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Client: I can work just fine with the Target objects: 2 | Target: The default target's behavior. 3 | 4 | Client: The Adaptee class has a weird interface. See, I don't understand it: 5 | Adaptee: .eetpadA eht fo roivaheb laicepS 6 | 7 | Client: But I can work with it via the Adapter: 8 | Adapter: (TRANSLATED) Special behavior of the Adaptee. -------------------------------------------------------------------------------- /courses/guru/js/patterns/command/commands/simple.js: -------------------------------------------------------------------------------- 1 | // command payload 2 | let privatePayload = null; 3 | 4 | // simple operation made by its own 5 | class SimpleCommand { 6 | constructor(payload) { 7 | privatePayload = payload; 8 | } 9 | 10 | execute() { 11 | console.log(`SimpleCommand: doing simple command (${privatePayload})`); 12 | } 13 | } 14 | 15 | export { SimpleCommand }; 16 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/facade/subsystems/subsystem1.js: -------------------------------------------------------------------------------- 1 | // this class can accept requests either from Facade or client directly 2 | // for this class Facade is just another client 3 | class Subsystem1 { 4 | // some operation 5 | operation1() { 6 | return 'Subsystem1: Ready!\n'; 7 | } 8 | 9 | operationN() { 10 | return 'Subsystem1: Go!\n'; 11 | } 12 | } 13 | 14 | export { Subsystem1 }; 15 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/facade/subsystems/subsystem2.js: -------------------------------------------------------------------------------- 1 | // this class can accept requests either from Facade or client directly 2 | // for this class Facade is just another client 3 | class Subsystem2 { 4 | // some operation 5 | operation1() { 6 | return 'Subsystem2: Ready!\n'; 7 | } 8 | 9 | operationZ() { 10 | return 'Subsystem2: Go!\n'; 11 | } 12 | } 13 | 14 | export { Subsystem2 }; 15 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/AbstractFactory/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | Testing rendering with the Twig factory: 2 |
3 |

{{ title }}

4 |
{{ content }}
5 |
6 | 7 | Testing rendering with the PHPTemplate factory: 8 |
9 |

10 |
11 |
-------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/iterator/example/social_networks/SocialNetwork.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.iterator.example.social_networks; 2 | 3 | import refactoring_guru.iterator.example.iterators.ProfileIterator; 4 | 5 | public interface SocialNetwork { 6 | ProfileIterator createFriendsIterator(String profileEmail); 7 | 8 | ProfileIterator createCoworkersIterator(String profileEmail); 9 | } 10 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/proxy/example/some_cool_media_library/Video.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.proxy.example.some_cool_media_library; 2 | 3 | public class Video { 4 | public String id; 5 | public String title; 6 | public String data; 7 | 8 | Video(String id, String title) { 9 | this.id = id; 10 | this.title = title; 11 | this.data = "Random video."; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/decorator/decorators/decorator.js: -------------------------------------------------------------------------------- 1 | // base Decorator class 2 | // follows Components interface 3 | class Decorator { 4 | constructor(component) { 5 | // field storing wrapped Component 6 | this.component = component; 7 | } 8 | 9 | // delegate all work to wrapped Component 10 | operation() { 11 | return this.component.operation(); 12 | } 13 | } 14 | 15 | export { Decorator }; 16 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/AbstractFactory/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Client: Testing client code with the first factory type: 2 | The result of the product B1. 3 | The result of the B1 collaborating with the (The result of the product A1.) 4 | 5 | Client: Testing the same client code with the second factory type: 6 | The result of the product B2. 7 | The result of the B2 collaborating with the (The result of the product A2.) 8 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Mediator/RealWorld/log.txt: -------------------------------------------------------------------------------- 1 | 2018-05-01 15:30:12: 'users:init' with data '"users.csv"' 2 | 2018-05-01 15:30:12: 'users:created' with data '{"attributes":{"name":"John Smith","email":"john99@example.com","id":"23d8e66d78b4daf57684c20375c0cbcc"}}' 3 | 2018-05-01 15:30:12: 'users:deleted' with data '{"attributes":{"name":"John Smith","email":"john99@example.com","id":"23d8e66d78b4daf57684c20375c0cbcc"}}' 4 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Proxy/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | Executing client code with real subject: 2 | Downloading a file from the Internet. 3 | Downloaded bytes: 1270 4 | Downloading a file from the Internet. 5 | Downloaded bytes: 1270 6 | 7 | Executing the same client code with a proxy: 8 | CacheProxy MISS. Downloading a file from the Internet. 9 | Downloaded bytes: 1270 10 | CacheProxy HIT. Retrieving result from cache. 11 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/bridge/example/devices/Device.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.bridge.example.devices; 2 | 3 | public interface Device { 4 | boolean isEnabled(); 5 | 6 | void enable(); 7 | 8 | void disable(); 9 | 10 | int getVolume(); 11 | 12 | void setVolume(int percent); 13 | 14 | int getChannel(); 15 | 16 | void setChannel(int channel); 17 | 18 | void printStatus(); 19 | } 20 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.AbstractFactory.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Client: Testing client code with the first factory type... 2 | The result of the product B1. 3 | The result of the B1 collaborating with the (The result of the product A1.) 4 | 5 | Client: Testing the same client code with the second factory type... 6 | The result of the product B2. 7 | The result of the B2 collaborating with the (The result of the product A2.) -------------------------------------------------------------------------------- /courses/guru/js/patterns/decorator/decorators/decoratorA.js: -------------------------------------------------------------------------------- 1 | import { Decorator } from './decorator'; 2 | 3 | // concrete Decorator call wrapped object and alter its result in some way 4 | class DecoratorA extends Decorator { 5 | // concrete Decorator may call parent implementation, instead of call wrapped object directly 6 | operation() { 7 | return `DecoratorA: ${super.operation()}`; 8 | } 9 | } 10 | 11 | export { DecoratorA }; 12 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/decorator/decorators/decoratorB.js: -------------------------------------------------------------------------------- 1 | import { Decorator } from './decorator'; 2 | 3 | // concrete Decorator call wrapped object and alter its result in some way 4 | class DecoratorB extends Decorator { 5 | // concrete Decorator may call parent implementation, instead of call wrapped object directly 6 | operation() { 7 | return `DecoratorB: ${super.operation()}`; 8 | } 9 | } 10 | 11 | export { DecoratorB }; 12 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/bridge/index.js: -------------------------------------------------------------------------------- 1 | import { Abstraction } from './abstractions/abstraction'; 2 | import { AbstractionExtended } from './abstractions/abstraction-extended'; 3 | 4 | import { ImplementationA } from './implementations/implementationA'; 5 | import { ImplementationB } from './implementations/implementationB'; 6 | 7 | export { 8 | Abstraction, 9 | AbstractionExtended, 10 | ImplementationA, 11 | ImplementationB, 12 | }; 13 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/template-method/operations/operation1.js: -------------------------------------------------------------------------------- 1 | import { Operation } from './operation'; 2 | 3 | // override some operations with own implementation 4 | class Operation1 extends Operation { 5 | requredOperation1() { 6 | console.log('Operation1: do required operation 1'); 7 | } 8 | 9 | requredOperation2() { 10 | console.log('Operation1: do required operation 2'); 11 | } 12 | } 13 | 14 | export { Operation1 }; 15 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Command/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Invoker: Does anybody want something done before I begin? 2 | SimpleCommand: See, I can do simple things like printing (Say Hi!) 3 | Invoker: ...doing something really important... 4 | Invoker: Does anybody want something done after I finish? 5 | ComplexCommand: Complex stuff should be done by a receiver object. 6 | Receiver: Working on (Send email.) 7 | Receiver: Also working on (Save report.) -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Command.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Invoker: Does anybody want something done before I begin? 2 | SimpleCommand: See, I can do simple things like printing (Say Hi!) 3 | Invoker: ...doing something really important... 4 | Invoker: Does anybody want something done after I finish? 5 | ComplexCommand: Complex stuff should be done by a receiver object. 6 | Receiver: Working on (Send email.) 7 | Receiver: Also working on (Save report.) -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/adapter/example/round/RoundPeg.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.adapter.example.round; 2 | 3 | /** 4 | * RoundPegs are compatible with RoundHoles. 5 | */ 6 | public class RoundPeg { 7 | private double radius; 8 | 9 | public RoundPeg() {} 10 | 11 | public RoundPeg(double radius) { 12 | this.radius = radius; 13 | } 14 | 15 | public double getRadius() { 16 | return radius; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/bridge/abstractions/abstraction-extended.js: -------------------------------------------------------------------------------- 1 | import { Abstraction } from './abstraction'; 2 | 3 | // Abstraction extended without any change in Implementation classes 4 | class AbstractionExtended extends Abstraction { 5 | operation() { 6 | const result = this.implementation.implementationOperation(); 7 | return `Abstraction Extended: Extended operation with: ${result}`; 8 | } 9 | } 10 | 11 | export { AbstractionExtended }; 12 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/visitor/components/component1.js: -------------------------------------------------------------------------------- 1 | // every concrete Component must implement 'accept' method 2 | // it can call corresponding to Component Visitor 3 | class Component1 { 4 | accept(visitor) { 5 | visitor.visitComponent1(this); 6 | } 7 | 8 | // special Component method 9 | // Visitor can use it (it aware of concrete Component) 10 | exclusiveMethodOfComponent1() { 11 | return '1'; 12 | } 13 | } 14 | 15 | export { Component1 }; 16 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/visitor/components/component2.js: -------------------------------------------------------------------------------- 1 | // every concrete Component must implement 'accept' method 2 | // it can call corresponding to Component Visitor 3 | class Component2 { 4 | accept(visitor) { 5 | visitor.visitComponent2(this); 6 | } 7 | 8 | // special Component method 9 | // Visitor can use it (it aware of concrete Component) 10 | specialMethodOfComponent2() { 11 | return '2'; 12 | } 13 | } 14 | 15 | export { Component2 }; 16 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/FactoryMethod/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | App: Launched with the ConcreteCreator1. 2 | Client: I'm not aware of the creator's class, but it still works. 3 | Creator: The same creator's code has just worked with {Result of the ConcreteProduct1} 4 | 5 | App: Launched with the ConcreteCreator2. 6 | Client: I'm not aware of the creator's class, but it still works. 7 | Creator: The same creator's code has just worked with {Result of the ConcreteProduct2} -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/State/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Context: Transition to RefactoringGuru\State\Structural\ConcreteStateA. 2 | ConcreteStateA handles request1. 3 | ConcreteStateA wants to change the state of the context. 4 | Context: Transition to RefactoringGuru\State\Structural\ConcreteStateB. 5 | ConcreteStateB handles request2. 6 | ConcreteStateB wants to change the state of the context. 7 | Context: Transition to RefactoringGuru\State\Structural\ConcreteStateA. 8 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.FactoryMethod.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | App: Launched with the ConcreteCreator1. 2 | Client: I'm not aware of the creator's class, but it still works. 3 | Creator: The same creator's code has just worked with {Result of ConcreteProduct1} 4 | 5 | App: Launched with the ConcreteCreator2. 6 | Client: I'm not aware of the creator's class, but it still works. 7 | Creator: The same creator's code has just worked with {Result of ConcreteProduct2} -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/abstract_factory/example/buttons/MacOSButton.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.abstract_factory.example.buttons; 2 | 3 | /** 4 | * All products families have the same varieties (MacOS/Windows). 5 | * 6 | * This is a MacOS variant of a button. 7 | */ 8 | public class MacOSButton implements Button { 9 | 10 | @Override 11 | public void paint() { 12 | System.out.println("You have created MacOSButton."); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/state/example/Demo.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.state.example; 2 | 3 | import refactoring_guru.state.example.ui.Player; 4 | import refactoring_guru.state.example.ui.UI; 5 | 6 | /** 7 | * Demo class. Everything comes together here. 8 | */ 9 | public class Demo { 10 | public static void main(String[] args) { 11 | Player player = new Player(); 12 | UI ui = new UI(player); 13 | ui.init(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/template_method/example/OutputDemo.txt: -------------------------------------------------------------------------------- 1 | Input user name: Jhonatan 2 | Input password: qswe 3 | Input message: Hello, World! 4 | 5 | Choose social network for posting message. 6 | 1 - Facebook 7 | 2 - Twitter 8 | 2 9 | 10 | Checking user's parameters 11 | Name: Jhonatan 12 | Password: **** 13 | .......... 14 | 15 | LogIn success on Twitter 16 | Message: 'Hello, World!' was posted on Twitter 17 | User: 'Jhonatan' was logged out from Twitter -------------------------------------------------------------------------------- /courses/guru/js/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | es6: true, 4 | node: true, 5 | }, 6 | extends: 'airbnb-base', 7 | globals: { 8 | Atomics: 'readonly', 9 | SharedArrayBuffer: 'readonly', 10 | }, 11 | parserOptions: { 12 | ecmaVersion: 2018, 13 | sourceType: 'module', 14 | }, 15 | rules: { 16 | 'class-methods-use-this': 'off', 17 | 'import/prefer-default-export': 'off', 18 | 'no-console': 'off' 19 | }, 20 | }; 21 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/abstract-factory/products/productB1.js: -------------------------------------------------------------------------------- 1 | // product factory 2 | class ProductB1 { 3 | // products of family B must implement this method 4 | productBMethod() { 5 | return 'Product B1'; 6 | } 7 | 8 | // and method collaborating with product A1 9 | productBMethod2(collaborator) { 10 | const result = collaborator.productAMethod(); 11 | return `Product B1 collaborating with ${result}`; 12 | } 13 | } 14 | 15 | export { ProductB1 }; 16 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/abstract-factory/products/productB2.js: -------------------------------------------------------------------------------- 1 | // product factory 2 | class ProductB2 { 3 | // products of family B must implement this method 4 | productBMethod() { 5 | return 'Product B2'; 6 | } 7 | 8 | // and method collaborating with product A2 9 | productBMethod2(collaborator) { 10 | const result = collaborator.productAMethod(); 11 | return `Product B2 collaborating with ${result}`; 12 | } 13 | } 14 | 15 | export { ProductB2 }; 16 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/adapter/adapter.js: -------------------------------------------------------------------------------- 1 | import { Target } from './target'; 2 | 3 | // makes Adaptee interface compatible with Target interface 4 | class Adapter extends Target { 5 | constructor(adaptee) { 6 | super(); 7 | this.adaptee = adaptee; 8 | } 9 | 10 | request() { 11 | const result = this.adaptee.incompatableRequest().split('').reverse().join(''); 12 | return `ADAPTER: (TRANSLATED) ${result}`; 13 | } 14 | } 15 | 16 | export { Adapter }; 17 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/chain-of-responsibility/handlers/handler.js: -------------------------------------------------------------------------------- 1 | // default chaining behavoir 2 | class AbstractHandler { 3 | // build chain of handlers 4 | setNext(handler) { 5 | this.nextHandler = handler; 6 | return handler; 7 | } 8 | 9 | // execute request 10 | handle(request) { 11 | if (this.nextHandler) { 12 | return this.nextHandler.handle(request); 13 | } 14 | return null; 15 | } 16 | } 17 | 18 | export { AbstractHandler }; 19 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/abstract_factory/example/buttons/WindowsButton.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.abstract_factory.example.buttons; 2 | 3 | /** 4 | * All products families have the same varieties (MacOS/Windows). 5 | * 6 | * This is another variant of a button. 7 | */ 8 | public class WindowsButton implements Button { 9 | 10 | @Override 11 | public void paint() { 12 | System.out.println("You have created WindowsButton."); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/facade/example/Demo.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.facade.example; 2 | 3 | import refactoring_guru.facade.example.facade.VideoConversionFacade; 4 | 5 | import java.io.File; 6 | 7 | public class Demo { 8 | public static void main(String[] args) { 9 | VideoConversionFacade converter = new VideoConversionFacade(); 10 | File mp4Video = converter.convertVideo("youtubevideo.ogg", "mp4"); 11 | // ... 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/abstract_factory/example/checkboxes/MacOSCheckbox.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.abstract_factory.example.checkboxes; 2 | 3 | /** 4 | * All products families have the same varieties (MacOS/Windows). 5 | * 6 | * This is a variant of a checkbox. 7 | */ 8 | public class MacOSCheckbox implements Checkbox { 9 | 10 | @Override 11 | public void paint() { 12 | System.out.println("You have created MacOSCheckbox."); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/bridge/example/remotes/AdvancedRemote.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.bridge.example.remotes; 2 | 3 | import refactoring_guru.bridge.example.devices.Device; 4 | 5 | public class AdvancedRemote extends BasicRemote { 6 | 7 | public AdvancedRemote(Device device) { 8 | super.device = device; 9 | } 10 | 11 | public void mute() { 12 | System.out.println("Remote: mute"); 13 | device.setVolume(0); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/composite/example/shapes/Shape.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.composite.example.shapes; 2 | 3 | import java.awt.*; 4 | 5 | public interface Shape { 6 | int getX(); 7 | int getY(); 8 | int getWidth(); 9 | int getHeight(); 10 | void move(int x, int y); 11 | boolean isInsideBounds(int x, int y); 12 | void select(); 13 | void unSelect(); 14 | boolean isSelected(); 15 | void paint(Graphics graphics); 16 | } 17 | -------------------------------------------------------------------------------- /courses/guru/js/examples/strategy.js: -------------------------------------------------------------------------------- 1 | import { Context, Strategy1, Strategy2 } from '../patterns/strategy'; 2 | 3 | // create context and use some Strategy 4 | const context = new Context(new Strategy1()); 5 | console.log('Client: strategy with normal sorting.'); 6 | context.businessLogic(); 7 | 8 | console.log(''); 9 | 10 | // use another Strategy 11 | console.log('Client: strategy with reverse sorting.'); 12 | context.setStrategy(new Strategy2()); 13 | context.businessLogic(); 14 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/abstract_factory/example/factories/GUIFactory.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.abstract_factory.example.factories; 2 | 3 | import refactoring_guru.abstract_factory.example.buttons.Button; 4 | import refactoring_guru.abstract_factory.example.checkboxes.Checkbox; 5 | 6 | /** 7 | * Abstract factory knows about all (abstract) product types. 8 | */ 9 | public interface GUIFactory { 10 | Button createButton(); 11 | Checkbox createCheckbox(); 12 | } 13 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/TemplateMethod/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | Username: 2 | > neo 3 | Password: 4 | > 123123 5 | Message: 6 | > What is the Matrix? 7 | 8 | Choose the social network to post the message: 9 | 1 - Facebook 10 | 2 - Twitter 11 | > 1 12 | 13 | Checking user's credentials... 14 | Name: neo 15 | Password: ****** 16 | ..... 17 | 18 | Facebook: 'neo' has logged in successfully. 19 | Facebook: 'neo' has posted 'What is the Matrix?'. 20 | Facebook: 'neo' has been logged out. -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/abstract_factory/example/buttons/Button.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.abstract_factory.example.buttons; 2 | 3 | /** 4 | * Abstract Factory assumes that you have several families of products, 5 | * structured into separate class hierarchies (Button/Checkbox). All products of 6 | * the same family have the common interface. 7 | * 8 | * This is the common interface for buttons family. 9 | */ 10 | public interface Button { 11 | void paint(); 12 | } 13 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/abstract_factory/example/checkboxes/WindowsCheckbox.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.abstract_factory.example.checkboxes; 2 | 3 | /** 4 | * All products families have the same varieties (MacOS/Windows). 5 | * 6 | * This is another variant of a checkbox. 7 | */ 8 | public class WindowsCheckbox implements Checkbox { 9 | 10 | @Override 11 | public void paint() { 12 | System.out.println("You have created WindowsCheckbox."); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/chain-of-responsibility/handlers/handler1.js: -------------------------------------------------------------------------------- 1 | import { AbstractHandler } from './handler'; 2 | 3 | // concrete Handler either handle request or pass it to next handler 4 | class Handler1 extends AbstractHandler { 5 | handle(request) { 6 | // handle request 7 | if (request === 1) { 8 | return `Handler1: handle ${request}`; 9 | } 10 | // pass it to next handler 11 | return super.handle(request); 12 | } 13 | } 14 | 15 | export { Handler1 }; 16 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/chain-of-responsibility/handlers/handler2.js: -------------------------------------------------------------------------------- 1 | import { AbstractHandler } from './handler'; 2 | 3 | // concrete Handler either handle request or pass it to next handler 4 | class Handler2 extends AbstractHandler { 5 | handle(request) { 6 | // handle request 7 | if (request === 2) { 8 | return `Handler2: handle ${request}`; 9 | } 10 | // pass it to next handler 11 | return super.handle(request); 12 | } 13 | } 14 | 15 | export { Handler2 }; 16 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/chain-of-responsibility/handlers/handler3.js: -------------------------------------------------------------------------------- 1 | import { AbstractHandler } from './handler'; 2 | 3 | // concrete Handler either handle request or pass it to next handler 4 | class Handler3 extends AbstractHandler { 5 | handle(request) { 6 | // handle request 7 | if (request === 3) { 8 | return `Handler3: handle ${request}`; 9 | } 10 | // pass it to next handler 11 | return super.handle(request); 12 | } 13 | } 14 | 15 | export { Handler3 }; 16 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/flyweight/example/trees/Tree.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.flyweight.example.trees; 2 | 3 | import java.awt.*; 4 | 5 | public class Tree { 6 | private int x; 7 | private int y; 8 | private TreeType type; 9 | 10 | public Tree(int x, int y, TreeType type) { 11 | this.x = x; 12 | this.y = y; 13 | this.type = type; 14 | } 15 | 16 | public void draw(Graphics g) { 17 | type.draw(g, x, y); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/command/example/commands/CommandHistory.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.command.example.commands; 2 | 3 | import java.util.Stack; 4 | 5 | public class CommandHistory { 6 | private Stack history = new Stack<>(); 7 | 8 | public void push(Command c) { 9 | history.push(c); 10 | } 11 | 12 | public Command pop() { 13 | return history.pop(); 14 | } 15 | 16 | public boolean isEmpty() { return history.isEmpty(); } 17 | } 18 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/command/example/commands/CopyCommand.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.command.example.commands; 2 | 3 | import refactoring_guru.command.example.editor.Editor; 4 | 5 | public class CopyCommand extends Command { 6 | 7 | public CopyCommand(Editor editor) { 8 | super(editor); 9 | } 10 | 11 | @Override 12 | public boolean execute() { 13 | editor.clipboard = editor.textField.getSelectedText(); 14 | return false; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/factory_method/example/buttons/HtmlButton.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.factory_method.example.buttons; 2 | 3 | /** 4 | * HTML button implementation. 5 | */ 6 | public class HtmlButton implements Button { 7 | 8 | public void render() { 9 | System.out.println(""); 10 | onClick(); 11 | } 12 | 13 | public void onClick() { 14 | System.out.println("Click! Button says - 'Hello World!'"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/memento/example/history/Memento.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.memento.example.history; 2 | 3 | import refactoring_guru.memento.example.editor.Editor; 4 | 5 | public class Memento { 6 | private String backup; 7 | private Editor editor; 8 | 9 | public Memento(Editor editor) { 10 | this.editor = editor; 11 | this.backup = editor.backup(); 12 | } 13 | 14 | public void restore() { 15 | editor.restore(backup); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/mediator/components/component.js: -------------------------------------------------------------------------------- 1 | // reference to connected Mediator 2 | let privateMediator = null; 3 | 4 | // provides basic functionality of saving Mediator instances inside Component objects 5 | class Component { 6 | constructor(mediator) { 7 | privateMediator = mediator; 8 | } 9 | 10 | get mediator() { 11 | return privateMediator; 12 | } 13 | 14 | set mediator(mediator) { 15 | privateMediator = mediator; 16 | } 17 | } 18 | 19 | export { Component }; 20 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/factory_method/example/factory/HtmlDialog.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.factory_method.example.factory; 2 | 3 | import refactoring_guru.factory_method.example.buttons.Button; 4 | import refactoring_guru.factory_method.example.buttons.HtmlButton; 5 | 6 | /** 7 | * HTML Dialog will produce HTML buttons. 8 | */ 9 | public class HtmlDialog extends Dialog { 10 | 11 | @Override 12 | public Button createButton() { 13 | return new HtmlButton(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/mediator/components/component1.js: -------------------------------------------------------------------------------- 1 | import { Component } from './component'; 2 | 3 | // implement specific functionality 4 | // do not depend on other Component 5 | // do not depend on any Mediator 6 | class Component1 extends Component { 7 | doA() { 8 | console.log('Component1 do A.'); 9 | this.mediator.notify(this, 'A'); 10 | } 11 | 12 | doB() { 13 | console.log('Component1 do B.'); 14 | this.mediator.notify(this, 'B'); 15 | } 16 | } 17 | 18 | export { Component1 }; 19 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/mediator/components/component2.js: -------------------------------------------------------------------------------- 1 | import { Component } from './component'; 2 | 3 | // implement specific functionality 4 | // do not depend on other Component 5 | // do not depend on any Mediator 6 | class Component2 extends Component { 7 | doC() { 8 | console.log('Component2 do C.'); 9 | this.mediator.notify(this, 'C'); 10 | } 11 | 12 | doD() { 13 | console.log('Component2 do D.'); 14 | this.mediator.notify(this, 'D'); 15 | } 16 | } 17 | 18 | export { Component2 }; 19 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/template-method/operations/operation2.js: -------------------------------------------------------------------------------- 1 | import { Operation } from './operation'; 2 | 3 | // override some operations with own implementation 4 | class Operation2 extends Operation { 5 | requredOperation1() { 6 | console.log('Operation2: do required operation 1'); 7 | } 8 | 9 | requredOperation2() { 10 | console.log('Operation2: do required operation 2'); 11 | } 12 | 13 | hook1() { 14 | console.log('Opearation2: hook 1'); 15 | } 16 | } 17 | 18 | export { Operation2 }; 19 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/visitor/visitors/visitor1.js: -------------------------------------------------------------------------------- 1 | // implement set of visiting methods corresponding to Components 2 | class Visitor1 { 3 | // visit one Component and execute it method 4 | visitComponent1(element) { 5 | console.log(`${element.exclusiveMethodOfComponent1()} + Visitor 1`); 6 | } 7 | 8 | // visit another Component and execute it method 9 | visitComponent2(element) { 10 | console.log(`${element.specialMethodOfComponent2()} + Visitor 1`); 11 | } 12 | } 13 | 14 | export { Visitor1 }; 15 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/visitor/visitors/visitor2.js: -------------------------------------------------------------------------------- 1 | // implement set of visiting methods corresponding to Components 2 | class Visitor2 { 3 | // visit one Component and execute it method 4 | visitComponent1(element) { 5 | console.log(`${element.exclusiveMethodOfComponent1()} + Visitor 2`); 6 | } 7 | 8 | // visit another Component and execute it method 9 | visitComponent2(element) { 10 | console.log(`${element.specialMethodOfComponent2()} + Visitor 2`); 11 | } 12 | } 13 | 14 | export { Visitor2 }; 15 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.State.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Context: Transition to RefactoringGuru.DesignPatterns.State.Structural.ConcreteStateA. 2 | ConcreteStateA handles request1. 3 | ConcreteStateA wants to change the state of the context. 4 | Context: Transition to RefactoringGuru.DesignPatterns.State.Structural.ConcreteStateB. 5 | ConcreteStateB handles request2. 6 | ConcreteStateB wants to change the state of the context. 7 | Context: Transition to RefactoringGuru.DesignPatterns.State.Structural.ConcreteStateA. -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/factory_method/example/factory/WindowsDialog.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.factory_method.example.factory; 2 | 3 | import refactoring_guru.factory_method.example.buttons.Button; 4 | import refactoring_guru.factory_method.example.buttons.WindowsButton; 5 | 6 | /** 7 | * Windows Dialog will produce Windows buttons. 8 | */ 9 | public class WindowsDialog extends Dialog { 10 | 11 | @Override 12 | public Button createButton() { 13 | return new WindowsButton(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Observer/RealWorld/log.txt: -------------------------------------------------------------------------------- 1 | 2018-06-04 14:59:48: 'users:init' with data '"\/Users\/neochief\/www\/dev.refactoring.guru\/resources\/examples\/design-patterns-php\/src\/RefactoringGuru\/Observer\/RealWorld\/users.csv"' 2 | 2018-06-04 14:59:48: 'users:created' with data '{"attributes":{"name":"John Smith","email":"john99@example.com","id":"75b7f717bae23472ee1665bf5bfd2425"}}' 3 | 2018-06-04 14:59:48: 'users:deleted' with data '{"attributes":{"name":"John Smith","email":"john99@example.com","id":"75b7f717bae23472ee1665bf5bfd2425"}}' 4 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/facade/example/some_complex_media_library/BitrateReader.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.facade.example.some_complex_media_library; 2 | 3 | public class BitrateReader { 4 | public static VideoFile read(VideoFile file, Codec codec) { 5 | System.out.println("BitrateReader: reading file..."); 6 | return file; 7 | } 8 | 9 | public static VideoFile convert(VideoFile buffer, Codec codec) { 10 | System.out.println("BitrateReader: writing file..."); 11 | return buffer; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /courses/guru/js/examples/command.js: -------------------------------------------------------------------------------- 1 | import { 2 | SimpleCommand, 3 | ComplexCommand, 4 | Invoker1, 5 | Receiver1, 6 | } from '../patterns/command'; 7 | 8 | // client code can parameterize Invoker with any Command 9 | 10 | const invoker1 = new Invoker1(); 11 | const receiver1 = new Receiver1(); 12 | 13 | /* --- do simple command --- */ 14 | invoker1.setOnStart(new SimpleCommand('Hi!')); 15 | 16 | /* --- do complex command --- */ 17 | invoker1.setOnFinish(new ComplexCommand(receiver1, 'Send email', 'Save report')); 18 | 19 | invoker1.doSmthImportant(); 20 | -------------------------------------------------------------------------------- /courses/guru/js/examples/proxy.js: -------------------------------------------------------------------------------- 1 | import { Subject, Proxy } from '../patterns/proxy'; 2 | 3 | // client can work through both Subject and Proxy interface 4 | function clientCode(subject) { 5 | subject.request(); 6 | } 7 | 8 | /* --- make request directly to Subject --- */ 9 | 10 | console.log('Client: execute Subject method...'); 11 | const subject = new Subject(); 12 | clientCode(subject); 13 | 14 | /* --- make request over Proxy --- */ 15 | 16 | console.log('Client: execute Proxy method...'); 17 | const proxy = new Proxy(subject); 18 | clientCode(proxy); 19 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/flyweight/flyweight.js: -------------------------------------------------------------------------------- 1 | // store intrinsic and add extrinsic states 2 | class Flyweight { 3 | // store intrinsic (common) state that belongs to many entities 4 | constructor(sharedState) { 5 | this.sharedState = sharedState; 6 | } 7 | 8 | // add extrinsic (unique) state 9 | operation(uniqueState) { 10 | const s = JSON.stringify(this.sharedState); 11 | const u = JSON.stringify(uniqueState); 12 | console.log(`Flyweight: Shared state: (${s}); Unique state: (${u})`); 13 | } 14 | } 15 | 16 | export { Flyweight }; 17 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/builder/director.js: -------------------------------------------------------------------------------- 1 | // optional class. just used to build some predefined products 2 | class Director { 3 | // set specific builder 4 | setBuilder(builder) { 5 | this.builder = builder; 6 | } 7 | 8 | // some predefined product 9 | buildMinimalProduct() { 10 | this.builder.producePartA(); 11 | } 12 | 13 | // some predefined product 14 | buildFullProduct() { 15 | this.builder.producePartA(); 16 | this.builder.producePartB(); 17 | this.builder.producePartC(); 18 | } 19 | } 20 | 21 | export { Director }; 22 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/state/states/state1.js: -------------------------------------------------------------------------------- 1 | import { State } from './state'; 2 | import { State2 } from './state2'; 3 | 4 | // implement behaviors associated with Context state 5 | // all concrete States must implement these methods 6 | class State1 extends State { 7 | handle1() { 8 | console.log('State1: handle request1.'); 9 | console.log('State1: change state of context.'); 10 | this.context.transitionTo(new State2()); 11 | } 12 | 13 | handle2() { 14 | console.log('State1: handle request2.'); 15 | } 16 | } 17 | 18 | export { State1 }; 19 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/state/states/state2.js: -------------------------------------------------------------------------------- 1 | import { State } from './state'; 2 | import { State1 } from './state1'; 3 | 4 | // implement behaviors associated with Context state 5 | // all concrete States must implement these methods 6 | class State2 extends State { 7 | handle1() { 8 | console.log('State2: handle request1.'); 9 | } 10 | 11 | handle2() { 12 | console.log('State2: handle request2.'); 13 | console.log('State2: change state of context.'); 14 | this.context.transitionTo(new State1()); 15 | } 16 | } 17 | 18 | export { State2 }; 19 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/ChainOfResponsibility/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Chain: Monkey > Squirrel > Dog 2 | 3 | Client: Who wants a Nut? 4 | Squirrel: I'll eat the Nut. 5 | Client: Who wants a Banana? 6 | Monkey: I'll eat the Banana. 7 | Client: Who wants a Cup of coffee? 8 | Cup of coffee was left untouched. 9 | 10 | Subchain: Squirrel > Dog 11 | 12 | Client: Who wants a Nut? 13 | Squirrel: I'll eat the Nut. 14 | Client: Who wants a Banana? 15 | Banana was left untouched. 16 | Client: Who wants a Cup of coffee? 17 | Cup of coffee was left untouched. 18 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/decorator/example/decorators/DataSourceDecorator.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.decorator.example.decorators; 2 | 3 | public class DataSourceDecorator implements DataSource { 4 | private DataSource wrappee; 5 | 6 | DataSourceDecorator(DataSource source) { 7 | this.wrappee = source; 8 | } 9 | 10 | @Override 11 | public void writeData(String data) { 12 | wrappee.writeData(data); 13 | } 14 | 15 | @Override 16 | public String readData() { 17 | return wrappee.readData(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/facade/example/some_complex_media_library/VideoFile.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.facade.example.some_complex_media_library; 2 | 3 | public class VideoFile { 4 | private String name; 5 | private String codecType; 6 | 7 | public VideoFile(String name) { 8 | this.name = name; 9 | this.codecType = name.substring(name.indexOf(".") + 1); 10 | } 11 | 12 | public String getCodecType() { 13 | return codecType; 14 | } 15 | 16 | public String getName() { 17 | return name; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /courses/guru/js/examples/mediator.js: -------------------------------------------------------------------------------- 1 | import { Component1, Component2, Mediator1 } from '../patterns/mediator'; 2 | 3 | const component1 = new Component1(); 4 | const component2 = new Component2(); 5 | // eslint-disable-next-line no-unused-vars 6 | const mediator1 = new Mediator1(component1, component2); 7 | 8 | /* --- call Component1 method --- */ 9 | 10 | console.log('Client trigger operation A...'); 11 | component1.doA(); 12 | 13 | console.log(''); 14 | 15 | /* --- call Component2 method --- */ 16 | 17 | console.log('Client trigger oparation D...'); 18 | component2.doD(); 19 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.ChainOfResponsibility.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Chain: Monkey > Squirerel > Dog 2 | 3 | Client: Who wants a Nut? 4 | Squirrel: I'll eat the Nut.Client: Who wants a Banana? 5 | Monkey: I'll eat the Banana 6 | Client: Who wants a Cup of coffee? 7 | Cup of coffee was left untouched. 8 | 9 | Subchain: Squirrel > Dog 10 | 11 | Client: Who wants a Nut? 12 | Squirrel: I'll eat the Nut.Client: Who wants a Banana? 13 | Banana was left untouched. 14 | Client: Who wants a Cup of coffee? 15 | Cup of coffee was left untouched. -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Flyweight/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | Client: Let's see what we have in "cats.csv". 2 | CatDataBase: Added a cat (Steve, Bengal). 3 | CatDataBase: Added a cat (Siri, Domestic short-haired). 4 | CatDataBase: Added a cat (Fluffy, Maine Coon). 5 | 6 | Client: Let's look for a cat named "Siri". 7 | = Siri = 8 | Age: 2 9 | Owner: Alexander Shvets 10 | Breed: Domestic short-haired 11 | Image: /cats/domestic-sh.jpg 12 | Color: Black 13 | Texture: Solid 14 | 15 | Client: Let's look for a cat named "Bob". 16 | CatDataBase: Sorry, your query does not yield any results. -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/builder/example/components/GPSNavigator.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.builder.example.components; 2 | 3 | /** 4 | * Just another feature of a car. 5 | */ 6 | public class GPSNavigator { 7 | private String route; 8 | 9 | public GPSNavigator() { 10 | this.route = "221b, Baker Street, London to Scotland Yard, 8-10 Broadway, London"; 11 | } 12 | 13 | public GPSNavigator(String manualRoute) { 14 | this.route = manualRoute; 15 | } 16 | 17 | public String getRoute() { 18 | return route; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/iterator/collection1/index.js: -------------------------------------------------------------------------------- 1 | import { Iterator1 } from './iterator1'; 2 | 3 | const items = []; 4 | 5 | // Collection provide some methods for retreiving items 6 | class Collection1 { 7 | getItems() { 8 | return items; 9 | } 10 | 11 | getCount() { 12 | return items.length; 13 | } 14 | 15 | addItem(item) { 16 | items.push(item); 17 | } 18 | 19 | getIterator() { 20 | return new Iterator1(this); 21 | } 22 | 23 | getReverseIterator() { 24 | return new Iterator1(this, true); 25 | } 26 | } 27 | 28 | export { Collection1 }; 29 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Composite/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 |
2 |

Add product

3 | 4 | 5 | 6 | 7 |
Product photo 8 | 9 | 10 | 11 | 12 |
13 |
14 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Adapter/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | Client code is designed correctly and works with email notifications: 2 | Sent email with title 'Website is down!' to 'developers@example.com' that says 'Alert! Our website is not responding. Call admins and bring it up!'. 3 | 4 | The same client code can work with other classes via adapter: 5 | Logged in to a slack account 'example.com'. 6 | Posted following message into the 'Example.com Developers' chat: '#Website is down!# Alert! Our website is not responding. Call admins and bring it up!'. 7 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Observer/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | UserRepository: Loading user records from a file. 2 | UserRepository: Broadcasting the 'users:init' event. 3 | Logger: I've written 'users:init' entry to the log. 4 | UserRepository: Creating a user. 5 | UserRepository: Broadcasting the 'users:created' event. 6 | OnboardingNotification: The notification has been emailed! 7 | Logger: I've written 'users:created' entry to the log. 8 | UserRepository: Deleting a user. 9 | UserRepository: Broadcasting the 'users:deleted' event. 10 | Logger: I've written 'users:deleted' entry to the log. 11 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/adapter/example/round/RoundHole.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.adapter.example.round; 2 | 3 | /** 4 | * RoundHoles are compatible with RoundPegs. 5 | */ 6 | public class RoundHole { 7 | private double radius; 8 | 9 | public RoundHole(double radius) { 10 | this.radius = radius; 11 | } 12 | 13 | public double getRadius() { 14 | return radius; 15 | } 16 | 17 | public boolean fits(RoundPeg peg) { 18 | boolean result; 19 | result = (this.getRadius() >= peg.getRadius()); 20 | return result; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/visitor/example/shapes/Circle.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.visitor.example.shapes; 2 | 3 | import refactoring_guru.visitor.example.visitor.Visitor; 4 | 5 | public class Circle extends Dot { 6 | private int radius; 7 | 8 | public Circle(int id, int x, int y, int radius) { 9 | super(id, x, y); 10 | this.radius = radius; 11 | } 12 | 13 | @Override 14 | public String accept(Visitor visitor) { 15 | return visitor.visitCircle(this); 16 | } 17 | 18 | public int getRadius() { 19 | return radius; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /courses/guru/js/examples/facade.js: -------------------------------------------------------------------------------- 1 | import { Facade, Subsystem1, Subsystem2 } from '../patterns/facade'; 2 | 3 | // client code works with complex subsystems through simple Facade interface 4 | function clientCode(facade) { 5 | console.log(facade.operation()); 6 | } 7 | 8 | /* --- manual subsystems initialization --- */ 9 | const subsystem1 = new Subsystem1(); 10 | const subsystem2 = new Subsystem2(); 11 | const facade1 = new Facade(subsystem1, subsystem2); 12 | clientCode(facade1); 13 | 14 | /* --- Facade itself creates subsystem instances --- */ 15 | const facade2 = new Facade(); 16 | clientCode(facade2); 17 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/bridge/abstractions/abstraction.js: -------------------------------------------------------------------------------- 1 | // interface for "control" of two class hierarchies 2 | // typically abstraction perform high-level operations 3 | 4 | // maintains reference to object of Implementation hierarchy 5 | // and delegates all real work to this object 6 | class Abstraction { 7 | constructor(implementation) { 8 | this.implementation = implementation; 9 | } 10 | 11 | operation() { 12 | const result = this.implementation.implementationOperation(); 13 | return `Abstraction: Base operation with: ${result}`; 14 | } 15 | } 16 | 17 | export { Abstraction }; 18 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/command/example/commands/Command.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.command.example.commands; 2 | 3 | import refactoring_guru.command.example.editor.Editor; 4 | 5 | public abstract class Command { 6 | public Editor editor; 7 | private String backup; 8 | 9 | Command(Editor editor) { 10 | this.editor = editor; 11 | } 12 | 13 | void backup() { 14 | backup = editor.textField.getText(); 15 | } 16 | 17 | public void undo() { 18 | editor.textField.setText(backup); 19 | } 20 | 21 | public abstract boolean execute(); 22 | } 23 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/visitor/example/visitor/Visitor.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.visitor.example.visitor; 2 | 3 | import refactoring_guru.visitor.example.shapes.Circle; 4 | import refactoring_guru.visitor.example.shapes.CompoundShape; 5 | import refactoring_guru.visitor.example.shapes.Dot; 6 | import refactoring_guru.visitor.example.shapes.Rectangle; 7 | 8 | public interface Visitor { 9 | String visitDot(Dot dot); 10 | 11 | String visitCircle(Circle circle); 12 | 13 | String visitRectangle(Rectangle rectangle); 14 | 15 | String visitCompoundGraphic(CompoundShape cg); 16 | } 17 | -------------------------------------------------------------------------------- /courses/guru/js/examples/factory-method.js: -------------------------------------------------------------------------------- 1 | import { Creator1, Creator2 } from '../patterns/factory-method'; 2 | 3 | /* --- use creator #1 --- */ 4 | console.log('App: launched with Creator1.'); 5 | // pick desired creator class 6 | const creator1 = new Creator1(); 7 | // do some work with instance of concrete creator 8 | console.log(creator1.someOperation()); 9 | 10 | /* --- use creator #2 --- */ 11 | console.log('App: launched with Creator2.'); 12 | // pick desired creator class 13 | const creator2 = new Creator2(); 14 | // do some work with instance of concrete creator 15 | console.log(creator2.someOperation()); 16 | -------------------------------------------------------------------------------- /courses/guru/js/examples/simple-factory.js: -------------------------------------------------------------------------------- 1 | import { ProductFactory } from '../patterns/simple-factory'; 2 | 3 | /* --- make product #1 --- */ 4 | const product1 = ProductFactory.makeProduct('property-A-1', 'property-B-1'); 5 | console.log('Product #1: property #1: %s', product1.getProp1()); 6 | console.log('Product #1: property #2: %s', product1.getProp2()); 7 | 8 | /* --- make product #2 --- */ 9 | const product2 = ProductFactory.makeProduct('property-A-2', 'property-B-2'); 10 | console.log('Product #2: property #1: %s', product2.getProp1()); 11 | console.log('Product #2: property #2: %s', product2.getProp2()); 12 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/chain_of_responsibility/example/middleware/RoleCheckMiddleware.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.chain_of_responsibility.example.middleware; 2 | 3 | /** 4 | * ConcreteHandler. Checks a user's role. 5 | */ 6 | public class RoleCheckMiddleware extends Middleware { 7 | public boolean check(String email, String password) { 8 | if (email.equals("admin@example.com")) { 9 | System.out.println("Hello, admin!"); 10 | return true; 11 | } 12 | System.out.println("Hello, user!"); 13 | return checkNext(email, password); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/observer/example/listeners/LogOpenListener.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.observer.example.listeners; 2 | 3 | import java.io.File; 4 | 5 | public class LogOpenListener implements EventListener { 6 | private File log; 7 | 8 | public LogOpenListener(String fileName) { 9 | this.log = new File(fileName); 10 | } 11 | 12 | @Override 13 | public void update(String eventType, File file) { 14 | System.out.println("Save to log " + log + ": Someone has performed " + eventType + " operation with the following file: " + file.getName()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/prototype/prototype.js: -------------------------------------------------------------------------------- 1 | // class supporting cloning must implement 'clone()' method 2 | class Prototype { 3 | constructor() { 4 | this.primitive = null; 5 | this.component = {}; 6 | this.circularReference = null; 7 | } 8 | 9 | clone() { 10 | // create new 'Prototype' object 11 | const clone = Object.create(this); 12 | // fill this new objects with keys and its values 13 | clone.component = Object.assign(this.component); 14 | clone.circularReference = Object.assign(this.circularReference); 15 | return clone; 16 | } 17 | } 18 | 19 | export { Prototype }; 20 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/observer/example/listeners/EmailNotificationListener.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.observer.example.listeners; 2 | 3 | import java.io.File; 4 | 5 | public class EmailNotificationListener implements EventListener { 6 | private String email; 7 | 8 | public EmailNotificationListener(String email) { 9 | this.email = email; 10 | } 11 | 12 | @Override 13 | public void update(String eventType, File file) { 14 | System.out.println("Email to " + email + ": Someone has performed " + eventType + " operation with the following file: " + file.getName()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/command/example/commands/PasteCommand.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.command.example.commands; 2 | 3 | import refactoring_guru.command.example.editor.Editor; 4 | 5 | public class PasteCommand extends Command { 6 | 7 | public PasteCommand(Editor editor) { 8 | super(editor); 9 | } 10 | 11 | @Override 12 | public boolean execute() { 13 | if (editor.clipboard == null || editor.clipboard.isEmpty()) return false; 14 | 15 | backup(); 16 | editor.textField.insert(editor.clipboard, editor.textField.getCaretPosition()); 17 | return true; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/ChainOfResponsibility/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | Enter your email: 2 | asd 3 | Enter your password: 4 | 123 5 | UserExistsMiddleware: This email is not registered! 6 | 7 | Enter your email: 8 | admin@example.com 9 | Enter your password: 10 | wrong 11 | UserExistsMiddleware: Wrong password! 12 | 13 | Enter your email: 14 | admin@example.com 15 | Enter your password: 16 | letmein 17 | ThrottlingMiddleware: Request limit exceeded! 18 | 19 | 20 | 21 | Enter your email: 22 | admin@example.com 23 | Enter your password: 24 | admin_pass 25 | RoleCheckMiddleware: Hello, admin! 26 | Server: Authorization has been successful! -------------------------------------------------------------------------------- /courses/guru/js/examples/memento.js: -------------------------------------------------------------------------------- 1 | import { Originator, Caretaker } from '../patterns/memento'; 2 | 3 | const originator = new Originator('Super-duper-super-puper-super.'); 4 | const caretaker = new Caretaker(originator); 5 | 6 | caretaker.backup(); 7 | originator.doSmth(); 8 | 9 | caretaker.backup(); 10 | originator.doSmth(); 11 | 12 | caretaker.backup(); 13 | originator.doSmth(); 14 | 15 | caretaker.backup(); 16 | originator.doSmth(); 17 | 18 | console.log(''); 19 | caretaker.showHistory(); 20 | 21 | console.log('\nClient: do rollback...'); 22 | caretaker.undo(); 23 | 24 | console.log('\nClient: do rollback again...'); 25 | caretaker.undo(); 26 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/facade/example/some_complex_media_library/CodecFactory.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.facade.example.some_complex_media_library; 2 | 3 | public class CodecFactory { 4 | public static Codec extract(VideoFile file) { 5 | String type = file.getCodecType(); 6 | if (type.equals("mp4")) { 7 | System.out.println("CodecFactory: extracting mpeg audio..."); 8 | return new MPEG4CompressionCodec(); 9 | } 10 | else { 11 | System.out.println("CodecFactory: extracting ogg audio..."); 12 | return new OggCompressionCodec(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Observer.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Subject: Attached an observer. 2 | Subject: Attached an observer. 3 | 4 | Subject: I'm doing something important. 5 | Subject: My state has just changed to: 6 6 | Subject: Notifying observers... 7 | ConcreteObserverB: Reacted to the event. 8 | 9 | Subject: I'm doing something important. 10 | Subject: My state has just changed to: 4 11 | Subject: Notifying observers... 12 | ConcreteObserverB: Reacted to the event. 13 | Subject: Detached an observer. 14 | 15 | Subject: I'm doing something important. 16 | Subject: My state has just changed to: 8 17 | Subject: Notifying observers... -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/flyweight/example/trees/TreeFactory.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.flyweight.example.trees; 2 | 3 | import java.awt.*; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | public class TreeFactory { 8 | static Map treeTypes = new HashMap<>(); 9 | 10 | public static TreeType getTreeType(String name, Color color, String otherTreeData) { 11 | TreeType result = treeTypes.get(name); 12 | if (result == null) { 13 | result = new TreeType(name, color, otherTreeData); 14 | treeTypes.put(name, result); 15 | } 16 | return result; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Mediator/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | UserRepository: Loading user records from a file. 2 | EventDispatcher: Broadcasting the 'users:init' event. 3 | Logger: I've written 'users:init' entry to the log. 4 | UserRepository: Creating a user. 5 | EventDispatcher: Broadcasting the 'users:created' event. 6 | OnboardingNotification: The notification has been emailed! 7 | Logger: I've written 'users:created' entry to the log. 8 | User: I can now delete myself without worrying about the repository. 9 | EventDispatcher: Broadcasting the 'users:deleted' event. 10 | UserRepository: Deleting a user. 11 | Logger: I've written 'users:deleted' entry to the log. 12 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/memento/example/shapes/Shape.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.memento.example.shapes; 2 | 3 | import java.awt.*; 4 | import java.io.Serializable; 5 | 6 | public interface Shape extends Serializable { 7 | int getX(); 8 | int getY(); 9 | int getWidth(); 10 | int getHeight(); 11 | void drag(); 12 | void drop(); 13 | void moveTo(int x, int y); 14 | void moveBy(int x, int y); 15 | boolean isInsideBounds(int x, int y); 16 | Color getColor(); 17 | void setColor(Color color); 18 | void select(); 19 | void unSelect(); 20 | boolean isSelected(); 21 | void paint(Graphics graphics); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/composite/components/component.js: -------------------------------------------------------------------------------- 1 | // base class declaring common operations for 2 | // both simple and complex composed objects 3 | 4 | class Component { 5 | // set component parent in tree structure 6 | setParent(parent) { 7 | this.parent = parent; 8 | } 9 | 10 | // get component parent in tree structure 11 | getParent() { 12 | return this.parent; 13 | } 14 | 15 | // check whether component can have children 16 | // its simple component by default 17 | isComposite() { 18 | return false; 19 | } 20 | 21 | // implement some default behavoir or leave it to concrete class 22 | operation() {} 23 | } 24 | 25 | export { Component }; 26 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/adapter/example/square/SquarePeg.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.adapter.example.square; 2 | 3 | /** 4 | * SquarePegs are not compatible with RoundHoles (they were implemented by 5 | * previous development team). But we have to integrate them into our program. 6 | */ 7 | public class SquarePeg { 8 | private double width; 9 | 10 | public SquarePeg(double width) { 11 | this.width = width; 12 | } 13 | 14 | public double getWidth() { 15 | return width; 16 | } 17 | 18 | public double getSquare() { 19 | double result; 20 | result = Math.pow(this.width, 2); 21 | return result; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/singleton/example/thread_safe/Singleton.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.singleton.example.thread_safe; 2 | 3 | public final class Singleton { 4 | private static volatile Singleton instance; 5 | public String value; 6 | 7 | private Singleton(String value) { 8 | this.value = value; 9 | } 10 | 11 | public static Singleton getInstance(String value) { 12 | if (instance == null) { 13 | synchronized (Singleton.class) { 14 | if (instance == null) { 15 | instance = new Singleton(value); 16 | } 17 | } 18 | } 19 | return instance; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: "" 5 | labels: "" 6 | assignees: "" 7 | --- 8 | 9 | **Is your feature request related to a problem? Please describe.** 10 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 11 | 12 | **Describe the solution you'd like** 13 | A clear and concise description of what you want to happen. 14 | 15 | **Describe alternatives you've considered** 16 | A clear and concise description of any alternative solutions or features you've considered. 17 | 18 | **Additional context** 19 | Add any other context or screenshots about the feature request here. 20 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/flyweight/example/trees/TreeType.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.flyweight.example.trees; 2 | 3 | import java.awt.*; 4 | 5 | public class TreeType { 6 | private String name; 7 | private Color color; 8 | private String otherTreeData; 9 | 10 | public TreeType(String name, Color color, String otherTreeData) { 11 | this.name = name; 12 | this.color = color; 13 | this.otherTreeData = otherTreeData; 14 | } 15 | 16 | public void draw(Graphics g, int x, int y) { 17 | g.setColor(Color.BLACK); 18 | g.fillRect(x - 1, y, 3, 5); 19 | g.setColor(color); 20 | g.fillOval(x - 5, y - 10, 10, 10); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/proxy/proxy.js: -------------------------------------------------------------------------------- 1 | // must have same interface as Subject 2 | class Proxy { 3 | #subject = null; 4 | 5 | #checkAccess = function() { 6 | console.log('Proxy: check access before executing real request...'); 7 | return true; 8 | } 9 | 10 | #logAccess = function() { 11 | console.log('Proxy: log request.'); 12 | } 13 | 14 | // reference to object of Subject class 15 | constructor(subject) { 16 | this.#subject = subject; 17 | } 18 | 19 | // Proxy may do smth before and after real request 20 | request() { 21 | if (this.#checkAccess()) { 22 | this.#subject.request(); 23 | this.#logAccess(); 24 | } 25 | } 26 | } 27 | 28 | export { Proxy }; 29 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/memento/example/shapes/Dot.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.memento.example.shapes; 2 | 3 | import java.awt.*; 4 | 5 | public class Dot extends BaseShape { 6 | private final int DOT_SIZE = 3; 7 | 8 | public Dot(int x, int y, Color color) { 9 | super(x, y, color); 10 | } 11 | 12 | @Override 13 | public int getWidth() { 14 | return DOT_SIZE; 15 | } 16 | 17 | @Override 18 | public int getHeight() { 19 | return DOT_SIZE; 20 | } 21 | 22 | @Override 23 | public void paint(Graphics graphics) { 24 | super.paint(graphics); 25 | graphics.fillRect(x - 1, y - 1, getWidth(), getHeight()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/memento/memento.js: -------------------------------------------------------------------------------- 1 | // provide interface for retrieving Memento metadata 2 | // it doesn't expose Originator state 3 | class Memento { 4 | #state = null; 5 | #date = null; 6 | 7 | constructor(state) { 8 | this.#state = state; 9 | this.#date = new Date().toISOString().slice(0, 19).replace('T', ' '); 10 | } 11 | 12 | // Originator uses this method to restore its state 13 | getState() { 14 | return this.#state; 15 | } 16 | 17 | /* --- some methods retrieving metadata --- */ 18 | getName() { 19 | return `${this.#date} / (${this.#state.substr(0, 9)}...)`; 20 | } 21 | 22 | getDate() { 23 | return this.#date; 24 | } 25 | } 26 | 27 | export { Memento }; 28 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/composite/example/shapes/Dot.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.composite.example.shapes; 2 | 3 | import java.awt.*; 4 | 5 | public class Dot extends BaseShape { 6 | private final int DOT_SIZE = 3; 7 | 8 | public Dot(int x, int y, Color color) { 9 | super(x, y, color); 10 | } 11 | 12 | @Override 13 | public int getWidth() { 14 | return DOT_SIZE; 15 | } 16 | 17 | @Override 18 | public int getHeight() { 19 | return DOT_SIZE; 20 | } 21 | 22 | @Override 23 | public void paint(Graphics graphics) { 24 | super.paint(graphics); 25 | graphics.fillRect(x - 1, y - 1, getWidth(), getHeight()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/mediator/example/mediator/Mediator.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.mediator.example.mediator; 2 | 3 | import refactoring_guru.mediator.example.components.Component; 4 | 5 | import javax.swing.*; 6 | 7 | /** 8 | * Common mediator interface. 9 | */ 10 | public interface Mediator { 11 | void addNewNote(Note note); 12 | void deleteNote(); 13 | void getInfoFromList(Note note); 14 | void saveChanges(); 15 | void markNote(); 16 | void clear(); 17 | void sendToFilter(ListModel listModel); 18 | void setElementsList(ListModel list); 19 | void registerComponent(Component component); 20 | void hideElements(boolean flag); 21 | void createGUI(); 22 | } 23 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/state/example/states/State.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.state.example.states; 2 | 3 | import refactoring_guru.state.example.ui.Player; 4 | 5 | /** 6 | * Common interface for all states. 7 | */ 8 | public abstract class State { 9 | Player player; 10 | 11 | /** 12 | * Context passes itself through the state constructor. This may help a 13 | * state to fetch some useful context data if needed. 14 | */ 15 | State(Player player) { 16 | this.player = player; 17 | } 18 | 19 | public abstract String onLock(); 20 | public abstract String onPlay(); 21 | public abstract String onNext(); 22 | public abstract String onPrevious(); 23 | } 24 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/strategy/example/strategies/CreditCard.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.strategy.example.strategies; 2 | 3 | /** 4 | * Dummy credit card class. 5 | */ 6 | public class CreditCard { 7 | private int amount; 8 | private String number; 9 | private String date; 10 | private String cvv; 11 | 12 | CreditCard(String number, String date, String cvv) { 13 | this.amount = 100_000; 14 | this.number = number; 15 | this.date = date; 16 | this.cvv = cvv; 17 | } 18 | 19 | public void setAmount(int amount) { 20 | this.amount = amount; 21 | } 22 | 23 | public int getAmount() { 24 | return amount; 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/singleton/example/non_thread_safe/DemoSingleThread.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.singleton.example.non_thread_safe; 2 | 3 | public class DemoSingleThread { 4 | public static void main(String[] args) { 5 | System.out.println("If you see the same value, then singleton was reused (yay!)" + "\n" + 6 | "If you see different values, then 2 singletons were created (booo!!)" + "\n\n" + 7 | "RESULT:" + "\n"); 8 | Singleton singleton = Singleton.getInstance("FOO"); 9 | Singleton anotherSingleton = Singleton.getInstance("BAR"); 10 | System.out.println(singleton.value); 11 | System.out.println(anotherSingleton.value); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Command/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | WebScrapingCommand: Downloaded https://www.imdb.com/feature/genre/ 2 | IMDBGenresScrapingCommand: Discovered 14 genres. 3 | WebScrapingCommand: Downloaded https://www.imdb.com/search/title?genres=comedy 4 | IMDBGenrePageScrapingCommand: Discovered 50 movies. 5 | WebScrapingCommand: Downloaded https://www.imdb.com/search/title?genres=sci-fi 6 | IMDBGenrePageScrapingCommand: Discovered 50 movies. 7 | WebScrapingCommand: Downloaded https://www.imdb.com/search/title?genres=horror 8 | IMDBGenrePageScrapingCommand: Discovered 50 movies. 9 | WebScrapingCommand: Downloaded https://www.imdb.com/search/title?genres=romance 10 | IMDBGenrePageScrapingCommand: Discovered 50 movies. 11 | ... -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Observer/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Subject: Attached an observer. 2 | Subject: Attached an observer. 3 | 4 | Subject: I'm doing something important. 5 | Subject: My state has just changed to: 2 6 | Subject: Notifying observers... 7 | ConcreteObserverA: Reacted to the event. 8 | ConcreteObserverB: Reacted to the event. 9 | 10 | Subject: I'm doing something important. 11 | Subject: My state has just changed to: 4 12 | Subject: Notifying observers... 13 | ConcreteObserverB: Reacted to the event. 14 | 15 | Subject: Detached an observer. 16 | 17 | Subject: I'm doing something important. 18 | Subject: My state has just changed to: 1 19 | Subject: Notifying observers... 20 | ConcreteObserverA: Reacted to the event. -------------------------------------------------------------------------------- /courses/guru/js/examples/template-method.js: -------------------------------------------------------------------------------- 1 | import { Operation1, Operation2 } from '../patterns/template-method'; 2 | 3 | /* --- client code --- */ 4 | 5 | function clientCode(operation) { 6 | // doesn't care about concrete algorithm implementation 7 | operation.templateMethod(); 8 | } 9 | 10 | /* --- work with one algoritm implementation --- */ 11 | 12 | console.log('Same client code can work with different subclasses:'); 13 | const operation1 = new Operation1(); 14 | clientCode(operation1); 15 | 16 | console.log(''); 17 | 18 | /* --- work with another algoritm implementation --- */ 19 | 20 | console.log('Same client code can work with different subclasses:'); 21 | const operation2 = new Operation2(); 22 | clientCode(operation2); 23 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/composite/example/shapes/Circle.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.composite.example.shapes; 2 | 3 | import java.awt.*; 4 | 5 | public class Circle extends BaseShape { 6 | public int radius; 7 | 8 | public Circle(int x, int y, int radius, Color color) { 9 | super(x, y, color); 10 | this.radius = radius; 11 | } 12 | 13 | @Override 14 | public int getWidth() { 15 | return radius * 2; 16 | } 17 | 18 | @Override 19 | public int getHeight() { 20 | return radius * 2; 21 | } 22 | 23 | public void paint(Graphics graphics) { 24 | super.paint(graphics); 25 | graphics.drawOval(x, y, getWidth() - 1, getHeight() - 1); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /courses/guru/js/README.md: -------------------------------------------------------------------------------- 1 | # Design patterns 2 | 3 | Design patterns written in vanilla JavaScript (ES6+) for node.js 4 | 5 | Based on book ["Dive into design patterns"](https://refactoring.guru/design-patterns/book) 6 | 7 | ## Installation 8 | 9 | 1. move somewhere in your filesystem 10 | 11 | 2. clone this repository 12 | 13 | 3. run this shell command 14 | 15 | ```shell 16 | npm install 17 | ``` 18 | 19 | ## Usage 20 | 21 | To use some pattern, run corresponding command ```npm run ```, where ```pattern-name``` is pattern filename in ```examples``` directory. 22 | 23 | **Examples:** 24 | 25 | Singleton: 26 | 27 | ```shell 28 | npm run singleton 29 | ``` 30 | 31 | Builder: 32 | 33 | ```shell 34 | npm run builder 35 | ``` 36 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/mediator/example/mediator/Note.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.mediator.example.mediator; 2 | 3 | /** 4 | * Note class. 5 | */ 6 | public class Note { 7 | private String name; 8 | private String text; 9 | 10 | public Note() { 11 | name = "New note"; 12 | } 13 | 14 | public void setName(String name) { 15 | this.name = name; 16 | } 17 | 18 | public void setText(String text) { 19 | this.text = text; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public String getText() { 27 | return text; 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return name; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/state/context.js: -------------------------------------------------------------------------------- 1 | // define interface of interest of Client 2 | // maintain reference to instance of State subclasses 3 | class Context { 4 | // reference to Context current state 5 | #state = null; 6 | 7 | constructor(state) { 8 | this.transitionTo(state); 9 | } 10 | 11 | // change State at runtime 12 | transitionTo(state) { 13 | console.log(`Context: transition to: ${state.constructor.name}`); 14 | this.#state = state; 15 | this.#state.context = this; 16 | } 17 | 18 | /* --- Context delegates part of its behavior to current State --- */ 19 | request1() { 20 | this.#state.handle1(); 21 | } 22 | 23 | request2() { 24 | this.#state.handle2(); 25 | } 26 | } 27 | 28 | export { Context }; 29 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/memento/example/shapes/Circle.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.memento.example.shapes; 2 | 3 | import java.awt.*; 4 | 5 | public class Circle extends BaseShape { 6 | private int radius; 7 | 8 | public Circle(int x, int y, int radius, Color color) { 9 | super(x, y, color); 10 | this.radius = radius; 11 | } 12 | 13 | @Override 14 | public int getWidth() { 15 | return radius * 2; 16 | } 17 | 18 | @Override 19 | public int getHeight() { 20 | return radius * 2; 21 | } 22 | 23 | @Override 24 | public void paint(Graphics graphics) { 25 | super.paint(graphics); 26 | graphics.drawOval(x, y, getWidth() - 1, getHeight() - 1); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/builder/example/components/TripComputer.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.builder.example.components; 2 | 3 | import refactoring_guru.builder.example.cars.Car; 4 | 5 | /** 6 | * Just another feature of a car. 7 | */ 8 | public class TripComputer { 9 | 10 | private Car car; 11 | 12 | public void setCar(Car car) { 13 | this.car = car; 14 | } 15 | 16 | public void showFuelLevel() { 17 | System.out.println("Fuel level: " + car.getFuel()); 18 | } 19 | 20 | public void showStatus() { 21 | if (this.car.getEngine().isStarted()) { 22 | System.out.println("Car is started"); 23 | } else { 24 | System.out.println("Car isn't started"); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/singleton/example/non_thread_safe/Singleton.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.singleton.example.non_thread_safe; 2 | 3 | public final class Singleton { 4 | private static Singleton instance; 5 | public String value; 6 | 7 | private Singleton(String value) { 8 | // Following code emulates slow initialization. 9 | try { 10 | Thread.sleep(1000); 11 | } catch (InterruptedException ex) { 12 | ex.printStackTrace(); 13 | } 14 | this.value = value; 15 | } 16 | 17 | public static Singleton getInstance(String value) { 18 | if (instance == null) { 19 | instance = new Singleton(value); 20 | } 21 | return instance; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/prototype/example/shapes/Circle.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.prototype.example.shapes; 2 | 3 | public class Circle extends Shape { 4 | public int radius; 5 | 6 | public Circle() { 7 | } 8 | 9 | public Circle(Circle target) { 10 | super(target); 11 | if (target != null) { 12 | this.radius = target.radius; 13 | } 14 | } 15 | 16 | @Override 17 | public Shape clone() { 18 | return new Circle(this); 19 | } 20 | 21 | @Override 22 | public boolean equals(Object object2) { 23 | if (!(object2 instanceof Circle) || !super.equals(object2)) return false; 24 | Circle shape2 = (Circle) object2; 25 | return shape2.radius == radius; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/TemplateMethod/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Same client code can work with different subclasses: 2 | AbstractClass says: I am doing bulk of the work 3 | ConcreteClass1 says: Implemented Operation1 4 | AbstractClass says: But I let subclasses to override some operations 5 | ConcreteClass1 says: Implemented Operation2 6 | AbstractClass says: But I am doing bulk of the work anyway 7 | 8 | Same client code can work with different subclasses: 9 | AbstractClass says: I am doing bulk of the work 10 | ConcreteClass2 says: Implemented Operation1 11 | AbstractClass says: But I let subclasses to override some operations 12 | ConcreteClass2 says: Overridden Hook1 13 | ConcreteClass2 says: Implemented Operation2 14 | AbstractClass says: But I am doing bulk of the work anyway -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/adapter/example/adapters/SquarePegAdapter.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.adapter.example.adapters; 2 | 3 | import refactoring_guru.adapter.example.round.RoundPeg; 4 | import refactoring_guru.adapter.example.square.SquarePeg; 5 | 6 | /** 7 | * Adapter allows fitting square pegs into round holes. 8 | */ 9 | public class SquarePegAdapter extends RoundPeg { 10 | private SquarePeg peg; 11 | 12 | public SquarePegAdapter(SquarePeg peg) { 13 | this.peg = peg; 14 | } 15 | 16 | @Override 17 | public double getRadius() { 18 | double result; 19 | // Calculate a minimum circle radius, which can fit this peg. 20 | result = (Math.sqrt(Math.pow((peg.getWidth() / 2), 2) * 2)); 21 | return result; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/factory_method/example/factory/Dialog.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.factory_method.example.factory; 2 | 3 | import refactoring_guru.factory_method.example.buttons.Button; 4 | 5 | /** 6 | * Base factory class. Note that "factory" is merely a role for the class. It 7 | * should have some core business logic which needs different products to be 8 | * created. 9 | */ 10 | public abstract class Dialog { 11 | 12 | public void renderWindow() { 13 | // ... other code ... 14 | 15 | Button okButton = createButton(); 16 | okButton.render(); 17 | } 18 | 19 | /** 20 | * Subclasses will override this method in order to create specific button 21 | * objects. 22 | */ 23 | public abstract Button createButton(); 24 | } 25 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/strategy/example/OutputDemo.txt: -------------------------------------------------------------------------------- 1 | Please, select a product: 2 | 1 - Mother board 3 | 2 - CPU 4 | 3 - HDD 5 | 4 - Memory 6 | 1 7 | Count: 2 8 | Do you wish to continue selecting products? Y/N: y 9 | Please, select a product: 10 | 1 - Mother board 11 | 2 - CPU 12 | 3 - HDD 13 | 4 - Memory 14 | 2 15 | Count: 1 16 | Do you wish to continue selecting products? Y/N: n 17 | Please, select a payment method: 18 | 1 - PalPay 19 | 2 - Credit Card 20 | 1 21 | Enter the user's email: user@example.com 22 | Enter the password: qwerty 23 | Wrong email or password! 24 | Enter user email: amanda@ya.com 25 | Enter password: amanda1985 26 | Data verification has been successful. 27 | Pay 6250 units or Continue shopping? P/C: p 28 | Paying 6250 using PayPal. 29 | Payment has been successful. 30 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/command/commands/complex.js: -------------------------------------------------------------------------------- 1 | // reference to some Receiver 2 | let privateReceiver = null; 3 | 4 | /* --- context data --- */ 5 | let privateA = null; 6 | let privateB = null; 7 | 8 | // delegate "complex" operations to other objects: Recievers 9 | class ComplexCommand { 10 | // can accept one or several Receiver objects + some context data 11 | constructor(receiver, a, b) { 12 | privateReceiver = receiver; 13 | privateA = a; 14 | privateB = b; 15 | } 16 | 17 | // command can be delegated to any Receiver method 18 | execute() { 19 | console.log('ComplexCommand: complex command will be done by receiver object.'); 20 | privateReceiver.doSmth(privateA); 21 | privateReceiver.doSmthElse(privateB); 22 | } 23 | } 24 | 25 | export { ComplexCommand }; 26 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Flyweight/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | 2 | FlyweightFactory: I have 5 flyweights: 3 | Chevrolet_Camaro2018_pink 4 | Mercedes Benz_C300_black 5 | Mercedes Benz_C500_red 6 | BMW_M5_red 7 | BMW_X6_white 8 | 9 | Client: Adding a car to database. 10 | FlyweightFactory: Reusing existing flyweight. 11 | Flyweight: Displaying shared (["BMW","M5","red"]) and unique (["CL234IR","James Doe"]) state. 12 | 13 | Client: Adding a car to database. 14 | FlyweightFactory: Can't find a flyweight, creating new one. 15 | Flyweight: Displaying shared (["BMW","X1","red"]) and unique (["CL234IR","James Doe"]) state. 16 | 17 | FlyweightFactory: I have 6 flyweights: 18 | Chevrolet_Camaro2018_pink 19 | Mercedes Benz_C300_black 20 | Mercedes Benz_C500_red 21 | BMW_M5_red 22 | BMW_X6_white 23 | BMW_X1_red 24 | -------------------------------------------------------------------------------- /courses/guru/js/examples/adapter.js: -------------------------------------------------------------------------------- 1 | import { Target, Adaptee, Adapter } from '../patterns/adapter'; 2 | 3 | // this function supports all classes that follows Target interface 4 | function clientCode(target) { 5 | console.log(target.request()); 6 | } 7 | 8 | // call Target method 9 | console.log('Client: can work with Target objects:'); 10 | const target = new Target(); 11 | clientCode(target); 12 | 13 | // call incompatable with Target Adaptee's method 14 | const adaptee = new Adaptee(); 15 | console.log('Client: Adaptee has incompatable interface!'); 16 | console.log('ADAPTEE: %s', adaptee.incompatableRequest()); 17 | 18 | // now we can call Adaptee incompatable method via Adapter 19 | console.log('Client: can work with it via Adapter:'); 20 | const adapter = new Adapter(adaptee); 21 | clientCode(adapter); 22 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.TemplateMethod.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Same client code can work with different subclasses: 2 | AbstractClass says: I am doing the bulk of the work 3 | ConcreteClass1 says: Implemented Operation1 4 | AbstractClass says: But I let subclasses override some operations 5 | ConcreteClass1 says: Implemented Operation2 6 | AbstractClass says: But I am doing the bulk of the work anyway 7 | 8 | Same client code can work with different subclasses: 9 | AbstractClass says: I am doing the bulk of the work 10 | ConcreteClass2 says: Implemented Operation1 11 | AbstractClass says: But I let subclasses override some operations 12 | ConcreteClass2 says: Overridden Hook1 13 | ConcreteClass2 says: Implemented Operation2 14 | AbstractClass says: But I am doing the bulk of the work anyway -------------------------------------------------------------------------------- /courses/guru/js/examples/iterator.js: -------------------------------------------------------------------------------- 1 | import { Collection1 } from '../patterns/iterator'; 2 | 3 | // client code may or may not to know about concrete Iterator or Collection classes 4 | 5 | /* --- make collection and add some items --- */ 6 | const collection1 = new Collection1(); 7 | collection1.addItem(1); 8 | collection1.addItem(2); 9 | collection1.addItem(3); 10 | 11 | // make straight iterator 12 | const iterator1 = collection1.getIterator(); 13 | 14 | console.log('Straight traversal:'); 15 | while (iterator1.valid()) { 16 | console.log(iterator1.next()); 17 | } 18 | 19 | console.log(''); 20 | console.log('Reverse traversal:'); 21 | 22 | // make reverse iterator 23 | const reverseIterator1 = collection1.getReverseIterator(); 24 | while (reverseIterator1.valid()) { 25 | console.log(reverseIterator1.next()); 26 | } 27 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/observer/example/editor/Editor.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.observer.example.editor; 2 | 3 | import refactoring_guru.observer.example.publisher.EventManager; 4 | 5 | import java.io.File; 6 | 7 | public class Editor { 8 | public EventManager events; 9 | private File file; 10 | 11 | public Editor() { 12 | this.events = new EventManager("open", "save"); 13 | } 14 | 15 | public void openFile(String filePath) { 16 | this.file = new File(filePath); 17 | events.notify("open", file); 18 | } 19 | 20 | public void saveFile() throws Exception { 21 | if (this.file != null) { 22 | events.notify("save", file); 23 | } else { 24 | throw new Exception("Please open a file first."); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/composite/example/shapes/Rectangle.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.composite.example.shapes; 2 | 3 | import java.awt.*; 4 | 5 | public class Rectangle extends BaseShape { 6 | public int width; 7 | public int height; 8 | 9 | public Rectangle(int x, int y, int width, int height, Color color) { 10 | super(x, y, color); 11 | this.width = width; 12 | this.height = height; 13 | } 14 | 15 | @Override 16 | public int getWidth() { 17 | return width; 18 | } 19 | 20 | @Override 21 | public int getHeight() { 22 | return height; 23 | } 24 | 25 | @Override 26 | public void paint(Graphics graphics) { 27 | super.paint(graphics); 28 | graphics.drawRect(x, y, getWidth() - 1, getHeight() - 1); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/memento/example/shapes/Rectangle.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.memento.example.shapes; 2 | 3 | import java.awt.*; 4 | 5 | public class Rectangle extends BaseShape { 6 | private int width; 7 | private int height; 8 | 9 | public Rectangle(int x, int y, int width, int height, Color color) { 10 | super(x, y, color); 11 | this.width = width; 12 | this.height = height; 13 | } 14 | 15 | @Override 16 | public int getWidth() { 17 | return width; 18 | } 19 | 20 | @Override 21 | public int getHeight() { 22 | return height; 23 | } 24 | 25 | @Override 26 | public void paint(Graphics graphics) { 27 | super.paint(graphics); 28 | graphics.drawRect(x, y, getWidth() - 1, getHeight() - 1); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/singleton/singleton.js: -------------------------------------------------------------------------------- 1 | // private data 2 | const privateObject = { 3 | username: 'sparx', 4 | password: 'superpassword', 5 | }; 6 | 7 | // instance save 8 | let instance = null; 9 | 10 | class Singleton { 11 | constructor() { 12 | // if we have instance just return it 13 | if (instance) return instance; 14 | // else: save Singleton object to instance 15 | instance = this; 16 | // add private data to instance object 17 | return Object.assign(instance, privateObject); 18 | } 19 | 20 | // static method for instance access 21 | static getInstance() { 22 | // if we have instance just return it 23 | if (instance) return instance; 24 | // else: create instance of Singleton and return it 25 | return new Singleton(); 26 | } 27 | } 28 | 29 | export { Singleton }; 30 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/observer/example/Demo.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.observer.example; 2 | 3 | import refactoring_guru.observer.example.editor.Editor; 4 | import refactoring_guru.observer.example.listeners.EmailNotificationListener; 5 | import refactoring_guru.observer.example.listeners.LogOpenListener; 6 | 7 | public class Demo { 8 | public static void main(String[] args) { 9 | Editor editor = new Editor(); 10 | editor.events.subscribe("open", new LogOpenListener("/path/to/log/file.txt")); 11 | editor.events.subscribe("save", new EmailNotificationListener("admin@example.com")); 12 | 13 | try { 14 | editor.openFile("test.txt"); 15 | editor.saveFile(); 16 | } catch (Exception e) { 17 | e.printStackTrace(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/strategy/context.js: -------------------------------------------------------------------------------- 1 | // defines interface for Client interest 2 | // doesn't know concrete Strategy 3 | // should work with all Strategies using Strategy interface 4 | class Context { 5 | // reference to one of Strategy 6 | #strategy = null; 7 | 8 | // set Strategy through constructor 9 | constructor(strategy) { 10 | this.#strategy = strategy; 11 | } 12 | 13 | // set Strategy at runtime 14 | setStrategy(strategy) { 15 | this.#strategy = strategy; 16 | } 17 | 18 | // delegate work to Strategy instead of implementing multiple versions of algorithm 19 | businessLogic() { 20 | console.log('Context: sort data using some strategy...'); 21 | const result = this.#strategy.doAlgorithm(['a', 'b', 'c', 'd', 'e']); 22 | console.log(result.join(',')); 23 | } 24 | } 25 | 26 | export { Context }; 27 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/memento/example/commands/ColorCommand.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.memento.example.commands; 2 | 3 | import refactoring_guru.memento.example.editor.Editor; 4 | import refactoring_guru.memento.example.shapes.Shape; 5 | 6 | import java.awt.*; 7 | 8 | public class ColorCommand implements Command { 9 | private Editor editor; 10 | private Color color; 11 | 12 | public ColorCommand(Editor editor, Color color) { 13 | this.editor = editor; 14 | this.color = color; 15 | } 16 | 17 | @Override 18 | public String getName() { 19 | return "Colorize: " + color.toString(); 20 | } 21 | 22 | @Override 23 | public void execute() { 24 | for (Shape child : editor.getShapes().getSelected()) { 25 | child.setColor(color); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/prototype/example/shapes/Shape.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.prototype.example.shapes; 2 | 3 | import java.util.Objects; 4 | 5 | public abstract class Shape { 6 | public int x; 7 | public int y; 8 | public String color; 9 | 10 | public Shape() { 11 | } 12 | 13 | public Shape(Shape target) { 14 | if (target != null) { 15 | this.x = target.x; 16 | this.y = target.y; 17 | this.color = target.color; 18 | } 19 | } 20 | 21 | public abstract Shape clone(); 22 | 23 | @Override 24 | public boolean equals(Object object2) { 25 | if (!(object2 instanceof Shape)) return false; 26 | Shape shape2 = (Shape) object2; 27 | return shape2.x == x && shape2.y == y && Objects.equals(shape2.color, color); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/builder/example/builders/Builder.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.builder.example.builders; 2 | 3 | import refactoring_guru.builder.example.cars.Type; 4 | import refactoring_guru.builder.example.components.Engine; 5 | import refactoring_guru.builder.example.components.GPSNavigator; 6 | import refactoring_guru.builder.example.components.Transmission; 7 | import refactoring_guru.builder.example.components.TripComputer; 8 | 9 | /** 10 | * Builder interface defines all possible ways to configure a product. 11 | */ 12 | public interface Builder { 13 | void setType(Type type); 14 | void setSeats(int seats); 15 | void setEngine(Engine engine); 16 | void setTransmission(Transmission transmission); 17 | void setTripComputer(TripComputer tripComputer); 18 | void setGPSNavigator(GPSNavigator gpsNavigator); 19 | } 20 | -------------------------------------------------------------------------------- /courses/guru/js/examples/bridge.js: -------------------------------------------------------------------------------- 1 | import { 2 | Abstraction, 3 | AbstractionExtended, 4 | ImplementationA, 5 | ImplementationB, 6 | } from '../patterns/bridge'; 7 | 8 | // client code should depend only on Abstraction class 9 | // this way client code can work with any abstraction-implementation combination 10 | function clientCode(abstraction) { 11 | console.log(abstraction.operation()); 12 | } 13 | 14 | let implementation = null; 15 | let abstraction = null; 16 | 17 | /* --- combination: Abstraction-ImplementationA ---*/ 18 | implementation = new ImplementationA(); 19 | abstraction = new Abstraction(implementation); 20 | clientCode(abstraction); 21 | 22 | /* --- combination: AbstractionExtended-ImplementationB ---*/ 23 | implementation = new ImplementationB(); 24 | abstraction = new AbstractionExtended(implementation); 25 | clientCode(abstraction); 26 | -------------------------------------------------------------------------------- /courses/guru/js/examples/observer.js: -------------------------------------------------------------------------------- 1 | import { Subject1, Observer1, Observer2 } from '../patterns/observer'; 2 | 3 | // create subject 4 | const subject1 = new Subject1(); 5 | 6 | // create observer 7 | const observer1 = new Observer1(); 8 | // attach observer to subject 9 | subject1.attach(observer1); 10 | 11 | // create another observer 12 | const observer2 = new Observer2(); 13 | // attach this observer to same subject 14 | subject1.attach(observer2); 15 | 16 | // execute some business logic methods 17 | // both observers will be notified about some event 18 | subject1.someBusinessLogic(); 19 | subject1.someBusinessLogic(); 20 | 21 | // detach second observer from subject 22 | subject1.detach(observer2); 23 | 24 | // execute some business logic methods again 25 | // now only first observer will be notified about some event 26 | subject1.someBusinessLogic(); 27 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/abstract_factory/example/factories/MacOSFactory.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.abstract_factory.example.factories; 2 | 3 | import refactoring_guru.abstract_factory.example.buttons.Button; 4 | import refactoring_guru.abstract_factory.example.buttons.MacOSButton; 5 | import refactoring_guru.abstract_factory.example.checkboxes.Checkbox; 6 | import refactoring_guru.abstract_factory.example.checkboxes.MacOSCheckbox; 7 | 8 | /** 9 | * Each concrete factory extends basic factory and responsible for creating 10 | * products of a single variety. 11 | */ 12 | public class MacOSFactory implements GUIFactory { 13 | 14 | @Override 15 | public Button createButton() { 16 | return new MacOSButton(); 17 | } 18 | 19 | @Override 20 | public Checkbox createCheckbox() { 21 | return new MacOSCheckbox(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /courses/guru/js/examples/decorator.js: -------------------------------------------------------------------------------- 1 | import { 2 | ComponentA, 3 | DecoratorA, 4 | DecoratorB, 5 | } from '../patterns/decorator'; 6 | 7 | // works with all objects using Component interface 8 | // this way it can stay independent of concrete classes of components it works with 9 | function clientCode(component) { 10 | console.log(`RESULT: ${component.operation()}`); 11 | } 12 | 13 | /* --- working with simple components --- */ 14 | 15 | const simpleComponent = new ComponentA(); 16 | console.log('Client: Simple component:'); 17 | clientCode(simpleComponent); 18 | 19 | /* --- working with decorated components --- */ 20 | 21 | // wrap simple component 22 | const decoratorA = new DecoratorA(simpleComponent); 23 | // wrap decorated component 24 | const decoratorB = new DecoratorB(decoratorA); 25 | console.log('Client: Decorated component:'); 26 | clientCode(decoratorB); 27 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/abstract_factory/example/factories/WindowsFactory.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.abstract_factory.example.factories; 2 | 3 | import refactoring_guru.abstract_factory.example.buttons.Button; 4 | import refactoring_guru.abstract_factory.example.buttons.WindowsButton; 5 | import refactoring_guru.abstract_factory.example.checkboxes.Checkbox; 6 | import refactoring_guru.abstract_factory.example.checkboxes.WindowsCheckbox; 7 | 8 | /** 9 | * Each concrete factory extends basic factory and responsible for creating 10 | * products of a single variety. 11 | */ 12 | public class WindowsFactory implements GUIFactory { 13 | 14 | @Override 15 | public Button createButton() { 16 | return new WindowsButton(); 17 | } 18 | 19 | @Override 20 | public Checkbox createCheckbox() { 21 | return new WindowsCheckbox(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/abstract_factory/example/app/Application.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.abstract_factory.example.app; 2 | 3 | import refactoring_guru.abstract_factory.example.buttons.Button; 4 | import refactoring_guru.abstract_factory.example.checkboxes.Checkbox; 5 | import refactoring_guru.abstract_factory.example.factories.GUIFactory; 6 | 7 | /** 8 | * Factory users don't care which concrete factory they use since they work with 9 | * factories and products through abstract interfaces. 10 | */ 11 | public class Application { 12 | private Button button; 13 | private Checkbox checkbox; 14 | 15 | public Application(GUIFactory factory) { 16 | button = factory.createButton(); 17 | checkbox = factory.createCheckbox(); 18 | } 19 | 20 | public void paint() { 21 | button.paint(); 22 | checkbox.paint(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/mediator/example/components/Title.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.mediator.example.components; 2 | 3 | import refactoring_guru.mediator.example.mediator.Mediator; 4 | 5 | import javax.swing.*; 6 | import java.awt.event.KeyEvent; 7 | 8 | /** 9 | * Concrete components don't talk with each other. They have only one 10 | * communication channel–sending requests to the mediator. 11 | */ 12 | public class Title extends JTextField implements Component { 13 | private Mediator mediator; 14 | 15 | @Override 16 | public void setMediator(Mediator mediator) { 17 | this.mediator = mediator; 18 | } 19 | 20 | @Override 21 | protected void processComponentKeyEvent(KeyEvent keyEvent) { 22 | mediator.markNote(); 23 | } 24 | 25 | @Override 26 | public String getName() { 27 | return "Title"; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/mediator/example/components/TextBox.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.mediator.example.components; 2 | 3 | import refactoring_guru.mediator.example.mediator.Mediator; 4 | 5 | import javax.swing.*; 6 | import java.awt.event.KeyEvent; 7 | 8 | /** 9 | * Concrete components don't talk with each other. They have only one 10 | * communication channel–sending requests to the mediator. 11 | */ 12 | public class TextBox extends JTextArea implements Component { 13 | private Mediator mediator; 14 | 15 | @Override 16 | public void setMediator(Mediator mediator) { 17 | this.mediator = mediator; 18 | } 19 | 20 | @Override 21 | protected void processComponentKeyEvent(KeyEvent keyEvent) { 22 | mediator.markNote(); 23 | } 24 | 25 | @Override 26 | public String getName() { 27 | return "TextBox"; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/prototype/example/shapes/Rectangle.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.prototype.example.shapes; 2 | 3 | public class Rectangle extends Shape { 4 | public int width; 5 | public int height; 6 | 7 | public Rectangle() { 8 | } 9 | 10 | public Rectangle(Rectangle target) { 11 | super(target); 12 | if (target != null) { 13 | this.width = target.width; 14 | this.height = target.height; 15 | } 16 | } 17 | 18 | @Override 19 | public Shape clone() { 20 | return new Rectangle(this); 21 | } 22 | 23 | @Override 24 | public boolean equals(Object object2) { 25 | if (!(object2 instanceof Rectangle) || !super.equals(object2)) return false; 26 | Rectangle shape2 = (Rectangle) object2; 27 | return shape2.width == width && shape2.height == height; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/state/example/states/PlayingState.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.state.example.states; 2 | 3 | import refactoring_guru.state.example.ui.Player; 4 | 5 | public class PlayingState extends State { 6 | 7 | PlayingState(Player player) { 8 | super(player); 9 | } 10 | 11 | @Override 12 | public String onLock() { 13 | player.changeState(new LockedState(player)); 14 | player.setCurrentTrackAfterStop(); 15 | return "Stop playing"; 16 | } 17 | 18 | @Override 19 | public String onPlay() { 20 | player.changeState(new ReadyState(player)); 21 | return "Paused..."; 22 | } 23 | 24 | @Override 25 | public String onNext() { 26 | return player.nextTrack(); 27 | } 28 | 29 | @Override 30 | public String onPrevious() { 31 | return player.previousTrack(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Bridge/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | HTML view of a simple content page: 2 | 3 |

Home

4 |
Welcome to our website!
5 | 6 | 7 | JSON view of a simple content page, rendered with the same client code: 8 | { 9 | "title": "Home", 10 | "text": "Welcome to our website!" 11 | } 12 | 13 | HTML view of a product page, same client code: 14 | 15 |

Star Wars, episode1

16 |
A long time ago in a galaxy far, far away...
17 | 18 | Add to cart 19 | 20 | 21 | JSON view of a simple content page, with the same client code: 22 | { 23 | "title": "Star Wars, episode1", 24 | "text": "A long time ago in a galaxy far, far away...", 25 | "img": "/images/star-wars.jpeg", 26 | "link": {"href": "Add to cart", "title": "Add to cart""} 27 | } -------------------------------------------------------------------------------- /courses/guru/js/examples/visitor.js: -------------------------------------------------------------------------------- 1 | import { 2 | Component1, 3 | Component2, 4 | Visitor1, 5 | Visitor2, 6 | } from '../patterns/visitor'; 7 | 8 | // client code can run operations on any set of elements without figuring out their concrete classes 9 | function clientCode(components, visitor) { 10 | components.forEach((component) => { 11 | component.accept(visitor); 12 | }); 13 | } 14 | 15 | // create components 16 | const components = [ 17 | new Component1(), 18 | new Component2(), 19 | ]; 20 | 21 | /* --- use one Visitor --- */ 22 | 23 | console.log('Client code works with all visitors using Visitor interface:'); 24 | const visitor1 = new Visitor1(); 25 | clientCode(components, visitor1); 26 | 27 | console.log(''); 28 | 29 | /* --- use another Visitor --- */ 30 | 31 | console.log('Same client can work with another Visitor:'); 32 | const visitor2 = new Visitor2(); 33 | clientCode(components, visitor2); 34 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/visitor/example/OutputDemo.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 2 4 | 23 5 | 15 6 | 10 7 | 8 | 9 | 10 | 11 | 4 12 | 13 | 1 14 | 10 15 | 55 16 | 17 | 18 | 2 19 | 23 20 | 15 21 | 10 22 | 23 | 24 | 3 25 | 10 26 | 17 27 | 20 28 | 30 29 | 30 | 31 | 5 32 | 33 | 1 34 | 10 35 | 55 36 | 37 | 38 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/FactoryMethod/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | Testing ConcreteCreator1: 2 | Send HTTP API request to log in user john_smith with password ****** 3 | Send HTTP API requests to create a post in Facebook timeline. 4 | Send HTTP API request to log out user john_smith 5 | Send HTTP API request to log in user john_smith with password ****** 6 | Send HTTP API requests to create a post in Facebook timeline. 7 | Send HTTP API request to log out user john_smith 8 | 9 | 10 | Testing ConcreteCreator2: 11 | Send HTTP API request to log in user john_smith@example.com with password ****** 12 | Send HTTP API requests to create a post in LinkedIn timeline. 13 | Send HTTP API request to log out user john_smith@example.com 14 | Send HTTP API request to log in user john_smith@example.com with password ****** 15 | Send HTTP API requests to create a post in LinkedIn timeline. 16 | Send HTTP API request to log out user john_smith@example.com 17 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/state/example/states/ReadyState.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.state.example.states; 2 | 3 | import refactoring_guru.state.example.ui.Player; 4 | 5 | /** 6 | * They can also trigger state transitions in the context. 7 | */ 8 | public class ReadyState extends State { 9 | 10 | public ReadyState(Player player) { 11 | super(player); 12 | } 13 | 14 | @Override 15 | public String onLock() { 16 | player.changeState(new LockedState(player)); 17 | return "Locked..."; 18 | } 19 | 20 | @Override 21 | public String onPlay() { 22 | String action = player.startPlayback(); 23 | player.changeState(new PlayingState(player)); 24 | return action; 25 | } 26 | 27 | @Override 28 | public String onNext() { 29 | return "Locked..."; 30 | } 31 | 32 | @Override 33 | public String onPrevious() { 34 | return "Locked..."; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/flyweight/example/forest/Forest.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.flyweight.example.forest; 2 | 3 | import refactoring_guru.flyweight.example.trees.Tree; 4 | import refactoring_guru.flyweight.example.trees.TreeFactory; 5 | import refactoring_guru.flyweight.example.trees.TreeType; 6 | 7 | import javax.swing.*; 8 | import java.awt.*; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | public class Forest extends JFrame { 13 | private List trees = new ArrayList<>(); 14 | 15 | public void plantTree(int x, int y, String name, Color color, String otherTreeData) { 16 | TreeType type = TreeFactory.getTreeType(name, color, otherTreeData); 17 | Tree tree = new Tree(x, y, type); 18 | trees.add(tree); 19 | } 20 | 21 | @Override 22 | public void paint(Graphics graphics) { 23 | for (Tree tree : trees) { 24 | tree.draw(graphics); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/template_method/example/networks/Network.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.template_method.example.networks; 2 | 3 | /** 4 | * Base class of social network. 5 | */ 6 | public abstract class Network { 7 | String userName; 8 | String password; 9 | 10 | Network() {} 11 | 12 | /** 13 | * Publish the data to whatever network. 14 | */ 15 | public boolean post(String message) { 16 | // Authenticate before posting. Every network uses a different 17 | // authentication method. 18 | if (logIn(this.userName, this.password)) { 19 | // Send the post data. 20 | boolean result = sendData(message.getBytes()); 21 | logOut(); 22 | return result; 23 | } 24 | return false; 25 | } 26 | 27 | abstract boolean logIn(String userName, String password); 28 | abstract boolean sendData(byte[] data); 29 | abstract void logOut(); 30 | } -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/visitor/example/shapes/CompoundShape.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.visitor.example.shapes; 2 | 3 | import refactoring_guru.visitor.example.visitor.Visitor; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | public class CompoundShape implements Shape { 9 | public int id; 10 | public List children = new ArrayList<>(); 11 | 12 | public CompoundShape(int id) { 13 | this.id = id; 14 | } 15 | 16 | @Override 17 | public void move(int x, int y) { 18 | // move shape 19 | } 20 | 21 | @Override 22 | public void draw() { 23 | // draw shape 24 | } 25 | 26 | public int getId() { 27 | return id; 28 | } 29 | 30 | @Override 31 | public String accept(Visitor visitor) { 32 | return visitor.visitCompoundGraphic(this); 33 | } 34 | 35 | public void add(Shape shape) { 36 | children.add(shape); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/command/example/commands/CutCommand.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.command.example.commands; 2 | 3 | import refactoring_guru.command.example.editor.Editor; 4 | 5 | public class CutCommand extends Command { 6 | 7 | public CutCommand(Editor editor) { 8 | super(editor); 9 | } 10 | 11 | @Override 12 | public boolean execute() { 13 | if (editor.textField.getSelectedText().isEmpty()) return false; 14 | 15 | backup(); 16 | String source = editor.textField.getText(); 17 | editor.clipboard = editor.textField.getSelectedText(); 18 | editor.textField.setText(cutString(source)); 19 | return true; 20 | } 21 | 22 | private String cutString(String source) { 23 | String start = source.substring(0, editor.textField.getSelectionStart()); 24 | String end = source.substring(editor.textField.getSelectionEnd()); 25 | return start + end; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/mediator/example/components/DeleteButton.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.mediator.example.components; 2 | 3 | import refactoring_guru.mediator.example.mediator.Mediator; 4 | 5 | import javax.swing.*; 6 | import java.awt.event.ActionEvent; 7 | 8 | /** 9 | * Concrete components don't talk with each other. They have only one 10 | * communication channel–sending requests to the mediator. 11 | */ 12 | public class DeleteButton extends JButton implements Component { 13 | private Mediator mediator; 14 | 15 | public DeleteButton() { 16 | super("Del"); 17 | } 18 | 19 | @Override 20 | public void setMediator(Mediator mediator) { 21 | this.mediator = mediator; 22 | } 23 | 24 | @Override 25 | protected void fireActionPerformed(ActionEvent actionEvent) { 26 | mediator.deleteNote(); 27 | } 28 | 29 | @Override 30 | public String getName() { 31 | return "DelButton"; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/mediator/example/components/SaveButton.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.mediator.example.components; 2 | 3 | import refactoring_guru.mediator.example.mediator.Mediator; 4 | 5 | import javax.swing.*; 6 | import java.awt.event.ActionEvent; 7 | 8 | /** 9 | * Concrete components don't talk with each other. They have only one 10 | * communication channel–sending requests to the mediator. 11 | */ 12 | public class SaveButton extends JButton implements Component { 13 | private Mediator mediator; 14 | 15 | public SaveButton() { 16 | super("Save"); 17 | } 18 | 19 | @Override 20 | public void setMediator(Mediator mediator) { 21 | this.mediator = mediator; 22 | } 23 | 24 | @Override 25 | protected void fireActionPerformed(ActionEvent actionEvent) { 26 | mediator.saveChanges(); 27 | } 28 | 29 | @Override 30 | public String getName() { 31 | return "SaveButton"; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/visitor/example/shapes/Dot.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.visitor.example.shapes; 2 | 3 | import refactoring_guru.visitor.example.visitor.Visitor; 4 | 5 | public class Dot implements Shape { 6 | private int id; 7 | private int x; 8 | private int y; 9 | 10 | public Dot() { 11 | } 12 | 13 | public Dot(int id, int x, int y) { 14 | this.id = id; 15 | this.x = x; 16 | this.y = y; 17 | } 18 | 19 | @Override 20 | public void move(int x, int y) { 21 | // move shape 22 | } 23 | 24 | @Override 25 | public void draw() { 26 | // draw shape 27 | } 28 | 29 | public String accept(Visitor visitor) { 30 | return visitor.visitDot(this); 31 | } 32 | 33 | public int getX() { 34 | return x; 35 | } 36 | 37 | public int getY() { 38 | return y; 39 | } 40 | 41 | public int getId() { 42 | return id; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/mediator/example/Demo.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.mediator.example; 2 | 3 | import refactoring_guru.mediator.example.components.*; 4 | import refactoring_guru.mediator.example.mediator.Editor; 5 | import refactoring_guru.mediator.example.mediator.Mediator; 6 | 7 | import javax.swing.*; 8 | 9 | /** 10 | * Demo class. Everything comes together here. 11 | */ 12 | public class Demo { 13 | public static void main(String[] args) { 14 | Mediator mediator = new Editor(); 15 | 16 | mediator.registerComponent(new Title()); 17 | mediator.registerComponent(new TextBox()); 18 | mediator.registerComponent(new AddButton()); 19 | mediator.registerComponent(new DeleteButton()); 20 | mediator.registerComponent(new SaveButton()); 21 | mediator.registerComponent(new List(new DefaultListModel())); 22 | mediator.registerComponent(new Filter()); 23 | 24 | mediator.createGUI(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/chain_of_responsibility/example/middleware/UserExistsMiddleware.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.chain_of_responsibility.example.middleware; 2 | 3 | import refactoring_guru.chain_of_responsibility.example.server.Server; 4 | 5 | /** 6 | * ConcreteHandler. Checks whether a user with the given credentials exists. 7 | */ 8 | public class UserExistsMiddleware extends Middleware { 9 | private Server server; 10 | 11 | public UserExistsMiddleware(Server server) { 12 | this.server = server; 13 | } 14 | 15 | public boolean check(String email, String password) { 16 | if (!server.hasEmail(email)) { 17 | System.out.println("This email is not registered!"); 18 | return false; 19 | } 20 | if (!server.isValidPassword(email, password)) { 21 | System.out.println("Wrong password!"); 22 | return false; 23 | } 24 | return checkNext(email, password); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/chain_of_responsibility/example/middleware/Middleware.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.chain_of_responsibility.example.middleware; 2 | 3 | /** 4 | * Base middleware class. 5 | */ 6 | public abstract class Middleware { 7 | private Middleware next; 8 | 9 | /** 10 | * Builds chains of middleware objects. 11 | */ 12 | public Middleware linkWith(Middleware next) { 13 | this.next = next; 14 | return next; 15 | } 16 | 17 | /** 18 | * Subclasses will implement this method with concrete checks. 19 | */ 20 | public abstract boolean check(String email, String password); 21 | 22 | /** 23 | * Runs check on the next object in chain or ends traversing if we're in 24 | * last object in chain. 25 | */ 26 | protected boolean checkNext(String email, String password) { 27 | if (next == null) { 28 | return true; 29 | } 30 | return next.check(email, password); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Iterator/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | Array 2 | ( 3 | [0] => Name 4 | [1] => Age 5 | [2] => Owner 6 | [3] => Breed 7 | [4] => Image 8 | [5] => Color 9 | [6] => Texture 10 | [7] => Fur 11 | [8] => Size 12 | ) 13 | Array 14 | ( 15 | [0] => Steve 16 | [1] => 3 17 | [2] => Alexander Shvets 18 | [3] => Bengal 19 | [4] => /cats/bengal.jpg 20 | [5] => Brown 21 | [6] => Stripes 22 | [7] => Short 23 | [8] => Medium 24 | ) 25 | Array 26 | ( 27 | [0] => Siri 28 | [1] => 2 29 | [2] => Alexander Shvets 30 | [3] => Domestic short-haired 31 | [4] => /cats/domestic-sh.jpg 32 | [5] => Black 33 | [6] => Solid 34 | [7] => Medium 35 | [8] => Medium 36 | ) 37 | Array 38 | ( 39 | [0] => Fluffy 40 | [1] => 5 41 | [2] => John Smith 42 | [3] => Maine Coon 43 | [4] => /cats/Maine-Coon.jpg 44 | [5] => Gray 45 | [6] => Stripes 46 | [7] => Long 47 | [8] => Large 48 | ) 49 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/mediator/mediators/mediator1.js: -------------------------------------------------------------------------------- 1 | /* --- references to connected components --- */ 2 | let component1 = null; 3 | let component2 = null; 4 | 5 | // concrete Mediator implement cooperative behavior by coordinating several components 6 | class Mediator1 { 7 | constructor(c1, c2) { 8 | component1 = c1; 9 | component1.mediator = this; 10 | 11 | component2 = c2; 12 | component2.mediator = this; 13 | } 14 | 15 | // method used by components to notify Mediator about some events 16 | // Mediator may react to these events and pass execution to other components 17 | notify(sender, event) { 18 | if (event === 'A') { 19 | console.log('Mediator1 react on event A and trigger following operations:'); 20 | component2.doC(); 21 | } 22 | 23 | if (event === 'D') { 24 | console.log('Mediator1 react on event D and trigger following operations:'); 25 | component1.doB(); 26 | component2.doC(); 27 | } 28 | } 29 | } 30 | 31 | export { Mediator1 }; 32 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/bridge/example/OutputDemo.txt: -------------------------------------------------------------------------------- 1 | Tests with basic remote. 2 | Remote: power toggle 3 | ------------------------------------ 4 | | I'm TV set. 5 | | I'm enabled 6 | | Current volume is 30% 7 | | Current channel is 1 8 | ------------------------------------ 9 | 10 | Tests with advanced remote. 11 | Remote: power toggle 12 | Remote: mute 13 | ------------------------------------ 14 | | I'm TV set. 15 | | I'm disabled 16 | | Current volume is 0% 17 | | Current channel is 1 18 | ------------------------------------ 19 | 20 | Tests with basic remote. 21 | Remote: power toggle 22 | ------------------------------------ 23 | | I'm radio. 24 | | I'm enabled 25 | | Current volume is 30% 26 | | Current channel is 1 27 | ------------------------------------ 28 | 29 | Tests with advanced remote. 30 | Remote: power toggle 31 | Remote: mute 32 | ------------------------------------ 33 | | I'm radio. 34 | | I'm disabled 35 | | Current volume is 0% 36 | | Current channel is 1 37 | ------------------------------------ -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Decorator/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | Website renders comments without filtering (unsafe): 2 | Hello! Nice blog post! 3 | Please visit my homepage. 4 | 7 | 8 | 9 | Website renders comments after stripping all tags (safe): 10 | Hello! Nice blog post! 11 | Please visit my homepage. 12 | 13 | performXSSAttack(); 14 | 15 | 16 | 17 | Website renders a forum post without filtering and formatting (unsafe, ugly): 18 | # Welcome 19 | 20 | This is my first post on this **gorgeous** forum. 21 | 22 | 25 | 26 | 27 | Website renders a forum post after translating markdown markupand filtering some dangerous HTML tags and attributes (safe, pretty): 28 |

Welcome

29 | 30 |

This is my first post on this gorgeous forum.

31 | 32 |

33 | 34 | 35 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: "" 5 | labels: "" 6 | assignees: "" 7 | --- 8 | 9 | **Describe the bug** 10 | A clear and concise description of what the bug is. 11 | 12 | **To Reproduce** 13 | Steps to reproduce the behavior: 14 | 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | 28 | - OS: [e.g. iOS] 29 | - Browser [e.g. chrome, safari] 30 | - Version [e.g. 22] 31 | 32 | **Smartphone (please complete the following information):** 33 | 34 | - Device: [e.g. iPhone6] 35 | - OS: [e.g. iOS8.1] 36 | - Browser [e.g. stock browser, safari] 37 | - Version [e.g. 22] 38 | 39 | **Additional context** 40 | Add any other context about the problem here. 41 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/decorator/example/Demo.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.decorator.example; 2 | 3 | import refactoring_guru.decorator.example.decorators.*; 4 | 5 | public class Demo { 6 | public static void main(String[] args) { 7 | String salaryRecords = "Name,Salary\nJohn Smith,100000\nSteven Jobs,912000"; 8 | DataSourceDecorator encoded = new CompressionDecorator( 9 | new EncryptionDecorator( 10 | new FileDataSource("out/OutputDemo.txt"))); 11 | encoded.writeData(salaryRecords); 12 | DataSource plain = new FileDataSource("out/OutputDemo.txt"); 13 | 14 | System.out.println("- Input ----------------"); 15 | System.out.println(salaryRecords); 16 | System.out.println("- Encoded --------------"); 17 | System.out.println(plain.readData()); 18 | System.out.println("- Decoded --------------"); 19 | System.out.println(encoded.readData()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Visitor/RealWorld/Output.txt: -------------------------------------------------------------------------------- 1 | Client: I can print a report for a whole company: 2 | 3 | SuperStarDevelopment (USD550,000.00) 4 | 5 | --Mobile Development (USD351,000.00) 6 | 7 | $100,000.00 Albert Falmore (designer) 8 | $100,000.00 Ali Halabay (programmer) 9 | $ 90,000.00 Sarah Konor (programmer) 10 | $ 31,000.00 Monica Ronaldino (QA engineer) 11 | $ 30,000.00 James Smith (QA engineer) 12 | 13 | --Tech Support (USD199,000.00) 14 | 15 | $ 70,000.00 Larry Ulbrecht (supervisor) 16 | $ 30,000.00 Elton Pale (operator) 17 | $ 30,000.00 Rajeet Kumar (operator) 18 | $ 34,000.00 John Burnovsky (operator) 19 | $ 35,000.00 Sergey Korolev (operator) 20 | 21 | Client: ...or just for a single department: 22 | 23 | Tech Support (USD199,000.00) 24 | 25 | $ 70,000.00 Larry Ulbrecht (supervisor) 26 | $ 30,000.00 Elton Pale (operator) 27 | $ 30,000.00 Rajeet Kumar (operator) 28 | $ 34,000.00 John Burnovsky (operator) 29 | $ 35,000.00 Sergey Korolev (operator) -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Flyweight.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | FlyweightFactory: I have 5 flyweights: 2 | Camaro2018_Chevrolet_pink 3 | black_C300_Mercedes Benz 4 | C500_Mercedes Benz_red 5 | BMW_M5_red 6 | BMW_white_X6 7 | 8 | Client: Adding a car to database. 9 | FlyweightFactory: Reusing existing flyweight. 10 | Flyweight: Displaying shared {"Owner":null,"Number":null,"Company":"BMW","Model":"M5","Color":"red"} and unique {"Owner":"James Doe","Number":"CL234IR","Company":"BMW","Model":"M5","Color":"red"} state. 11 | 12 | Client: Adding a car to database. 13 | FlyweightFactory: Can't find a flyweight, creating new one. 14 | Flyweight: Displaying shared {"Owner":null,"Number":null,"Company":"BMW","Model":"X1","Color":"red"} and unique {"Owner":"James Doe","Number":"CL234IR","Company":"BMW","Model":"X1","Color":"red"} state. 15 | 16 | FlyweightFactory: I have 6 flyweights: 17 | Camaro2018_Chevrolet_pink 18 | black_C300_Mercedes Benz 19 | C500_Mercedes Benz_red 20 | BMW_M5_red 21 | BMW_white_X6 22 | BMW_red_X1 -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/iterator/example/OutputDemo.txt: -------------------------------------------------------------------------------- 1 | Please specify social network to target spam tool (default:Facebook): 2 | 1. Facebook 3 | 2. LinkedIn 4 | > 1 5 | 6 | Iterating over friends... 7 | 8 | Facebook: Loading 'friends' list of 'anna.smith@bing.com' over the network... 9 | Facebook: Loading profile 'mad_max@ya.com' over the network... 10 | Sent message to: 'mad_max@ya.com'. Message body: 'Hey! This is Anna's friend Josh. Can you do me a favor and like this post [link]?' 11 | Facebook: Loading profile 'catwoman@yahoo.com' over the network... 12 | Sent message to: 'catwoman@yahoo.com'. Message body: 'Hey! This is Anna's friend Josh. Can you do me a favor and like this post [link]?' 13 | 14 | Iterating over coworkers... 15 | 16 | Facebook: Loading 'coworkers' list of 'anna.smith@bing.com' over the network... 17 | Facebook: Loading profile 'sam@amazon.com' over the network... 18 | Sent message to: 'sam@amazon.com'. Message body: 'Hey! This is Anna's boss Jason. Anna told me you would be interested in [link].' 19 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/visitor/example/Demo.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.visitor.example; 2 | 3 | import refactoring_guru.visitor.example.shapes.*; 4 | import refactoring_guru.visitor.example.visitor.XMLExportVisitor; 5 | 6 | public class Demo { 7 | public static void main(String[] args) { 8 | Dot dot = new Dot(1, 10, 55); 9 | Circle circle = new Circle(2, 23, 15, 10); 10 | Rectangle rectangle = new Rectangle(3, 10, 17, 20, 30); 11 | 12 | CompoundShape compoundShape = new CompoundShape(4); 13 | compoundShape.add(dot); 14 | compoundShape.add(circle); 15 | compoundShape.add(rectangle); 16 | 17 | CompoundShape c = new CompoundShape(5); 18 | c.add(dot); 19 | compoundShape.add(c); 20 | 21 | export(circle, compoundShape); 22 | } 23 | 24 | private static void export(Shape... shapes) { 25 | XMLExportVisitor exportVisitor = new XMLExportVisitor(); 26 | System.out.println(exportVisitor.export(shapes)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/mediator/example/components/AddButton.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.mediator.example.components; 2 | 3 | import refactoring_guru.mediator.example.mediator.Mediator; 4 | import refactoring_guru.mediator.example.mediator.Note; 5 | 6 | import javax.swing.*; 7 | import java.awt.event.ActionEvent; 8 | 9 | /** 10 | * Concrete components don't talk with each other. They have only one 11 | * communication channel–sending requests to the mediator. 12 | */ 13 | public class AddButton extends JButton implements Component { 14 | private Mediator mediator; 15 | 16 | public AddButton() { 17 | super("Add"); 18 | } 19 | 20 | @Override 21 | public void setMediator(Mediator mediator) { 22 | this.mediator = mediator; 23 | } 24 | 25 | @Override 26 | protected void fireActionPerformed(ActionEvent actionEvent) { 27 | mediator.addNewNote(new Note()); 28 | } 29 | 30 | @Override 31 | public String getName() { 32 | return "AddButton"; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/builder/builders/builder1.js: -------------------------------------------------------------------------------- 1 | import { Product1 } from './product1'; 2 | 3 | // implementation of building steps for concrete builder 4 | class Builder1 { 5 | // builder instance must have blank product object, which is used in further product assembly 6 | constructor() { 7 | this.reset(); 8 | } 9 | 10 | reset() { 11 | this.product = new Product1(); 12 | } 13 | 14 | // all production steps working with same product instance 15 | producePartA() { 16 | this.product.parts.push('PART-A-1'); 17 | } 18 | 19 | producePartB() { 20 | this.product.parts.push('PART-B-1'); 21 | } 22 | 23 | producePartC() { 24 | this.product.parts.push('PART-C-1'); 25 | } 26 | 27 | // return result to client and reset for building next product (optionally) 28 | getProduct() { 29 | const result = this.product; 30 | // this is optional: builder can wait for explicit reset call from client before result return 31 | this.reset(); 32 | return result; 33 | } 34 | } 35 | 36 | export { Builder1 }; 37 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/builder/builders/builder2.js: -------------------------------------------------------------------------------- 1 | import { Product2 } from './product2'; 2 | 3 | // implementation of building steps for concrete builder 4 | class Builder2 { 5 | // builder instance must have blank product object, which is used in further product assembly 6 | constructor() { 7 | this.reset(); 8 | } 9 | 10 | reset() { 11 | this.product = new Product2(); 12 | } 13 | 14 | // all production steps working with same product instance 15 | producePartA() { 16 | this.product.parts.push('PART-A-2'); 17 | } 18 | 19 | producePartB() { 20 | this.product.parts.push('PART-B-2'); 21 | } 22 | 23 | producePartC() { 24 | this.product.parts.push('PART-C-2'); 25 | } 26 | 27 | // return result to client and reset for building next product (optionally) 28 | getProduct() { 29 | const result = this.product; 30 | // this is optional: builder can wait for explicit reset call from client before result return 31 | this.reset(); 32 | return result; 33 | } 34 | } 35 | 36 | export { Builder2 }; 37 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/facade/facade.js: -------------------------------------------------------------------------------- 1 | import { Subsystem1, Subsystem2 } from './subsystems'; 2 | 3 | // provides simple interface to complex logic of one or many subsystems 4 | // delegates client requests to appropriate objects within subsystem 5 | class Facade { 6 | // Facade can be provided with existing subsystem objects or be forced to create its own 7 | constructor(subsystem1, subsystem2) { 8 | this.subsystem1 = subsystem1 || new Subsystem1(); 9 | this.subsystem2 = subsystem2 || new Subsystem2(); 10 | } 11 | 12 | // this method is shortcut to subsystems functionality 13 | // i.e. it "hides" some complexity behind simple interface 14 | operation() { 15 | let result = 'Facade initializes subsystems\n'; 16 | result += this.subsystem1.operation1(); 17 | result += this.subsystem2.operation1(); 18 | result += 'Facade orders subsytems to perform action:\n'; 19 | result += this.subsystem1.operationN(); 20 | result += this.subsystem2.operationZ(); 21 | return result; 22 | } 23 | } 24 | 25 | export { Facade }; 26 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/template-method/operations/operation.js: -------------------------------------------------------------------------------- 1 | // define skeleton of algorithm composed from primitive operations 2 | class Operation { 3 | // algorithm skeleton 4 | templateMethod() { 5 | this.baseOperation1(); 6 | this.requredOperation1(); 7 | this.baseOperation2(); 8 | this.hook1(); 9 | this.requredOperation2(); 10 | this.baseOperation3(); 11 | this.hook2(); 12 | } 13 | 14 | /* --- default operations implementation --- */ 15 | baseOperation1() { 16 | console.log('Operation: do base operation 1.'); 17 | } 18 | 19 | baseOperation2() { 20 | console.log('Operation: do base operation 2.'); 21 | } 22 | 23 | baseOperation3() { 24 | console.log('Operation: do base operation 3.'); 25 | } 26 | 27 | /* --- operations implemented in subclasses --- */ 28 | requredOperation1() {} 29 | 30 | requredOperation2() {} 31 | 32 | /* --- these methods provide additional extension points somewhere in algoriythm --- */ 33 | hook1() {} 34 | 35 | hook2() {} 36 | } 37 | 38 | export { Operation }; 39 | -------------------------------------------------------------------------------- /courses/guru/js/examples/abstract-factory.js: -------------------------------------------------------------------------------- 1 | import { Factory1, Factory2 } from '../patterns/abstract-factory'; 2 | 3 | /* --- create products of type 1 --- */ 4 | 5 | // use concrete factory 6 | let factory = new Factory1(); 7 | // create product of family A 8 | let productA = factory.createProductA(); 9 | // create product of family B 10 | let productB = factory.createProductB(); 11 | 12 | // call product A method 13 | console.log('Factory #1: %s', productA.productAMethod()); 14 | // call product B method 15 | console.log('Factory #1: %s', productB.productBMethod()); 16 | 17 | /* --- create products of type 2 --- */ 18 | 19 | // use concrete factory 20 | factory = new Factory2(); 21 | // create product of family A 22 | productA = factory.createProductA(); 23 | // create product of family B 24 | productB = factory.createProductB(); 25 | 26 | // call product A method 27 | console.log('Factory #2: %s', productA.productAMethod()); 28 | // call product B method collaborating with product of family A 29 | console.log('Factory #2: %s', productB.productBMethod2(productA)); 30 | -------------------------------------------------------------------------------- /courses/guru/js/examples/chain-of-responsibility.js: -------------------------------------------------------------------------------- 1 | import { Handler1, Handler2, Handler3 } from '../patterns/chain-of-responsibility'; 2 | 3 | // client code usually suited to work with single handler 4 | // in most cases it is not even aware that handler is part of chain 5 | function clientCode(handler) { 6 | const requests = [1, 2, 3]; 7 | requests.forEach((request) => { 8 | console.log(`Client: who can handle ${request}`); 9 | const result = handler.handle(request); 10 | if (result) { 11 | console.log(` ${result}`); 12 | } else { 13 | console.log(` Nobody can handle ${result}`); 14 | } 15 | }); 16 | } 17 | 18 | /* --- build chain --- */ 19 | const handler1 = new Handler1(); 20 | const handler2 = new Handler2(); 21 | const handler3 = new Handler3(); 22 | 23 | handler1.setNext(handler2).setNext(handler3); 24 | 25 | /* --- sending requests --- */ 26 | 27 | // all handlers 28 | console.log('Chain: 1 -> 2 -> 3\n'); 29 | clientCode(handler1); 30 | 31 | // some of handlers 32 | console.log('Subchain: 2 -> 3\n'); 33 | clientCode(handler2); 34 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/command/invokers/invoker1.js: -------------------------------------------------------------------------------- 1 | let onStart = null; 2 | let onFinish = null; 3 | 4 | function isCommand(object) { 5 | return object.execute !== undefined; 6 | } 7 | 8 | // associated with one or several commands 9 | // it sends request to command 10 | class Invoker1 { 11 | // initialize command 12 | setOnStart(command) { 13 | onStart = command; 14 | } 15 | 16 | // initialize command 17 | setOnFinish(command) { 18 | onFinish = command; 19 | } 20 | 21 | // Invoker doesn't depend on concrete Command or Receiver 22 | // it pass request to Receiver indirectly by executing some Command 23 | doSmthImportant() { 24 | console.log('Invoker1: Does anybody want something done before I begin?'); 25 | if (isCommand) { 26 | onStart.execute(); 27 | } 28 | 29 | console.log('Invoker1: ...doing something really important...'); 30 | console.log('Invoker1: Does anybody want something done after I finish?'); 31 | if (isCommand) { 32 | onFinish.execute(); 33 | } 34 | } 35 | } 36 | 37 | export { Invoker1 }; 38 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/bridge/example/Demo.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.bridge.example; 2 | 3 | import refactoring_guru.bridge.example.devices.Device; 4 | import refactoring_guru.bridge.example.devices.Radio; 5 | import refactoring_guru.bridge.example.devices.Tv; 6 | import refactoring_guru.bridge.example.remotes.AdvancedRemote; 7 | import refactoring_guru.bridge.example.remotes.BasicRemote; 8 | 9 | public class Demo { 10 | public static void main(String[] args) { 11 | testDevice(new Tv()); 12 | testDevice(new Radio()); 13 | } 14 | 15 | public static void testDevice(Device device) { 16 | System.out.println("Tests with basic remote."); 17 | BasicRemote basicRemote = new BasicRemote(device); 18 | basicRemote.power(); 19 | device.printStatus(); 20 | 21 | System.out.println("Tests with advanced remote."); 22 | AdvancedRemote advancedRemote = new AdvancedRemote(device); 23 | advancedRemote.power(); 24 | advancedRemote.mute(); 25 | device.printStatus(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/state/example/states/LockedState.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.state.example.states; 2 | 3 | import refactoring_guru.state.example.ui.Player; 4 | 5 | /** 6 | * Concrete states provide the special implementation for all interface methods. 7 | */ 8 | public class LockedState extends State { 9 | 10 | LockedState(Player player) { 11 | super(player); 12 | player.setPlaying(false); 13 | } 14 | 15 | @Override 16 | public String onLock() { 17 | if (player.isPlaying()) { 18 | player.changeState(new ReadyState(player)); 19 | return "Stop playing"; 20 | } else { 21 | return "Locked..."; 22 | } 23 | } 24 | 25 | @Override 26 | public String onPlay() { 27 | player.changeState(new ReadyState(player)); 28 | return "Ready"; 29 | } 30 | 31 | @Override 32 | public String onNext() { 33 | return "Locked..."; 34 | } 35 | 36 | @Override 37 | public String onPrevious() { 38 | return "Locked..."; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/builder/example/components/Engine.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.builder.example.components; 2 | 3 | /** 4 | * Just another feature of a car. 5 | */ 6 | public class Engine { 7 | private final double volume; 8 | private double mileage; 9 | private boolean started; 10 | 11 | public Engine(double volume, double mileage) { 12 | this.volume = volume; 13 | this.mileage = mileage; 14 | } 15 | 16 | public void on() { 17 | started = true; 18 | } 19 | 20 | public void off() { 21 | started = false; 22 | } 23 | 24 | public boolean isStarted() { 25 | return started; 26 | } 27 | 28 | public void go(double mileage) { 29 | if (started) { 30 | this.mileage += mileage; 31 | } else { 32 | System.err.println("Cannot go(), you must start engine first!"); 33 | } 34 | } 35 | 36 | public double getVolume() { 37 | return volume; 38 | } 39 | 40 | public double getMileage() { 41 | return mileage; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/strategy/example/order/Order.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.strategy.example.order; 2 | 3 | import refactoring_guru.strategy.example.strategies.PayStrategy; 4 | 5 | /** 6 | * Order class. Doesn't know the concrete payment method (strategy) user has 7 | * picked. It uses common strategy interface to delegate collecting payment data 8 | * to strategy object. It can be used to save order to database. 9 | */ 10 | public class Order { 11 | private int totalCost = 0; 12 | private boolean isClosed = false; 13 | 14 | public void processOrder(PayStrategy strategy) { 15 | strategy.collectPaymentDetails(); 16 | // Here we could collect and store payment data from the strategy. 17 | } 18 | 19 | public void setTotalCost(int cost) { 20 | this.totalCost += cost; 21 | } 22 | 23 | public int getTotalCost() { 24 | return totalCost; 25 | } 26 | 27 | public boolean isClosed() { 28 | return isClosed; 29 | } 30 | 31 | public void setClosed() { 32 | isClosed = true; 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /courses/guru/js/patterns/memento/caretaker.js: -------------------------------------------------------------------------------- 1 | // doesn't depend on Memento 2 | // doesn't have access to Originator state stored in Memento 3 | // it works with all mementos via Memento interface 4 | class Caretaker { 5 | #mementos = []; 6 | #originator = null; 7 | 8 | constructor(originator) { 9 | this.#originator = originator; 10 | } 11 | 12 | // save current memento state 13 | backup() { 14 | console.log('\nCaretaker: saving Originator state...'); 15 | this.#mementos.push(this.#originator.save()); 16 | } 17 | 18 | // restore memento to previous state 19 | undo() { 20 | if (!this.#mementos.length) { 21 | return; 22 | } 23 | const memento = this.#mementos.pop(); 24 | console.log(`Caretaker: restoring state to: ${memento.getName()}`); 25 | this.#originator.restore(memento); 26 | } 27 | 28 | // list all saved mementos 29 | showHistory() { 30 | console.log('Caretaker: list of mementos:'); 31 | this.#mementos.forEach((memento) => { 32 | console.log(memento.getName()); 33 | }); 34 | } 35 | } 36 | 37 | export { Caretaker }; 38 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/facade/example/facade/VideoConversionFacade.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.facade.example.facade; 2 | 3 | import refactoring_guru.facade.example.some_complex_media_library.*; 4 | 5 | import java.io.File; 6 | 7 | public class VideoConversionFacade { 8 | public File convertVideo(String fileName, String format) { 9 | System.out.println("VideoConversionFacade: conversion started."); 10 | VideoFile file = new VideoFile(fileName); 11 | Codec sourceCodec = CodecFactory.extract(file); 12 | Codec destinationCodec; 13 | if (format.equals("mp4")) { 14 | destinationCodec = new OggCompressionCodec(); 15 | } else { 16 | destinationCodec = new MPEG4CompressionCodec(); 17 | } 18 | VideoFile buffer = BitrateReader.read(file, sourceCodec); 19 | VideoFile intermediateResult = BitrateReader.convert(buffer, destinationCodec); 20 | File result = (new AudioMixer()).fix(intermediateResult); 21 | System.out.println("VideoConversionFacade: conversion completed."); 22 | return result; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/decorator/example/decorators/EncryptionDecorator.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.decorator.example.decorators; 2 | 3 | import java.util.Base64; 4 | 5 | public class EncryptionDecorator extends DataSourceDecorator { 6 | 7 | public EncryptionDecorator(DataSource source) { 8 | super(source); 9 | } 10 | 11 | @Override 12 | public void writeData(String data) { 13 | super.writeData(encode(data)); 14 | } 15 | 16 | @Override 17 | public String readData() { 18 | return decode(super.readData()); 19 | } 20 | 21 | private String encode(String data) { 22 | byte[] result = data.getBytes(); 23 | for (int i = 0; i < result.length; i++) { 24 | result[i] += (byte) 1; 25 | } 26 | return Base64.getEncoder().encodeToString(result); 27 | } 28 | 29 | private String decode(String data) { 30 | byte[] result = Base64.getDecoder().decode(data); 31 | for (int i = 0; i < result.length; i++) { 32 | result[i] -= (byte) 1; 33 | } 34 | return new String(result); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/decorator/example/decorators/FileDataSource.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.decorator.example.decorators; 2 | 3 | import java.io.*; 4 | 5 | public class FileDataSource implements DataSource { 6 | private String name; 7 | 8 | public FileDataSource(String name) { 9 | this.name = name; 10 | } 11 | 12 | @Override 13 | public void writeData(String data) { 14 | File file = new File(name); 15 | try (OutputStream fos = new FileOutputStream(file)) { 16 | fos.write(data.getBytes(), 0, data.length()); 17 | } catch (IOException ex) { 18 | System.out.println(ex.getMessage()); 19 | } 20 | } 21 | 22 | @Override 23 | public String readData() { 24 | char[] buffer = null; 25 | File file = new File(name); 26 | try (FileReader reader = new FileReader(file)) { 27 | buffer = new char[(int) file.length()]; 28 | reader.read(buffer); 29 | } catch (IOException ex) { 30 | System.out.println(ex.getMessage()); 31 | } 32 | return new String(buffer); 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /courses/guru/js/examples/composite.js: -------------------------------------------------------------------------------- 1 | import { ComponentLeaf, ComponentComposite } from '../patterns/composite'; 2 | 3 | // client code works with all (simple and both) components via base interface 4 | function clientCode(component) { 5 | console.log(`RESULT: ${component.operation()}`); 6 | } 7 | 8 | /* --- simple component --- */ 9 | const leaf = new ComponentLeaf(); 10 | 11 | console.log('Client: Simple component:'); 12 | clientCode(leaf); 13 | 14 | /* --- composite component --- */ 15 | // make "root" component 16 | const tree = new ComponentComposite(); 17 | // make one composite component 18 | const branch1 = new ComponentComposite(); 19 | // add some simple components to this composite component 20 | branch1.add(new ComponentLeaf()); 21 | branch1.add(new ComponentLeaf()); 22 | 23 | // make another composite component 24 | const branch2 = new ComponentComposite(); 25 | // add some simple component to another composite component 26 | branch2.add(new ComponentLeaf()); 27 | 28 | // add composite components to "root" component 29 | tree.add(branch1); 30 | tree.add(branch2); 31 | 32 | console.log('Client: Composite component:'); 33 | clientCode(tree); 34 | -------------------------------------------------------------------------------- /courses/guru/php/src/RefactoringGuru/Memento/Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Originator: My initial state is: Super-duper-super-puper-super. 2 | 3 | Caretaker: Saving Originator's state... 4 | Originator: I'm doing something important. 5 | Originator: and my state has changed to: srGIngezAEboNPDjBkuvymJKUtMSFX 6 | 7 | Caretaker: Saving Originator's state... 8 | Originator: I'm doing something important. 9 | Originator: and my state has changed to: UwCZQaHJOiERLlchyVuMbXNtpqTgWF 10 | 11 | Caretaker: Saving Originator's state... 12 | Originator: I'm doing something important. 13 | Originator: and my state has changed to: incqsdoJXkbDUuVOvRFYyKBgfzwZCQ 14 | 15 | Caretaker: Here's the list of mementos: 16 | 2018-06-04 14:50:39 / (Super-dup...) 17 | 2018-06-04 14:50:39 / (srGIngezA...) 18 | 2018-06-04 14:50:39 / (UwCZQaHJO...) 19 | 20 | Client: Now, let's rollback! 21 | 22 | Caretaker: Restoring state to: 2018-06-04 14:50:39 / (UwCZQaHJO...) 23 | Originator: My state has changed to: UwCZQaHJOiERLlchyVuMbXNtpqTgWF 24 | 25 | Client: Once more! 26 | 27 | Caretaker: Restoring state to: 2018-06-04 14:50:39 / (srGIngezA...) 28 | Originator: My state has changed to: srGIngezAEboNPDjBkuvymJKUtMSFX 29 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/singleton/example/thread_safe/DemoMultiThread.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.singleton.example.thread_safe; 2 | 3 | public class DemoMultiThread { 4 | public static void main(String[] args) { 5 | System.out.println("If you see the same value, then singleton was reused (yay!)" + "\n" + 6 | "If you see different values, then 2 singletons were created (booo!!)" + "\n\n" + 7 | "RESULT:" + "\n"); 8 | Thread threadFoo = new Thread(new ThreadFoo()); 9 | Thread threadBar = new Thread(new ThreadBar()); 10 | threadFoo.start(); 11 | threadBar.start(); 12 | } 13 | 14 | static class ThreadFoo implements Runnable { 15 | @Override 16 | public void run() { 17 | Singleton singleton = Singleton.getInstance("FOO"); 18 | System.out.println(singleton.value); 19 | } 20 | } 21 | 22 | static class ThreadBar implements Runnable { 23 | @Override 24 | public void run() { 25 | Singleton singleton = Singleton.getInstance("BAR"); 26 | System.out.println(singleton.value); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/singleton/example/non_thread_safe/DemoMultiThread.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.singleton.example.non_thread_safe; 2 | 3 | public class DemoMultiThread { 4 | public static void main(String[] args) { 5 | System.out.println("If you see the same value, then singleton was reused (yay!)" + "\n" + 6 | "If you see different values, then 2 singletons were created (booo!!)" + "\n\n" + 7 | "RESULT:" + "\n"); 8 | Thread threadFoo = new Thread(new ThreadFoo()); 9 | Thread threadBar = new Thread(new ThreadBar()); 10 | threadFoo.start(); 11 | threadBar.start(); 12 | } 13 | 14 | static class ThreadFoo implements Runnable { 15 | @Override 16 | public void run() { 17 | Singleton singleton = Singleton.getInstance("FOO"); 18 | System.out.println(singleton.value); 19 | } 20 | } 21 | 22 | static class ThreadBar implements Runnable { 23 | @Override 24 | public void run() { 25 | Singleton singleton = Singleton.getInstance("BAR"); 26 | System.out.println(singleton.value); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /courses/guru/c#/RefactoringGuru.DesignPatterns.Memento.Structural/Output.txt: -------------------------------------------------------------------------------- 1 | Originator: My initial state is: Super-duper-super-puper-super. 2 | 3 | Caretaker: Saving Originator's state... 4 | Originator: I'm doing something important. 5 | Originator: and my state has changed to: oGyQIIatlDDWNgYYqJATTmdwnnGZQj 6 | 7 | Caretaker: Saving Originator's state... 8 | Originator: I'm doing something important. 9 | Originator: and my state has changed to: jBtMDDWogzzRJbTTmEwOOhZrjjBULe 10 | 11 | Caretaker: Saving Originator's state... 12 | Originator: I'm doing something important. 13 | Originator: and my state has changed to: exoHyyRkbuuNEXOhhArKccUmexPPHZ 14 | 15 | Caretaker: Here's the list of mementos: 16 | 12.06.2018 15:52:45 / (Super-dup...) 17 | 12.06.2018 15:52:46 / (oGyQIIatl...) 18 | 12.06.2018 15:52:46 / (jBtMDDWog...) 19 | 20 | Client: Now, let's rollback! 21 | 22 | Caretaker: Restoring state to: 12.06.2018 15:52:46 / (jBtMDDWog...) 23 | Originator: My state has changed to: jBtMDDWogzzRJbTTmEwOOhZrjjBULe 24 | 25 | Client: Once more! 26 | 27 | Caretaker: Restoring state to: 12.06.2018 15:52:46 / (oGyQIIatl...) 28 | Originator: My state has changed to: oGyQIIatlDDWNgYYqJATTmdwnnGZQj -------------------------------------------------------------------------------- /courses/guru/js/patterns/observer/subjects/subject1.js: -------------------------------------------------------------------------------- 1 | // manage subscribers 2 | class Subject1 { 3 | // Subject state 4 | state = null; 5 | 6 | // list of subscribers 7 | #observers = []; 8 | 9 | // attach Observer to Subject 10 | attach(observer) { 11 | console.log('Subject1: attached to observer.'); 12 | this.#observers.push(observer); 13 | } 14 | 15 | // detach Observer from Subject 16 | detach(observer) { 17 | const observerIndex = this.#observers.indexOf(observer); 18 | this.#observers.splice(observerIndex, 1); 19 | console.log('Subject1: detached observer.'); 20 | } 21 | 22 | // notify all Observers about event 23 | notify() { 24 | console.log('Subject1: notifying observers...'); 25 | this.#observers.forEach((observer) => { 26 | observer.update(this); 27 | }); 28 | } 29 | 30 | // some business logic that triggers notification method when smth is happened 31 | someBusinessLogic() { 32 | console.log('\nSubject1: doing some business logic.'); 33 | this.state = Math.floor(Math.random() * (10 + 1)); 34 | 35 | console.log(`Subject1: state changed to: ${this.state}`); 36 | this.notify(); 37 | } 38 | } 39 | 40 | export { Subject1 }; 41 | -------------------------------------------------------------------------------- /courses/guru/java/src/refactoring_guru/memento/example/Demo.java: -------------------------------------------------------------------------------- 1 | package refactoring_guru.memento.example; 2 | 3 | import refactoring_guru.memento.example.editor.Editor; 4 | import refactoring_guru.memento.example.shapes.Circle; 5 | import refactoring_guru.memento.example.shapes.CompoundShape; 6 | import refactoring_guru.memento.example.shapes.Dot; 7 | import refactoring_guru.memento.example.shapes.Rectangle; 8 | 9 | import java.awt.*; 10 | 11 | public class Demo { 12 | public static void main(String[] args) { 13 | Editor editor = new Editor(); 14 | editor.loadShapes( 15 | new Circle(10, 10, 10, Color.BLUE), 16 | 17 | new CompoundShape( 18 | new Circle(110, 110, 50, Color.RED), 19 | new Dot(160, 160, Color.RED) 20 | ), 21 | 22 | new CompoundShape( 23 | new Rectangle(250, 250, 100, 100, Color.GREEN), 24 | new Dot(240, 240, Color.GREEN), 25 | new Dot(240, 360, Color.GREEN), 26 | new Dot(360, 360, Color.GREEN), 27 | new Dot(360, 240, Color.GREEN) 28 | ) 29 | ); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /courses/guru/js/examples/singleton.js: -------------------------------------------------------------------------------- 1 | import { Singleton } from '../patterns/singleton'; 2 | 3 | // create object #1 with new 4 | const singleton1 = new Singleton(); 5 | console.log(singleton1); 6 | 7 | // create object #2 with new 8 | const singleton2 = new Singleton(); 9 | console.log(singleton2); 10 | 11 | // check: are they totally equal? 12 | // must be true 13 | console.log(singleton1 === singleton2); 14 | 15 | // create object #3 with method 16 | const singleton3 = Singleton.getInstance(); 17 | console.log(singleton3); 18 | 19 | // create object #4 with method 20 | const singleton4 = Singleton.getInstance(); 21 | console.log(singleton4); 22 | 23 | // check: are they totally equal? 24 | // must be true 25 | console.log(singleton3 === singleton4); 26 | 27 | // check: is object instance of Singleton 28 | // must be true 29 | console.log(singleton1 instanceof Singleton); 30 | console.log(singleton3 instanceof Singleton); 31 | 32 | // read object property 33 | // must return property value 34 | console.log(singleton1.password); 35 | 36 | // check: direct access to instance 37 | // must be undefined 38 | console.log(Singleton.instance); 39 | 40 | // check: direct access to private data 41 | // must be undefined 42 | console.log(Singleton.privateObject); 43 | --------------------------------------------------------------------------------