├── .gitignore ├── todos.txt ├── src ├── test │ ├── resources │ │ ├── dummy │ │ │ ├── file1.txt │ │ │ ├── file2.txt │ │ │ ├── file3.txt │ │ │ └── another │ │ │ │ └── file4.txt │ │ ├── FizzBuzz.feature │ │ └── Calculator.feature │ └── java │ │ └── com │ │ └── javamultiplex │ │ ├── pattern │ │ ├── behavioral │ │ │ ├── visitor │ │ │ │ ├── example1 │ │ │ │ │ └── VisitorClient.java │ │ │ │ └── example2 │ │ │ │ │ └── ShoppingCartClient.java │ │ │ ├── state │ │ │ │ └── example1 │ │ │ │ │ └── CourierClient.java │ │ │ ├── chainofresponsibility │ │ │ │ └── ATMDispenserClient.java │ │ │ ├── command │ │ │ │ └── example1 │ │ │ │ │ └── BrokerClient.java │ │ │ ├── observer │ │ │ │ ├── example1 │ │ │ │ │ └── NewsAgencyClient.java │ │ │ │ └── example2 │ │ │ │ │ └── NumberSystemClient.java │ │ │ ├── templatemethod │ │ │ │ └── TemplateMethodClient.java │ │ │ ├── mediator │ │ │ │ └── ChatClient.java │ │ │ └── iterator │ │ │ │ └── IteratorClient.java │ │ ├── structural │ │ │ ├── proxy │ │ │ │ └── ProxyClient.java │ │ │ ├── adapter │ │ │ │ ├── example1 │ │ │ │ │ └── DrawingClient.java │ │ │ │ └── example2 │ │ │ │ │ └── VoltClient.java │ │ │ ├── composite │ │ │ │ └── example1 │ │ │ │ │ └── DepartmentClient.java │ │ │ ├── facade │ │ │ │ └── ShopKeeperClient.java │ │ │ ├── decorator │ │ │ │ └── example2 │ │ │ │ │ └── CarClient.java │ │ │ └── bridge │ │ │ │ └── example1 │ │ │ │ └── ShapeClient.java │ │ └── creational │ │ │ ├── factory │ │ │ ├── example1 │ │ │ │ └── PointClient.java │ │ │ └── example2 │ │ │ │ └── NotificationClient.java │ │ │ ├── builder │ │ │ ├── single │ │ │ │ ├── example1 │ │ │ │ │ └── PersonClient.java │ │ │ │ └── example2 │ │ │ │ │ └── CodeClient.java │ │ │ └── multi │ │ │ │ └── PersonClient.java │ │ │ └── singleton │ │ │ ├── InnerStaticSingletonClient.java │ │ │ └── SingletonV4Client.java │ │ ├── testing │ │ ├── principles │ │ │ ├── example4 │ │ │ │ ├── DivisionTest.java │ │ │ │ └── CalculatorV1Test.java │ │ │ └── example3 │ │ │ │ ├── DateTimeServiceTest.java │ │ │ │ └── DateTimeServiceV2Test.java │ │ └── tddbdd │ │ │ ├── example2 │ │ │ ├── FizzBuzzTest.java │ │ │ └── FizzBuzzStepdefs.java │ │ │ └── example1 │ │ │ ├── CalculatorTest.java │ │ │ └── CalculatorStepdefs.java │ │ ├── principle │ │ ├── yagni │ │ │ └── BinarySearchTest.java │ │ ├── solid │ │ │ ├── dip │ │ │ │ └── bad │ │ │ │ │ └── ProjectTest.java │ │ │ ├── lsp │ │ │ │ ├── good │ │ │ │ │ └── AreaCalculatorTest.java │ │ │ │ └── bad │ │ │ │ │ └── AreaCalculatorTest.java │ │ │ └── isp │ │ │ │ ├── good │ │ │ │ └── OldGenerationPrinterTest.java │ │ │ │ └── bad │ │ │ │ └── NewGenerationPrinterTest.java │ │ ├── dry │ │ │ └── WebsiteTest.java │ │ ├── delegation │ │ │ ├── example1 │ │ │ │ └── TravelAgentTest.java │ │ │ └── example2 │ │ │ │ └── PrinterControllerTest.java │ │ └── kiss │ │ │ └── Example1Test.java │ │ └── util │ │ └── SerializationUtil.java └── main │ └── java │ └── com │ └── javamultiplex │ ├── principle │ ├── dry │ │ ├── README.md │ │ └── Calculator.java │ ├── solid │ │ ├── isp │ │ │ ├── README.md │ │ │ ├── good │ │ │ │ ├── Fax.java │ │ │ │ ├── Printer.java │ │ │ │ ├── Scanner.java │ │ │ │ ├── OldGenerationPrinter.java │ │ │ │ └── NewGenerationPrinter.java │ │ │ ├── bad │ │ │ │ ├── Machine.java │ │ │ │ ├── NewGenerationPrinter.java │ │ │ │ └── OldGenerationPrinter.java │ │ │ └── Document.java │ │ ├── ocp │ │ │ ├── README.md │ │ │ ├── Color.java │ │ │ ├── Size.java │ │ │ ├── good │ │ │ │ ├── Specification.java │ │ │ │ ├── Filter.java │ │ │ │ ├── ProductFilter.java │ │ │ │ ├── SizeSpecification.java │ │ │ │ ├── ColorSpecification.java │ │ │ │ └── AndSpecification.java │ │ │ ├── Product.java │ │ │ └── bad │ │ │ │ └── ProductFilter.java │ │ ├── lsp │ │ │ ├── README.md │ │ │ ├── good │ │ │ │ ├── Shape.java │ │ │ │ ├── Square.java │ │ │ │ └── Rectangle.java │ │ │ └── bad │ │ │ │ ├── Square.java │ │ │ │ └── Rectangle.java │ │ ├── srp │ │ │ ├── README.md │ │ │ ├── example1 │ │ │ │ ├── good │ │ │ │ │ ├── Employee.java │ │ │ │ │ ├── HRPromotions.java │ │ │ │ │ └── FinITCalculations.java │ │ │ │ └── bad │ │ │ │ │ └── Employee.java │ │ │ └── example2 │ │ │ │ └── good │ │ │ │ ├── Journal.java │ │ │ │ └── JournalPersistence.java │ │ ├── dip │ │ │ ├── README.md │ │ │ ├── good │ │ │ │ ├── Developer.java │ │ │ │ ├── BackendDeveloper.java │ │ │ │ ├── FrontendDeveloper.java │ │ │ │ └── Project.java │ │ │ └── bad │ │ │ │ ├── BackendDeveloper.java │ │ │ │ ├── FrontEndDeveloper.java │ │ │ │ └── Project.java │ │ └── README.md │ ├── yagni │ │ ├── README.md │ │ ├── SortAlgorithm.java │ │ ├── BubbleSort.java │ │ └── BinarySearch.java │ ├── delegation │ │ ├── example1 │ │ │ ├── README.md │ │ │ ├── TravelBooking.java │ │ │ ├── AirBooking.java │ │ │ ├── TrainBooking.java │ │ │ └── TravelAgent.java │ │ └── example2 │ │ │ ├── Printer.java │ │ │ ├── HPPrinter.java │ │ │ ├── CanonPrinter.java │ │ │ ├── EpsonPrinter.java │ │ │ └── PrinterController.java │ └── kiss │ │ ├── README.md │ │ ├── Example1.java │ │ └── Example2.java │ ├── pattern │ ├── creational │ │ ├── README.md │ │ ├── uml │ │ │ ├── Builder_Design_Pattern_UML.jpg │ │ │ ├── Prototype_Design_Pattern_UML.jpg │ │ │ └── singleton_pattern_uml_diagram.jpg │ │ ├── abstractfactory │ │ │ ├── README.md │ │ │ ├── Color.java │ │ │ ├── Shape.java │ │ │ ├── AbstractFactory.java │ │ │ ├── Red.java │ │ │ ├── Black.java │ │ │ ├── Square.java │ │ │ ├── White.java │ │ │ ├── Circle.java │ │ │ ├── Rectangle.java │ │ │ ├── FactoryProvider.java │ │ │ ├── ColorFactory.java │ │ │ └── ShapeFactory.java │ │ ├── multiton │ │ │ ├── SubSystem.java │ │ │ └── Printer.java │ │ ├── factory │ │ │ ├── example2 │ │ │ │ ├── Notification.java │ │ │ │ ├── NotificationChannel.java │ │ │ │ ├── SMSNotification.java │ │ │ │ ├── PushNotification.java │ │ │ │ ├── EmailNotification.java │ │ │ │ └── NotificationFactory.java │ │ │ ├── README.md │ │ │ └── example1 │ │ │ │ └── Point.java │ │ ├── singleton │ │ │ ├── README.md │ │ │ ├── InnerStaticSingleton.java │ │ │ ├── EnumBasedSingleton.java │ │ │ ├── BasicSingleton.java │ │ │ ├── SingletonV2.java │ │ │ ├── StaticBlockSingleton.java │ │ │ └── SingletonV3.java │ │ ├── prototype │ │ │ └── README.md │ │ └── builder │ │ │ ├── multi │ │ │ ├── PersonBuilder.java │ │ │ ├── PersonJobBuilder.java │ │ │ └── PersonAddressBuilder.java │ │ │ ├── single │ │ │ └── example2 │ │ │ │ ├── CodeBuilder.java │ │ │ │ └── Field.java │ │ │ └── README.md │ ├── structural │ │ ├── README.md │ │ ├── proxy │ │ │ ├── Image.java │ │ │ ├── README.md │ │ │ ├── ProxyImage.java │ │ │ └── RealImage.java │ │ ├── decorator │ │ │ ├── example2 │ │ │ │ ├── Car.java │ │ │ │ ├── BasicCar.java │ │ │ │ ├── CarDecorator.java │ │ │ │ ├── LuxuryCar.java │ │ │ │ └── SportsCar.java │ │ │ ├── example1 │ │ │ │ ├── FarmHouse.java │ │ │ │ ├── Margherita.java │ │ │ │ ├── SimplePizza.java │ │ │ │ ├── PeppyPaneer.java │ │ │ │ ├── ChickenFiesta.java │ │ │ │ ├── ToppingsDecorator.java │ │ │ │ ├── Pizza.java │ │ │ │ ├── Paneer.java │ │ │ │ ├── Barbeque.java │ │ │ │ └── FreshTomato.java │ │ │ └── README.md │ │ ├── facade │ │ │ ├── MobileShop.java │ │ │ ├── Iphone.java │ │ │ ├── Blackberry.java │ │ │ ├── Samsung.java │ │ │ ├── README.md │ │ │ └── ShopKeeper.java │ │ ├── flyweight │ │ │ ├── Player.java │ │ │ ├── PlayerType.java │ │ │ ├── CounterTerrorist.java │ │ │ ├── Terrorist.java │ │ │ └── PlayerFactory.java │ │ ├── uml │ │ │ ├── Bridge_Design_Pattern_UML.jpg │ │ │ ├── Proxy_Design_Pattern_UML.jpg │ │ │ ├── Adapter_Design_Pattern_UML.jpg │ │ │ ├── Composite_Design_Pattern_UML.jpg │ │ │ ├── Decorator_Design_Pattern_UML.jpg │ │ │ └── Flyweight_Design_Pattern_UML.jpg │ │ ├── adapter │ │ │ ├── example2 │ │ │ │ ├── Socket.java │ │ │ │ ├── SocketAdapter.java │ │ │ │ ├── Volt.java │ │ │ │ └── SocketObjectAdapterImpl.java │ │ │ ├── example1 │ │ │ │ ├── GeometricShape.java │ │ │ │ ├── Shape.java │ │ │ │ ├── Circle.java │ │ │ │ ├── Rectangle.java │ │ │ │ ├── Rhombus.java │ │ │ │ └── Drawing.java │ │ │ └── README.md │ │ ├── bridge │ │ │ ├── example1 │ │ │ │ ├── Color.java │ │ │ │ ├── Blue.java │ │ │ │ ├── Red.java │ │ │ │ ├── Shape.java │ │ │ │ ├── Triangle.java │ │ │ │ └── Rectangle.java │ │ │ ├── example2 │ │ │ │ ├── Workshop.java │ │ │ │ ├── Bus.java │ │ │ │ ├── Car.java │ │ │ │ ├── Bike.java │ │ │ │ ├── PaintWorkShop.java │ │ │ │ ├── TestWorkShop.java │ │ │ │ ├── ProduceWorkShop.java │ │ │ │ ├── RepairWorkShop.java │ │ │ │ ├── AssembleWorkShop.java │ │ │ │ └── Vehicle.java │ │ │ └── README.md │ │ └── composite │ │ │ ├── example2 │ │ │ ├── Employee.java │ │ │ ├── Manager.java │ │ │ ├── Developer.java │ │ │ └── Director.java │ │ │ ├── example1 │ │ │ ├── Department.java │ │ │ ├── SalesDepartment.java │ │ │ ├── FinanceDepartment.java │ │ │ └── HeadDepartment.java │ │ │ └── README.md │ ├── behavioral │ │ ├── iterator │ │ │ ├── List.java │ │ │ ├── Iterator.java │ │ │ ├── Topic.java │ │ │ ├── TopicList.java │ │ │ ├── README.md │ │ │ └── TopicIterator.java │ │ ├── command │ │ │ ├── example1 │ │ │ │ ├── Order.java │ │ │ │ ├── BuyStock.java │ │ │ │ ├── SellStock.java │ │ │ │ ├── Stock.java │ │ │ │ └── Broker.java │ │ │ ├── example2 │ │ │ │ ├── Command.java │ │ │ │ ├── RemoteControl.java │ │ │ │ ├── Light.java │ │ │ │ ├── LightOffCommand.java │ │ │ │ └── LightOnCommand.java │ │ │ └── README.md │ │ ├── observer │ │ │ ├── example1 │ │ │ │ ├── Channel.java │ │ │ │ ├── NewsChannel.java │ │ │ │ └── NewsAgency.java │ │ │ └── example2 │ │ │ │ ├── Observer.java │ │ │ │ ├── OctalObserver.java │ │ │ │ ├── BinaryObserver.java │ │ │ │ ├── HexaObserver.java │ │ │ │ └── Subject.java │ │ ├── uml │ │ │ ├── State_Design_Pattern_UML.jpg │ │ │ ├── Command_Design_Pattern_UML.jpg │ │ │ ├── Iterator_Design_Pattern_UML.jpg │ │ │ ├── Mediator_Design_Pattern_UML.jpg │ │ │ ├── Memento_Design_Pattern_UML.jpg │ │ │ ├── Observer_Design_Pattern_UML.jpg │ │ │ ├── Strategy_Design_Pattern_UML.jpg │ │ │ ├── Visitor_Design_Pattern_UML.jpg │ │ │ ├── Template_Method_Design_Pattern_UML.jpg │ │ │ └── Chain_of_Responsibility_Design_Pattern_UML.jpg │ │ ├── strategy │ │ │ ├── ISocialMediaStrategy.java │ │ │ ├── FacebookStrategy.java │ │ │ ├── TwitterStrategy.java │ │ │ ├── GooglePlusStrategy.java │ │ │ ├── OrkutStrategy.java │ │ │ ├── SocialMediaContext.java │ │ │ └── README.md │ │ ├── state │ │ │ ├── example1 │ │ │ │ ├── PackageState.java │ │ │ │ ├── Delivered.java │ │ │ │ ├── Shipped.java │ │ │ │ ├── Acknowledged.java │ │ │ │ ├── InTransition.java │ │ │ │ ├── OutForDelivery.java │ │ │ │ └── DeliveryContext.java │ │ │ └── example2 │ │ │ │ ├── PackageState.java │ │ │ │ ├── DeliveredState.java │ │ │ │ ├── ReceivedState.java │ │ │ │ ├── OrderedState.java │ │ │ │ └── Package.java │ │ ├── visitor │ │ │ ├── example2 │ │ │ │ ├── ItemElement.java │ │ │ │ ├── ShoppingCartVisitor.java │ │ │ │ ├── Book.java │ │ │ │ ├── Fruit.java │ │ │ │ └── ShoppingCartVisitorImpl.java │ │ │ ├── example1 │ │ │ │ ├── ComputerPart.java │ │ │ │ ├── Mouse.java │ │ │ │ ├── Monitor.java │ │ │ │ ├── Keyboard.java │ │ │ │ ├── ComputerPartVisitor.java │ │ │ │ ├── Computer.java │ │ │ │ └── ComputerPartDisplayVisitor.java │ │ │ └── README.md │ │ ├── README.md │ │ ├── mediator │ │ │ ├── ChatMediator.java │ │ │ ├── User.java │ │ │ ├── UserImpl.java │ │ │ ├── ChatMediatorImpl.java │ │ │ └── README.md │ │ ├── memento │ │ │ ├── Memento.java │ │ │ ├── CareTaker.java │ │ │ ├── README.md │ │ │ └── Originator.java │ │ ├── chainofresponsibility │ │ │ ├── DispenseChain.java │ │ │ ├── Currency.java │ │ │ ├── Dollar10Dispenser.java │ │ │ ├── Dollar20Dispenser.java │ │ │ └── Dollar50Dispenser.java │ │ └── templatemethod │ │ │ ├── README.md │ │ │ ├── StoreOrder.java │ │ │ ├── NetOrder.java │ │ │ └── OrderProcessTemplate.java │ └── README.md │ └── testing │ ├── tddbdd │ ├── example1 │ │ ├── Operator.java │ │ ├── Operation.java │ │ ├── Addition.java │ │ ├── Subtraction.java │ │ ├── Calculator.java │ │ └── OperationFactory.java │ └── example2 │ │ └── FizzBuzz.java │ └── principles │ ├── example1 │ ├── Adder.java │ ├── Multiplier.java │ └── Calculator.java │ ├── example4 │ ├── CalculatorV1.java │ ├── README.md │ ├── CalculatorV2.java │ └── Division.java │ └── example3 │ ├── DateTimeService.java │ └── DateTimeServiceV2.java ├── person.ser ├── singleton.ser └── enum-singleton.ser /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | clean-code.iml 3 | target -------------------------------------------------------------------------------- /todos.txt: -------------------------------------------------------------------------------- 1 | Learn clean code 2 | Learn Driving 3 | -------------------------------------------------------------------------------- /src/test/resources/dummy/file1.txt: -------------------------------------------------------------------------------- 1 | I love my india -------------------------------------------------------------------------------- /src/test/resources/dummy/file2.txt: -------------------------------------------------------------------------------- 1 | I love my india -------------------------------------------------------------------------------- /src/test/resources/dummy/file3.txt: -------------------------------------------------------------------------------- 1 | I love my india -------------------------------------------------------------------------------- /src/test/resources/dummy/another/file4.txt: -------------------------------------------------------------------------------- 1 | I love my india -------------------------------------------------------------------------------- /person.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/person.ser -------------------------------------------------------------------------------- /singleton.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/singleton.ser -------------------------------------------------------------------------------- /enum-singleton.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/enum-singleton.ser -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/dry/README.md: -------------------------------------------------------------------------------- 1 | # Don't Repeat Yourself (DRY) 2 | 3 | Don't write same code again and again -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/isp/README.md: -------------------------------------------------------------------------------- 1 | # Interface Segregation Principle (ISP) 2 | 3 | Client should not force to implement interface. -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/README.md: -------------------------------------------------------------------------------- 1 | # Creational Patterns 2 | - Singleton 3 | - Factory 4 | - Abstract Factory 5 | - Builder 6 | - Prototype 7 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/yagni/README.md: -------------------------------------------------------------------------------- 1 | # You aren't gonna need it (YAGNI) 2 | 3 | Programmer should not add functionality until it is deemed necessary. -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/README.md: -------------------------------------------------------------------------------- 1 | ## Structural Patterns 2 | - Decorator 3 | - Adapter 4 | - Bridge 5 | - Composite 6 | - Proxy 7 | - Flyweight 8 | - Facade -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/proxy/Image.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.proxy; 2 | 3 | public interface Image { 4 | void display(); 5 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/ocp/README.md: -------------------------------------------------------------------------------- 1 | # Open Close Principle (OCP) 2 | 3 | An Object or Entity should be open for extension but close for modification. 4 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/lsp/README.md: -------------------------------------------------------------------------------- 1 | # Liskove Subsitution Principle (LSP) 2 | 3 | Every derived/child class should be replaceable with base/parent class. 4 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/iterator/List.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.iterator; 2 | 3 | public interface List { 4 | Iterator iterator(); 5 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/delegation/example1/README.md: -------------------------------------------------------------------------------- 1 | # Delegation Principle 2 | 3 | Delegation means hand over the responsibility for a particular task to another class or method. -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/command/example1/Order.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.command.example1; 2 | 3 | public interface Order { 4 | void execute(); 5 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/command/example2/Command.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.command.example2; 2 | 3 | public interface Command { 4 | void execute(); 5 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/decorator/example2/Car.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.decorator.example2; 2 | 3 | public interface Car { 4 | 5 | void assemble(); 6 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/observer/example1/Channel.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.observer.example1; 2 | 3 | public interface Channel { 4 | void update(Object o); 5 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/facade/MobileShop.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.facade; 2 | 3 | public interface MobileShop { 4 | void modelNo(); 5 | 6 | void price(); 7 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/uml/State_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/behavioral/uml/State_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/flyweight/Player.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.flyweight; 2 | 3 | interface Player { 4 | void assignWeapon(String weapon); 5 | 6 | void mission(); 7 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/uml/Bridge_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/structural/uml/Bridge_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/uml/Proxy_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/structural/uml/Proxy_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/strategy/ISocialMediaStrategy.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.strategy; 2 | 3 | public interface ISocialMediaStrategy { 4 | void connectTo(String friendName); 5 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/uml/Command_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/behavioral/uml/Command_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/uml/Iterator_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/behavioral/uml/Iterator_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/uml/Mediator_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/behavioral/uml/Mediator_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/uml/Memento_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/behavioral/uml/Memento_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/uml/Observer_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/behavioral/uml/Observer_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/uml/Strategy_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/behavioral/uml/Strategy_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/uml/Visitor_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/behavioral/uml/Visitor_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/uml/Builder_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/creational/uml/Builder_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/uml/Adapter_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/structural/uml/Adapter_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/uml/Prototype_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/creational/uml/Prototype_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/uml/singleton_pattern_uml_diagram.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/creational/uml/singleton_pattern_uml_diagram.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/uml/Composite_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/structural/uml/Composite_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/uml/Decorator_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/structural/uml/Decorator_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/uml/Flyweight_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/structural/uml/Flyweight_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/srp/README.md: -------------------------------------------------------------------------------- 1 | # Single Responsibility Principle (SRP) 2 | 3 | A Class or Module should have one and only one reason to change. It means a Class or Module should have only one responsibility. 4 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/state/example1/PackageState.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.state.example1; 2 | 3 | public interface PackageState 4 | { 5 | public void updateState(DeliveryContext ctx); 6 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/visitor/example2/ItemElement.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.visitor.example2; 2 | 3 | public interface ItemElement { 4 | 5 | int accept(ShoppingCartVisitor visitor); 6 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/uml/Template_Method_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/behavioral/uml/Template_Method_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/abstractfactory/README.md: -------------------------------------------------------------------------------- 1 | # Abstract Factory Pattern 2 | 3 | Abstract Factory patterns work around a super-factory which creates other factories. This factory is also called as factory of factories. -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/README.md: -------------------------------------------------------------------------------- 1 | # Behavioral Patterns 2 | - Chain of Responsibility 3 | - Iterator 4 | - Command 5 | - Mediator 6 | - Memento 7 | - Observer 8 | - State 9 | - Strategy 10 | - Template Method 11 | - Visitor -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/visitor/example1/ComputerPart.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.visitor.example1; 2 | 3 | public interface ComputerPart { 4 | void accept(ComputerPartVisitor computerPartVisitor); 5 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/adapter/example2/Socket.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.adapter.example2; 2 | 3 | public class Socket { 4 | 5 | public Volt getVolt() { 6 | return new Volt(120); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/uml/Chain_of_Responsibility_Design_Pattern_UML.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javamultiplex/clean-code-java/HEAD/src/main/java/com/javamultiplex/pattern/behavioral/uml/Chain_of_Responsibility_Design_Pattern_UML.jpg -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/mediator/ChatMediator.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.mediator; 2 | 3 | public interface ChatMediator { 4 | 5 | void sendMessage(String msg, User user); 6 | 7 | void addUser(User user); 8 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/observer/example2/Observer.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.observer.example2; 2 | 3 | public abstract class Observer { 4 | protected Subject subject; 5 | public abstract void update(); 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/visitor/example2/ShoppingCartVisitor.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.visitor.example2; 2 | 3 | public interface ShoppingCartVisitor { 4 | 5 | int visit(Book book); 6 | 7 | int visit(Fruit fruit); 8 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/state/example2/PackageState.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.state.example2; 2 | 3 | public interface PackageState { 4 | 5 | void next(Package pkg); 6 | void prev(Package pkg); 7 | void printStatus(); 8 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/adapter/example1/GeometricShape.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.adapter.example1; 2 | 3 | public interface GeometricShape { 4 | double area(); 5 | 6 | double perimeter(); 7 | 8 | void drawShape(); 9 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/ocp/Color.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.ocp; 2 | 3 | /** 4 | * @author Rohit Agarwal on 16/08/20 2:19 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public enum Color { 8 | GREEN, BLUE, RED 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/kiss/README.md: -------------------------------------------------------------------------------- 1 | # Keep it Simple, Stupid (KISS) 2 | 3 | The KISS principle states that most systems work best if they are kept simple rather than made complicated so 4 | Keep your code as simple as possible, don't over complicate it. 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/dip/README.md: -------------------------------------------------------------------------------- 1 | # Dependency Inversion Principle 2 | 3 | - High-level modules should not depend on low-level modules. Both should depend on abstractions. 4 | 5 | - Abstractions should not depend on details. Details should depend on abstractions. -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/ocp/Size.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.ocp; 2 | 3 | /** 4 | * @author Rohit Agarwal on 16/08/20 2:19 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public enum Size { 8 | SMALL, MEDIUM, LARGE 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/adapter/example2/SocketAdapter.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.adapter.example2; 2 | 3 | public interface SocketAdapter { 4 | 5 | Volt get120Volt(); 6 | 7 | Volt get12Volt(); 8 | 9 | Volt get3Volt(); 10 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/adapter/example1/Shape.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.adapter.example1; 2 | 3 | public interface Shape { 4 | void draw(); 5 | 6 | void resize(); 7 | 8 | String description(); 9 | 10 | boolean isHide(); 11 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/README.md: -------------------------------------------------------------------------------- 1 | # SOLID Design Priciples in Java 2 | 3 | - Single Responsibility Principle (SRP) 4 | - Open Close Principle (OCP) 5 | - Liskove Substitution Principle (LSP) 6 | - Interface Segregation Principle (ISP) 7 | - Dependency Inversion Principle (DIP) 8 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/lsp/good/Shape.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.lsp.good; 2 | 3 | /** 4 | * @author Rohit Agarwal on 19/08/20 9:04 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface Shape { 8 | int calculateArea(); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/testing/tddbdd/example1/Operator.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.tddbdd.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 22/08/20 8:22 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public enum Operator { 8 | ADD, SUB, DIV, MUL 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/dip/good/Developer.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.dip.good; 2 | 3 | /** 4 | * @author Rohit Agarwal on 20/08/20 8:22 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface Developer { 8 | void develop(); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/yagni/SortAlgorithm.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.yagni; 2 | 3 | /** 4 | * @author Rohit Agarwal on 20/08/20 11:10 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface SortAlgorithm { 8 | int[] sort(int[] numbers); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/example1/Color.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 30/08/20 8:41 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface Color { 8 | String fill(); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/abstractfactory/Color.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.abstractfactory; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 8:41 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface Color { 8 | String getColor(); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/abstractfactory/Shape.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.abstractfactory; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 8:45 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface Shape { 8 | String getShape(); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/multiton/SubSystem.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.multiton; 2 | 3 | /** 4 | * @author Rohit Agarwal on 29/08/20 11:55 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public enum SubSystem { 8 | PRIMARY, AUXILIARY, FALLBACK 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/decorator/example2/BasicCar.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.decorator.example2; 2 | 3 | public class BasicCar implements Car { 4 | 5 | @Override 6 | public void assemble() { 7 | System.out.print("Basic Car."); 8 | } 9 | 10 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/testing/tddbdd/example1/Operation.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.tddbdd.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 22/08/20 8:24 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface Operation { 8 | int operate(int num1, int num2); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/flyweight/PlayerType.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.flyweight; 2 | 3 | /** 4 | * @author Rohit Agarwal on 31/08/20 11:31 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public enum PlayerType { 8 | TERRORIST, COUNTER_TERRORIST 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/delegation/example1/TravelBooking.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.delegation.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 21/08/20 11:55 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface TravelBooking { 8 | void bookTicket(); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/delegation/example2/Printer.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.delegation.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 22/08/20 1:19 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface Printer { 8 | void print(final String message); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/ocp/good/Specification.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.ocp.good; 2 | 3 | /** 4 | * @author Rohit Agarwal on 16/08/20 2:36 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface Specification { 8 | boolean isSatisfied(T item); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/visitor/example1/Mouse.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.visitor.example1; 2 | 3 | public class Mouse implements ComputerPart { 4 | 5 | @Override 6 | public void accept(ComputerPartVisitor computerPartVisitor) { 7 | computerPartVisitor.visit(this); 8 | } 9 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/factory/example2/Notification.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.factory.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 12:15 am 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface Notification { 8 | void notifyUser(); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/visitor/example1/Monitor.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.visitor.example1; 2 | 3 | public class Monitor implements ComputerPart { 4 | 5 | @Override 6 | public void accept(ComputerPartVisitor computerPartVisitor) { 7 | computerPartVisitor.visit(this); 8 | } 9 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/composite/example2/Employee.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.composite.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 01/09/20 9:26 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface Employee { 8 | void showEmployeeDetails(); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/visitor/example1/Keyboard.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.visitor.example1; 2 | 3 | public class Keyboard implements ComputerPart { 4 | 5 | @Override 6 | public void accept(ComputerPartVisitor computerPartVisitor) { 7 | computerPartVisitor.visit(this); 8 | } 9 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/factory/example2/NotificationChannel.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.factory.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 12:21 am 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public enum NotificationChannel { 8 | SMS, EMAIL, PUSH 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/composite/example1/Department.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.composite.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 01/09/20 9:10 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface Department { 8 | void printDepartmentName(); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/abstractfactory/AbstractFactory.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.abstractfactory; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 8:48 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface AbstractFactory { 8 | 9 | T create(String type); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/example2/Workshop.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 30/08/20 9:09 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public abstract class Workshop { 8 | public abstract void work(Vehicle vehicle); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/decorator/example1/FarmHouse.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.decorator.example1; 2 | 3 | public class FarmHouse extends Pizza { 4 | public FarmHouse() { 5 | description = "FarmHouse"; 6 | } 7 | 8 | public int getCost() { 9 | return 200; 10 | } 11 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/testing/principles/example1/Adder.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.principles.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 04/09/20 10:08 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface Adder { 8 | T zero(); 9 | T add(T number1, T number2); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/strategy/FacebookStrategy.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.strategy; 2 | 3 | public class FacebookStrategy implements ISocialMediaStrategy { 4 | 5 | public void connectTo(String friendName) { 6 | System.out.println("Connecting with " + friendName + " through Facebook"); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/strategy/TwitterStrategy.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.strategy; 2 | 3 | public class TwitterStrategy implements ISocialMediaStrategy { 4 | 5 | public void connectTo(String friendName) { 6 | System.out.println("Connecting with " + friendName + " through Twitter"); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/decorator/example1/Margherita.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.decorator.example1; 2 | 3 | public class Margherita extends Pizza { 4 | public Margherita() { 5 | description = "Margherita"; 6 | } 7 | 8 | public int getCost() { 9 | return 100; 10 | } 11 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/decorator/example1/SimplePizza.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.decorator.example1; 2 | 3 | public class SimplePizza extends Pizza { 4 | public SimplePizza() { 5 | description = "SimplePizza"; 6 | } 7 | 8 | public int getCost() { 9 | return 50; 10 | } 11 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/memento/Memento.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.memento; 2 | 3 | public class Memento { 4 | private final String state; 5 | 6 | public Memento(String state) { 7 | this.state = state; 8 | } 9 | 10 | public String getState() { 11 | return state; 12 | } 13 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/strategy/GooglePlusStrategy.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.strategy; 2 | 3 | public class GooglePlusStrategy implements ISocialMediaStrategy { 4 | 5 | public void connectTo(String friendName) { 6 | System.out.println("Connecting with " + friendName + " through GooglePlus"); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/decorator/example1/PeppyPaneer.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.decorator.example1; 2 | 3 | public class PeppyPaneer extends Pizza { 4 | public PeppyPaneer() { 5 | description = "PeppyPaneer"; 6 | } 7 | 8 | public int getCost() { 9 | return 100; 10 | } 11 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/testing/principles/example1/Multiplier.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.principles.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 04/09/20 11:25 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface Multiplier { 8 | T one(); 9 | T multiply(T number1, T number2); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/singleton/README.md: -------------------------------------------------------------------------------- 1 | # Singleton Pattern 2 | 3 | Single Instance of particular class per JVM 4 | 5 | ## UML Diagram 6 | ![Singleton pattern UML diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/creational/uml/singleton_pattern_uml_diagram.jpg) 7 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/decorator/example1/ChickenFiesta.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.decorator.example1; 2 | 3 | public class ChickenFiesta extends Pizza { 4 | public ChickenFiesta() { 5 | description = "ChickenFiesta"; 6 | } 7 | 8 | public int getCost() { 9 | return 200; 10 | } 11 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/isp/good/Fax.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.isp.good; 2 | 3 | import com.javamultiplex.principle.solid.isp.Document; 4 | 5 | /** 6 | * @author Rohit Agarwal on 19/08/20 11:48 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public interface Fax { 10 | public void fax(Document d); 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/strategy/OrkutStrategy.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.strategy; 2 | 3 | public class OrkutStrategy implements ISocialMediaStrategy { 4 | 5 | public void connectTo(String friendName) { 6 | System.out.println("Connecting with " + friendName + " through Orkut [not possible though :)]"); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/visitor/example1/ComputerPartVisitor.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.visitor.example1; 2 | 3 | public interface ComputerPartVisitor { 4 | void visit(Computer computer); 5 | 6 | void visit(Mouse mouse); 7 | 8 | void visit(Keyboard keyboard); 9 | 10 | void visit(Monitor monitor); 11 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/isp/good/Printer.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.isp.good; 2 | 3 | import com.javamultiplex.principle.solid.isp.Document; 4 | 5 | /** 6 | * @author Rohit Agarwal on 19/08/20 11:47 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public interface Printer { 10 | void print(Document document); 11 | } 12 | -------------------------------------------------------------------------------- /src/test/resources/FizzBuzz.feature: -------------------------------------------------------------------------------- 1 | Feature: FizzBuzz Game Play 2 | 3 | Scenario: Play FizzBuzz to get Fizz 4 | Given Create a FizzBuzz Game Play 5 | When I play with number 3 6 | Then The result is "Fizz" 7 | 8 | Scenario: Play FizzBuzz to get Buzz 9 | Given Create a FizzBuzz Game Play 10 | When I play with number 5 11 | Then The result is "Buzz" -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/dip/bad/BackendDeveloper.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.dip.bad; 2 | 3 | /** 4 | * @author Rohit Agarwal on 20/08/20 8:19 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class BackendDeveloper { 8 | public void writeJava() { 9 | System.out.println("I am writing JAVA code"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/isp/good/Scanner.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.isp.good; 2 | 3 | import com.javamultiplex.principle.solid.isp.Document; 4 | 5 | /** 6 | * @author Rohit Agarwal on 19/08/20 11:48 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public interface Scanner { 10 | public void scan(Document document); 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/abstractfactory/Red.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.abstractfactory; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 8:44 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Red implements Color { 8 | @Override 9 | public String getColor() { 10 | return "Red"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/iterator/Iterator.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.iterator; 2 | 3 | /** 4 | * @author Rohit Agarwal on 09/09/20 9:52 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface Iterator { 8 | E next(); 9 | 10 | boolean hasNext(); 11 | 12 | void reset(); 13 | 14 | E currentItem(); 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/abstractfactory/Black.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.abstractfactory; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 8:45 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Black implements Color { 8 | @Override 9 | public String getColor() { 10 | return "Black"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/abstractfactory/Square.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.abstractfactory; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 8:47 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Square implements Shape { 8 | @Override 9 | public String getShape() { 10 | return "Square"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/abstractfactory/White.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.abstractfactory; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 8:44 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class White implements Color { 8 | @Override 9 | public String getColor() { 10 | return "White"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/example1/Blue.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 30/08/20 8:42 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Blue implements Color { 8 | @Override 9 | public String fill() { 10 | return "Color is Blue"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/example1/Red.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 30/08/20 8:42 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Red implements Color { 8 | @Override 9 | public String fill() { 10 | return "Color is Red."; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/command/example1/BuyStock.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.command.example1; 2 | 3 | public class BuyStock implements Order { 4 | private final Stock abcStock; 5 | 6 | public BuyStock(Stock abcStock){ 7 | this.abcStock = abcStock; 8 | } 9 | 10 | public void execute() { 11 | abcStock.buy(); 12 | } 13 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/facade/Iphone.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.facade; 2 | 3 | public class Iphone implements MobileShop { 4 | @Override 5 | public void modelNo() { 6 | System.out.println(" Iphone 6 "); 7 | } 8 | 9 | @Override 10 | public void price() { 11 | System.out.println(" Rs 65000.00 "); 12 | } 13 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/dip/bad/FrontEndDeveloper.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.dip.bad; 2 | 3 | /** 4 | * @author Rohit Agarwal on 20/08/20 8:15 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class FrontEndDeveloper { 8 | public void writeJavascript() { 9 | System.out.println("I am write Javascript Code"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/testing/tddbdd/example1/Addition.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.tddbdd.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 22/08/20 8:25 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Addition implements Operation { 8 | @Override 9 | public int operate(int num1, int num2) { 10 | return num1 + num2; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/command/example1/SellStock.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.command.example1; 2 | 3 | public class SellStock implements Order { 4 | private final Stock abcStock; 5 | 6 | public SellStock(Stock abcStock){ 7 | this.abcStock = abcStock; 8 | } 9 | 10 | public void execute() { 11 | abcStock.sell(); 12 | } 13 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/abstractfactory/Circle.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.abstractfactory; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 8:46 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Circle implements Shape { 8 | 9 | @Override 10 | public String getShape() { 11 | return "Circle"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/abstractfactory/Rectangle.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.abstractfactory; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 8:46 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Rectangle implements Shape { 8 | @Override 9 | public String getShape() { 10 | return "Rectangle"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/decorator/README.md: -------------------------------------------------------------------------------- 1 | # Decorator Pattern 2 | 3 | The decorator pattern attaches additional responsibilities to an object dynamically. 4 | 5 | ## UML Diagram 6 | ![Decorator pattern UML diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/structural/uml/Decorator_Design_Pattern_UML.jpg) 7 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/testing/tddbdd/example1/Subtraction.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.tddbdd.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 22/08/20 8:25 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Subtraction implements Operation { 8 | @Override 9 | public int operate(int num1, int num2) { 10 | return num1 - num2; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/chainofresponsibility/DispenseChain.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.chainofresponsibility; 2 | 3 | /** 4 | * @author Rohit Agarwal on 02/09/20 11:17 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public interface DispenseChain { 8 | void setNextChain(DispenseChain nextChain); 9 | 10 | void dispense(Currency currency); 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/decorator/example1/ToppingsDecorator.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.decorator.example1; 2 | 3 | abstract class ToppingsDecorator extends Pizza { 4 | protected final Pizza pizza; 5 | 6 | public ToppingsDecorator(Pizza pizza) { 7 | this.pizza = pizza; 8 | } 9 | 10 | public abstract String getDescription(); 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/facade/Blackberry.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.facade; 2 | 3 | public class Blackberry implements MobileShop { 4 | @Override 5 | public void modelNo() { 6 | System.out.println(" Blackberry Z10 "); 7 | } 8 | 9 | @Override 10 | public void price() { 11 | System.out.println(" Rs 55000.00 "); 12 | } 13 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/facade/Samsung.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.facade; 2 | 3 | public class Samsung implements MobileShop { 4 | @Override 5 | public void modelNo() { 6 | System.out.println(" Samsung galaxy tab 3 "); 7 | } 8 | 9 | @Override 10 | public void price() { 11 | System.out.println(" Rs 45000.00 "); 12 | } 13 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/ocp/good/Filter.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.ocp.good; 2 | 3 | import java.util.List; 4 | import java.util.stream.Stream; 5 | 6 | /** 7 | * @author Rohit Agarwal on 16/08/20 2:38 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public interface Filter { 11 | Stream filter(List items, Specification specification); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/command/example2/RemoteControl.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.command.example2; 2 | 3 | //Invoker 4 | public class RemoteControl { 5 | private Command command; 6 | 7 | public void setCommand(Command command) { 8 | this.command = command; 9 | } 10 | 11 | public void pressButton() { 12 | command.execute(); 13 | } 14 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/decorator/example1/Pizza.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.decorator.example1; 2 | 3 | public abstract class Pizza 4 | { 5 | // it is an abstract pizza 6 | String description = "Unkknown Pizza"; 7 | 8 | public String getDescription() 9 | { 10 | return description; 11 | } 12 | 13 | public abstract int getCost(); 14 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/decorator/example2/CarDecorator.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.decorator.example2; 2 | 3 | public class CarDecorator implements Car { 4 | 5 | protected Car car; 6 | 7 | public CarDecorator(Car c) { 8 | this.car = c; 9 | } 10 | 11 | @Override 12 | public void assemble() { 13 | this.car.assemble(); 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/delegation/example1/AirBooking.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.delegation.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 21/08/20 11:57 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class AirBooking implements TravelBooking { 8 | @Override 9 | public void bookTicket() { 10 | System.out.println("Air ticket booked"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/delegation/example1/TrainBooking.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.delegation.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 21/08/20 11:56 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class TrainBooking implements TravelBooking { 8 | @Override 9 | public void bookTicket() { 10 | System.out.println("Train ticket booked"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/yagni/BubbleSort.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.yagni; 2 | 3 | /** 4 | * @author Rohit Agarwal on 20/08/20 11:13 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class BubbleSort implements SortAlgorithm { 8 | @Override 9 | public int[] sort(int[] numbers) { 10 | //TODO: Bubble Sort Implementation 11 | return new int[0]; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/iterator/Topic.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.iterator; 2 | 3 | public class Topic { 4 | private String name; 5 | 6 | public Topic(String name) { 7 | this.name = name; 8 | } 9 | 10 | public String getName() { 11 | return name; 12 | } 13 | 14 | public void setName(String name) { 15 | this.name = name; 16 | } 17 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/command/example2/Light.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.command.example2; 2 | 3 | //Receiver 4 | public class Light { 5 | private boolean on; 6 | 7 | public void switchOn() { 8 | on = true; 9 | } 10 | 11 | public void switchOff() { 12 | on = false; 13 | } 14 | 15 | public boolean isOn() { 16 | return on; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/iterator/TopicList.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.iterator; 2 | 3 | public class TopicList implements List { 4 | private final Topic[] topics; 5 | 6 | public TopicList(Topic[] topics) { 7 | this.topics = topics; 8 | } 9 | 10 | @Override 11 | public Iterator iterator() { 12 | return new TopicIterator(topics); 13 | } 14 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/decorator/example2/LuxuryCar.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.decorator.example2; 2 | 3 | public class LuxuryCar extends CarDecorator { 4 | 5 | public LuxuryCar(Car c) { 6 | super(c); 7 | } 8 | 9 | @Override 10 | public void assemble() { 11 | super.assemble(); 12 | System.out.print(" Adding features of Luxury Car."); 13 | } 14 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/decorator/example2/SportsCar.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.decorator.example2; 2 | 3 | public class SportsCar extends CarDecorator { 4 | 5 | public SportsCar(Car c) { 6 | super(c); 7 | } 8 | 9 | @Override 10 | public void assemble() { 11 | super.assemble(); 12 | System.out.print(" Adding features of Sports Car."); 13 | } 14 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/delegation/example2/HPPrinter.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.delegation.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 22/08/20 1:23 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class HPPrinter implements Printer { 8 | @Override 9 | public void print(String message) { 10 | System.out.println("HP Printer with message : " + message); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/isp/bad/Machine.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.isp.bad; 2 | 3 | import com.javamultiplex.principle.solid.isp.Document; 4 | 5 | /** 6 | * @author Rohit Agarwal on 19/08/20 11:31 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public interface Machine { 10 | void print(Document d); 11 | 12 | void scan(Document d); 13 | 14 | void fax(Document d); 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/mediator/User.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.mediator; 2 | 3 | public abstract class User { 4 | protected ChatMediator mediator; 5 | protected String name; 6 | 7 | public User(ChatMediator med, String name){ 8 | this.mediator=med; 9 | this.name=name; 10 | } 11 | 12 | public abstract void send(String msg); 13 | 14 | public abstract void receive(String msg); 15 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/strategy/SocialMediaContext.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.strategy; 2 | 3 | public class SocialMediaContext { 4 | ISocialMediaStrategy smStrategy; 5 | 6 | public SocialMediaContext(ISocialMediaStrategy smStrategy) { 7 | this.smStrategy = smStrategy; 8 | } 9 | 10 | public void connect(String name) { 11 | smStrategy.connectTo(name); 12 | } 13 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/factory/example2/SMSNotification.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.factory.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 12:16 am 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class SMSNotification implements Notification { 8 | @Override 9 | public void notifyUser() { 10 | System.out.println("Sending sms notification"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/delegation/example2/CanonPrinter.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.delegation.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 22/08/20 1:20 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class CanonPrinter implements Printer { 8 | @Override 9 | public void print(String message) { 10 | System.out.println("Canon Printer with message : " + message); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/delegation/example2/EpsonPrinter.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.delegation.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 22/08/20 1:22 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class EpsonPrinter implements Printer { 8 | @Override 9 | public void print(String message) { 10 | System.out.println("Epson Printer with message : " + message); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/iterator/README.md: -------------------------------------------------------------------------------- 1 | # Iterator Pattern 2 | 3 | An iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. 4 | 5 | ## UML Diagram 6 | ![Iterator Pattern UML Diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/behavioral/uml/Iterator_Design_Pattern_UML.jpg) 7 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/factory/example2/PushNotification.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.factory.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 12:18 am 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class PushNotification implements Notification { 8 | @Override 9 | public void notifyUser() { 10 | System.out.println("Sending push notification"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/adapter/example2/Volt.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.adapter.example2; 2 | 3 | public class Volt { 4 | 5 | private int volts; 6 | 7 | public Volt(int v) { 8 | this.volts = v; 9 | } 10 | 11 | public int getVolts() { 12 | return volts; 13 | } 14 | 15 | public void setVolts(int volts) { 16 | this.volts = volts; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/command/example2/LightOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.command.example2; 2 | 3 | //Concrete Command 4 | public class LightOffCommand implements Command { 5 | //reference to the light 6 | Light light; 7 | 8 | public LightOffCommand(Light light) { 9 | this.light = light; 10 | } 11 | 12 | public void execute() { 13 | light.switchOff(); 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/command/example2/LightOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.command.example2; 2 | 3 | //Concrete Command 4 | public class LightOnCommand implements Command { 5 | //reference to the light 6 | Light light; 7 | 8 | public LightOnCommand(Light light) { 9 | this.light = light; 10 | } 11 | 12 | public void execute() { 13 | light.switchOn(); 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/factory/example2/EmailNotification.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.factory.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 12:17 am 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class EmailNotification implements Notification { 8 | @Override 9 | public void notifyUser() { 10 | System.out.println("Sending e-mail notification"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/example1/Shape.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 30/08/20 8:44 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public abstract class Shape { 8 | protected final Color color; 9 | 10 | protected Shape(Color color) { 11 | this.color = color; 12 | } 13 | 14 | protected abstract void draw(); 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/composite/example1/SalesDepartment.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.composite.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 01/09/20 9:12 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class SalesDepartment implements Department { 8 | @Override 9 | public void printDepartmentName() { 10 | System.out.println(this.getClass().getSimpleName()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/proxy/README.md: -------------------------------------------------------------------------------- 1 | # Proxy Pattern 2 | 3 | The Proxy pattern allows us to create an intermediary that acts as an interface to another resource, while also hiding the underlying complexity of the component. 4 | 5 | ## UML Diagram 6 | ![Proxy pattern UML diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/structural/uml/Proxy_Design_Pattern_UML.jpg) 7 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/composite/example1/FinanceDepartment.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.composite.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 01/09/20 9:11 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class FinanceDepartment implements Department { 8 | @Override 9 | public void printDepartmentName() { 10 | System.out.println(this.getClass().getSimpleName()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/testing/tddbdd/example1/Calculator.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.tddbdd.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 22/08/20 5:38 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Calculator { 8 | 9 | public int calculate(final Operator operator, final int number1, final int number2) { 10 | return OperationFactory.getInstance(operator).operate(number1, number2); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/prototype/README.md: -------------------------------------------------------------------------------- 1 | # Prototype Pattern 2 | 3 | The Prototype pattern is generally used when we have an instance of the class (prototype) and we'd like to create new objects by just copying the prototype. 4 | 5 | # UML Diagram 6 | ![Prototype pattern UML diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/creational/uml/Prototype_Design_Pattern_UML.jpg) 7 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/README.md: -------------------------------------------------------------------------------- 1 | # Bridge Pattern 2 | 3 | Bridge design pattern introduced by Gang of Four (GoF) is to decouple an abstraction(hierarchy) from its implementation(hierarchy) so that the two can vary independently. 4 | 5 | ## UML Diagram 6 | ![Bridge pattern UML diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/structural/uml/Bridge_Design_Pattern_UML.jpg) 7 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/decorator/example1/Paneer.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.decorator.example1; 2 | 3 | public class Paneer extends ToppingsDecorator { 4 | public Paneer(Pizza pizza) { 5 | super(pizza); 6 | } 7 | 8 | public String getDescription() { 9 | return pizza.getDescription() + ", Paneer "; 10 | } 11 | 12 | public int getCost() { 13 | return 70 + pizza.getCost(); 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/srp/example1/good/Employee.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.srp.example1.good; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * @author Rohit Agarwal on 20/08/20 8:06 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class Employee { 10 | private String employeeId; 11 | private String name; 12 | private String address; 13 | private Date dateOfJoining; 14 | //Getters & Setters 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/srp/example1/good/HRPromotions.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.srp.example1.good; 2 | 3 | /** 4 | * @author Rohit Agarwal on 20/08/20 8:08 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class HRPromotions { 8 | public boolean isPromotionDueThisYear(Employee emp) { 9 | //TODO: promotion logic implementation using the employee information passed 10 | return false; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/memento/CareTaker.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.memento; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class CareTaker { 7 | private final List mementoList = new ArrayList<>(); 8 | 9 | public void add(Memento state) { 10 | mementoList.add(state); 11 | } 12 | 13 | public Memento get(int index) { 14 | return mementoList.get(index); 15 | } 16 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/decorator/example1/Barbeque.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.decorator.example1; 2 | 3 | public class Barbeque extends ToppingsDecorator { 4 | public Barbeque(Pizza pizza) { 5 | super(pizza); 6 | } 7 | 8 | public String getDescription() { 9 | return pizza.getDescription() + ", Barbeque "; 10 | } 11 | 12 | public int getCost() { 13 | return 90 + pizza.getCost(); 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/chainofresponsibility/Currency.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.chainofresponsibility; 2 | 3 | /** 4 | * @author Rohit Agarwal on 02/09/20 11:16 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Currency { 8 | private final int amount; 9 | 10 | public Currency(int amount) { 11 | this.amount = amount; 12 | } 13 | 14 | public int getAmount() { 15 | return amount; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/strategy/README.md: -------------------------------------------------------------------------------- 1 | # Strategy Pattern 2 | 3 | Strategy design pattern is behavioral design pattern where we choose a specific implementation of algorithm or task in run time – out of multiple other implementations for same task. 4 | 5 | ## UML Diagram 6 | 7 | ![Strategy Pattern UML Diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/behavioral/uml/Strategy_Design_Pattern_UML.jpg) 8 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/decorator/example1/FreshTomato.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.decorator.example1; 2 | 3 | public class FreshTomato extends ToppingsDecorator { 4 | public FreshTomato(Pizza pizza) { 5 | super(pizza); 6 | } 7 | 8 | public String getDescription() { 9 | return pizza.getDescription() + ", Fresh Tomato "; 10 | } 11 | 12 | public int getCost() { 13 | return 40 + pizza.getCost(); 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/dip/good/BackendDeveloper.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.dip.good; 2 | 3 | /** 4 | * @author Rohit Agarwal on 20/08/20 8:23 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class BackendDeveloper implements Developer { 8 | @Override 9 | public void develop() { 10 | writeJava(); 11 | } 12 | 13 | public void writeJava() { 14 | System.out.println("I am writing JAVA code"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/lsp/good/Square.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.lsp.good; 2 | 3 | /** 4 | * @author Rohit Agarwal on 19/08/20 9:09 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Square implements Shape { 8 | private final int size; 9 | 10 | public Square(int size) { 11 | this.size = size; 12 | } 13 | 14 | @Override 15 | public int calculateArea() { 16 | return size * size; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/srp/example1/good/FinITCalculations.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.srp.example1.good; 2 | 3 | /** 4 | * @author Rohit Agarwal on 20/08/20 8:09 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class FinITCalculations { 8 | public Double calcIncomeTaxForCurrentYear(Employee emp) { 9 | //TODO: income tax logic implementation using the employee information passed 10 | return 0d; 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/README.md: -------------------------------------------------------------------------------- 1 | # Design Patterns 2 | 3 | ## Creational 4 | - Singleton 5 | - Factory 6 | - Abstract Factory 7 | - Builder 8 | - Prototype 9 | 10 | ## Structural 11 | - Decorator 12 | - Adapter 13 | - Bridge 14 | - Composite 15 | - Proxy 16 | - Flyweight 17 | - Facade 18 | 19 | ## Behavioral 20 | - Chain of Responsibility 21 | - Iterator 22 | - Command 23 | - Mediator 24 | - Memento 25 | - Observer 26 | - State 27 | - Strategy 28 | - Template Method 29 | - Visitor 30 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/observer/example2/OctalObserver.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.observer.example2; 2 | 3 | public class OctalObserver extends Observer { 4 | 5 | public OctalObserver(Subject subject) { 6 | this.subject = subject; 7 | this.subject.attach(this); 8 | } 9 | 10 | @Override 11 | public void update() { 12 | System.out.println("Octal String: " + Integer.toOctalString(subject.getState())); 13 | } 14 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/testing/principles/example4/CalculatorV1.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.principles.example4; 2 | 3 | /** 4 | * @author Rohit Agarwal on 06/09/20 8:17 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class CalculatorV1 { 8 | 9 | public Division division(int number1, int number2) { 10 | int quotient = number1 / number2; 11 | int remainder = number1 % number2; 12 | return new Division(quotient, remainder); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/resources/Calculator.feature: -------------------------------------------------------------------------------- 1 | Feature: Create Calculator that perform Addition, Subtraction, Division and Multiplication 2 | 3 | Scenario: Perform Addition of two numbers 4 | Given You have a calculator 5 | When Operator is "ADD", first number is 3 and second number is 4 6 | Then The result is 7 7 | 8 | Scenario: Perform Subtraction of two numbers 9 | Given You have a calculator 10 | When Operator is "SUB", first number is 6 and second number is 4 11 | Then The result is 2 -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/observer/example2/BinaryObserver.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.observer.example2; 2 | 3 | public class BinaryObserver extends Observer { 4 | 5 | public BinaryObserver(Subject subject) { 6 | this.subject = subject; 7 | this.subject.attach(this); 8 | } 9 | 10 | @Override 11 | public void update() { 12 | System.out.println("Binary String: " + Integer.toBinaryString(subject.getState())); 13 | } 14 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/observer/example2/HexaObserver.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.observer.example2; 2 | 3 | public class HexaObserver extends Observer { 4 | 5 | public HexaObserver(Subject subject) { 6 | this.subject = subject; 7 | this.subject.attach(this); 8 | } 9 | 10 | @Override 11 | public void update() { 12 | System.out.println("Hex String: " + Integer.toHexString(subject.getState()).toUpperCase()); 13 | } 14 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/visitor/README.md: -------------------------------------------------------------------------------- 1 | # Visitor Pattern 2 | 3 | Visitor pattern is used when we have to perform an operation on a group of similar kind of Objects. With the help of visitor pattern, we can move the operational logic from the objects to another class. 4 | 5 | ## UML Diagram 6 | ![Visitor Pattern UML Diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/behavioral/uml/Visitor_Design_Pattern_UML.jpg) 7 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/isp/good/OldGenerationPrinter.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.isp.good; 2 | 3 | import com.javamultiplex.principle.solid.isp.Document; 4 | 5 | /** 6 | * @author Rohit Agarwal on 19/08/20 11:37 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class OldGenerationPrinter implements Printer { 10 | 11 | @Override 12 | public void print(Document d) { 13 | System.out.println("Printing Document : " + d); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/example1/Triangle.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 30/08/20 8:47 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Triangle extends Shape { 8 | protected Triangle(Color color) { 9 | super(color); 10 | } 11 | 12 | @Override 13 | protected void draw() { 14 | System.out.println("Draw Triangle : " + color.fill()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/command/example1/Stock.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.command.example1; 2 | 3 | public class Stock { 4 | 5 | private final String name = "ABC"; 6 | private final int quantity = 10; 7 | 8 | public void buy(){ 9 | System.out.println("Stock [ Name: "+name+", Quantity: " + quantity +" ] bought"); 10 | } 11 | public void sell(){ 12 | System.out.println("Stock [ Name: "+name+", Quantity: " + quantity +" ] sold"); 13 | } 14 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/memento/README.md: -------------------------------------------------------------------------------- 1 | # Memento Pattern 2 | 3 | Memento pattern is used to restore state of an object to a previous state. As your application is progressing, you may want to save checkpoints in your application and restore back to those checkpoints later. 4 | 5 | ## UML Diagram 6 | ![Memento Pattern UML Diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/behavioral/uml/Memento_Design_Pattern_UML.jpg) 7 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/observer/example1/NewsChannel.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.observer.example1; 2 | 3 | public class NewsChannel implements Channel { 4 | private String news; 5 | 6 | public String getNews() { 7 | return news; 8 | } 9 | 10 | public void setNews(String news) { 11 | this.news = news; 12 | } 13 | 14 | @Override 15 | public void update(Object news) { 16 | this.setNews((String) news); 17 | } 18 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/example1/Rectangle.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 30/08/20 8:45 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Rectangle extends Shape { 8 | protected Rectangle(Color color) { 9 | super(color); 10 | } 11 | 12 | @Override 13 | protected void draw() { 14 | System.out.println("Draw Rectangle : " + color.fill()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/dip/good/FrontendDeveloper.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.dip.good; 2 | 3 | /** 4 | * @author Rohit Agarwal on 20/08/20 8:25 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class FrontendDeveloper implements Developer { 8 | @Override 9 | public void develop() { 10 | writeJavascript(); 11 | } 12 | 13 | public void writeJavascript() { 14 | System.out.println("I am write Javascript Code"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/example2/Bus.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example2; 2 | 3 | public class Bus extends Vehicle { 4 | @Override 5 | public void manufacture() { 6 | System.out.println("Manufacturing Bus"); 7 | workshops.forEach(workshop -> workshop.work(this)); 8 | System.out.println("Done."); 9 | System.out.println(); 10 | } 11 | 12 | @Override 13 | public int minWorkTime() { 14 | return 20; 15 | } 16 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/example2/Car.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example2; 2 | 3 | public class Car extends Vehicle { 4 | @Override 5 | public void manufacture() { 6 | System.out.println("Manufacturing Car"); 7 | workshops.forEach(workshop -> workshop.work(this)); 8 | System.out.println("Done."); 9 | System.out.println(); 10 | } 11 | 12 | @Override 13 | public int minWorkTime() { 14 | return 10; 15 | } 16 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/example2/Bike.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example2; 2 | 3 | public class Bike extends Vehicle { 4 | @Override 5 | public void manufacture() { 6 | System.out.println("Manufacturing Bike..."); 7 | workshops.forEach(workshop -> workshop.work(this)); 8 | System.out.println("Done."); 9 | System.out.println(); 10 | } 11 | 12 | @Override 13 | public int minWorkTime() { 14 | return 5; 15 | } 16 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/testing/principles/example4/README.md: -------------------------------------------------------------------------------- 1 | # Remainder with negative numbers 2 | 3 | The remainder operator can be used with negative integers. The rule is: 4 | 5 | - Perform the operation as if both operands were positive. 6 | - If the left operand is negative, then make the result negative. 7 | - If the left operand is positive, then make the result positive. 8 | - Ignore the sign of the right operand in all cases. 9 | 10 | For example: 11 | 12 | 17 % 3 == 2 -17 % 3 == -2 13 | 17 % -3 == 2 -17 % -3 == -2 -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/behavioral/visitor/example1/VisitorClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.visitor.example1; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | /** 6 | * @author Rohit Agarwal on 10/09/20 11:47 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class VisitorClient { 10 | 11 | @Test 12 | public void shouldDisplayComputerParts(){ 13 | ComputerPart computerPart=new Computer(); 14 | computerPart.accept(new ComputerPartDisplayVisitor()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/proxy/ProxyImage.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.proxy; 2 | 3 | public class ProxyImage implements Image { 4 | 5 | private RealImage realImage; 6 | private final String fileName; 7 | 8 | public ProxyImage(String fileName) { 9 | this.fileName = fileName; 10 | } 11 | 12 | @Override 13 | public void display() { 14 | if (realImage == null) { 15 | realImage = new RealImage(fileName); 16 | } 17 | realImage.display(); 18 | } 19 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/dip/good/Project.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.dip.good; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * @author Rohit Agarwal on 20/08/20 8:26 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class Project { 10 | private final List developers; 11 | 12 | public Project(List developers) { 13 | this.developers = developers; 14 | } 15 | 16 | public void implement() { 17 | developers.forEach(Developer::develop); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/command/example1/Broker.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.command.example1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Broker { 7 | private final List orderList = new ArrayList<>(); 8 | 9 | public void takeOrder(Order order){ 10 | orderList.add(order); 11 | } 12 | 13 | public void placeOrders(){ 14 | 15 | for (Order order : orderList) { 16 | order.execute(); 17 | } 18 | orderList.clear(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/templatemethod/README.md: -------------------------------------------------------------------------------- 1 | # Template Method Pattern 2 | 3 | Template method design pattern is to define an algorithm as a skeleton of operations and leave the details to be implemented by the child classes. The overall structure and sequence of the algorithm is preserved by the parent class. 4 | 5 | ## UML Diagram 6 | ![Template method pattern UML Diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/behavioral/uml/Template_Method_Design_Pattern_UML.jpg) 7 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/delegation/example2/PrinterController.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.delegation.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 22/08/20 1:23 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class PrinterController implements Printer { 8 | private final Printer printer; 9 | 10 | public PrinterController(Printer printer) { 11 | this.printer = printer; 12 | } 13 | 14 | @Override 15 | public void print(String message) { 16 | printer.print(message); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/lsp/bad/Square.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.lsp.bad; 2 | 3 | /** 4 | * @author Rohit Agarwal on 18/08/20 11:49 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Square extends Rectangle { 8 | 9 | @Override 10 | public void setHeight(int height) { 11 | super.setWidth(height); 12 | super.setHeight(height); 13 | } 14 | 15 | @Override 16 | public void setWidth(int width) { 17 | super.setHeight(width); 18 | super.setWidth(width); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/memento/Originator.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.memento; 2 | 3 | public class Originator { 4 | private String state; 5 | 6 | public void setState(String state) { 7 | this.state = state; 8 | } 9 | 10 | public String getState() { 11 | return state; 12 | } 13 | 14 | public Memento saveStateToMemento() { 15 | return new Memento(state); 16 | } 17 | 18 | public void getStateFromMemento(Memento memento) { 19 | state = memento.getState(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/delegation/example1/TravelAgent.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.delegation.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 21/08/20 11:58 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class TravelAgent implements TravelBooking { 8 | 9 | private final TravelBooking travelBooking; 10 | 11 | public TravelAgent(TravelBooking travelBooking) { 12 | this.travelBooking = travelBooking; 13 | } 14 | 15 | @Override 16 | public void bookTicket() { 17 | travelBooking.bookTicket(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/state/example1/Delivered.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.state.example1; 2 | 3 | public class Delivered implements PackageState 4 | { 5 | //Singleton 6 | private static Delivered instance = new Delivered(); 7 | 8 | private Delivered() {} 9 | 10 | public static Delivered instance() { 11 | return instance; 12 | } 13 | 14 | //Business logic 15 | @Override 16 | public void updateState(DeliveryContext ctx) 17 | { 18 | System.out.println("Package is delivered!!"); 19 | } 20 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/proxy/RealImage.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.proxy; 2 | 3 | public class RealImage implements Image { 4 | 5 | private final String fileName; 6 | 7 | public RealImage(String fileName) { 8 | this.fileName = fileName; 9 | loadFromDisk(fileName); 10 | } 11 | 12 | @Override 13 | public void display() { 14 | System.out.println("Displaying " + fileName); 15 | } 16 | 17 | private void loadFromDisk(String fileName) { 18 | System.out.println("Loading " + fileName); 19 | } 20 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/lsp/good/Rectangle.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.lsp.good; 2 | 3 | /** 4 | * @author Rohit Agarwal on 19/08/20 9:07 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Rectangle implements Shape { 8 | 9 | private final int width; 10 | private final int height; 11 | 12 | public Rectangle(int width, int height) { 13 | this.width = width; 14 | this.height = height; 15 | } 16 | 17 | @Override 18 | public int calculateArea() { 19 | return width * height; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/singleton/InnerStaticSingleton.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.singleton; 2 | 3 | /** 4 | * @author Rohit Agarwal on 29/08/20 9:29 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class InnerStaticSingleton { 8 | private InnerStaticSingleton() { 9 | } 10 | 11 | public static InnerStaticSingleton getInstance() { 12 | return Impl.INSTANCE; 13 | } 14 | 15 | private static class Impl { 16 | private static final InnerStaticSingleton INSTANCE = new InnerStaticSingleton(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/state/example2/DeliveredState.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.state.example2; 2 | 3 | public class DeliveredState implements PackageState { 4 | 5 | @Override 6 | public void next(Package pkg) { 7 | pkg.setState(new ReceivedState()); 8 | } 9 | 10 | @Override 11 | public void prev(Package pkg) { 12 | pkg.setState(new OrderedState()); 13 | } 14 | 15 | @Override 16 | public void printStatus() { 17 | System.out.println("Package delivered to post office, not received yet."); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/mediator/UserImpl.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.mediator; 2 | 3 | public class UserImpl extends User { 4 | 5 | public UserImpl(ChatMediator med, String name) { 6 | super(med, name); 7 | } 8 | 9 | @Override 10 | public void send(String msg) { 11 | System.out.println(this.name + ": Sending Message=" + msg); 12 | mediator.sendMessage(msg, this); 13 | } 14 | 15 | @Override 16 | public void receive(String msg) { 17 | System.out.println(this.name + ": Received Message:" + msg); 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/adapter/example1/Circle.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.adapter.example1; 2 | 3 | public class Circle implements Shape { 4 | @Override 5 | public void draw() { 6 | System.out.println("Drawing Circle"); 7 | } 8 | 9 | @Override 10 | public void resize() { 11 | System.out.println("Resizing Circle"); 12 | } 13 | 14 | @Override 15 | public String description() { 16 | return "Circle object"; 17 | } 18 | 19 | @Override 20 | public boolean isHide() { 21 | return false; 22 | } 23 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/example2/PaintWorkShop.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example2; 2 | 3 | public class PaintWorkShop extends Workshop { 4 | @Override 5 | public void work(Vehicle vehicle) { 6 | System.out.print("Painting... "); 7 | long timeToTake = 100 * vehicle.minWorkTime(); 8 | try { 9 | Thread.sleep(timeToTake); 10 | } catch (InterruptedException exp) { 11 | // nothing to do for now. 12 | } 13 | System.out.printf("(Time taken: %d millis), Done.\n", timeToTake); 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/example2/TestWorkShop.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example2; 2 | 3 | public class TestWorkShop extends Workshop { 4 | @Override 5 | public void work(Vehicle vehicle) { 6 | System.out.print("Testing... "); 7 | long timeToTake = 50 * vehicle.minWorkTime(); 8 | try { 9 | Thread.sleep(timeToTake); 10 | } catch (InterruptedException exp) { 11 | // nothing to do for now. 12 | } 13 | System.out.printf("(Time taken: %d millis), Done.\n", timeToTake); 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/state/example2/ReceivedState.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.state.example2; 2 | 3 | public class ReceivedState implements PackageState { 4 | 5 | @Override 6 | public void next(Package pkg) { 7 | System.out.println("This package is already received by a client."); 8 | } 9 | 10 | @Override 11 | public void prev(Package pkg) { 12 | pkg.setState(new DeliveredState()); 13 | } 14 | 15 | @Override 16 | public void printStatus() { 17 | System.out.println("Package is received by customer"); 18 | } 19 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/templatemethod/StoreOrder.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.templatemethod; 2 | 3 | class StoreOrder extends OrderProcessTemplate { 4 | 5 | @Override 6 | public void doSelect() { 7 | System.out.println("Customer chooses the item from shelf."); 8 | } 9 | 10 | @Override 11 | public void doPayment() { 12 | System.out.println("Pays at counter through cash/POS"); 13 | } 14 | 15 | @Override 16 | public void doDelivery() { 17 | System.out.println("Item delivered to in delivery counter."); 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/example2/ProduceWorkShop.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example2; 2 | 3 | public class ProduceWorkShop extends Workshop { 4 | @Override 5 | public void work(Vehicle vehicle) { 6 | System.out.print("Producing... "); 7 | long timeToTake = 300 * vehicle.minWorkTime(); 8 | try { 9 | Thread.sleep(timeToTake); 10 | } catch (InterruptedException exp) { 11 | // nothing to do for now. 12 | } 13 | System.out.printf("(Time taken: %d millis), Done.\n", timeToTake); 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/example2/RepairWorkShop.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example2; 2 | 3 | public class RepairWorkShop extends Workshop { 4 | @Override 5 | public void work(Vehicle vehicle) { 6 | System.out.print("Repairing... "); 7 | long timeToTake = 150 * vehicle.minWorkTime(); 8 | try { 9 | Thread.sleep(timeToTake); 10 | } catch (InterruptedException exp) { 11 | // nothing to do for now. 12 | } 13 | System.out.printf("(Time taken: %d millis), Done.\n", timeToTake); 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/command/README.md: -------------------------------------------------------------------------------- 1 | # Command Pattern 2 | 3 | Command pattern is a data driven design pattern and falls under behavioral pattern category. A request is wrapped under an object as command and passed to invoker object. Invoker object looks for the appropriate object which can handle this command and passes the command to the corresponding object which executes the command. 4 | 5 | ## UML Diagram 6 | ![Command Pattern UML Diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/behavioral/uml/Command_Design_Pattern_UML.jpg) 7 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/state/example2/OrderedState.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.state.example2; 2 | 3 | public class OrderedState implements PackageState { 4 | 5 | @Override 6 | public void next(Package pkg) { 7 | pkg.setState(new DeliveredState()); 8 | } 9 | 10 | @Override 11 | public void prev(Package pkg) { 12 | System.out.println("The package is in its root state."); 13 | } 14 | 15 | @Override 16 | public void printStatus() { 17 | System.out.println("Package ordered, not delivered to the office yet."); 18 | } 19 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/visitor/example1/Computer.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.visitor.example1; 2 | 3 | public class Computer implements ComputerPart { 4 | 5 | ComputerPart[] parts; 6 | 7 | public Computer() { 8 | parts = new ComputerPart[]{new Mouse(), new Keyboard(), new Monitor()}; 9 | } 10 | 11 | 12 | @Override 13 | public void accept(ComputerPartVisitor computerPartVisitor) { 14 | for (ComputerPart part : parts) { 15 | part.accept(computerPartVisitor); 16 | } 17 | computerPartVisitor.visit(this); 18 | } 19 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/singleton/EnumBasedSingleton.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.singleton; 2 | 3 | /** 4 | * @author Rohit Agarwal on 29/08/20 11:19 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | 8 | //enum is by default serializable 9 | public enum EnumBasedSingleton { 10 | INSTANCE; 11 | 12 | private int value; 13 | 14 | EnumBasedSingleton() { 15 | value = 42; 16 | } 17 | 18 | public int getValue() { 19 | return value; 20 | } 21 | 22 | public void setValue(int value) { 23 | this.value = value; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/example2/AssembleWorkShop.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example2; 2 | 3 | public class AssembleWorkShop extends Workshop { 4 | @Override 5 | public void work(Vehicle vehicle) { 6 | System.out.print("Assembling... "); 7 | long timeToTake = 200 * vehicle.minWorkTime(); 8 | try { 9 | Thread.sleep(timeToTake); 10 | } catch (InterruptedException exp) { 11 | // nothing to do for now. 12 | } 13 | System.out.printf("(Time taken: %d millis), Done.\n", timeToTake); 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/ocp/good/ProductFilter.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.ocp.good; 2 | 3 | import com.javamultiplex.principle.solid.ocp.Product; 4 | 5 | import java.util.List; 6 | import java.util.stream.Stream; 7 | 8 | /** 9 | * @author Rohit Agarwal on 16/08/20 2:42 pm 10 | * @copyright www.javamultiplex.com 11 | */ 12 | public class ProductFilter implements Filter { 13 | @Override 14 | public Stream filter(List items, Specification specification) { 15 | return items.stream().filter(specification::isSatisfied); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/testing/principles/example4/CalculatorV2.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.principles.example4; 2 | 3 | /** 4 | * @author Rohit Agarwal on 06/09/20 8:17 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class CalculatorV2 { 8 | 9 | public Division division(int number1, int number2) { 10 | if (number2 == 0) { 11 | throw new IllegalArgumentException("/ by zero is not possible"); 12 | } 13 | int quotient = number1 / number2; 14 | int remainder = number1 % number2; 15 | return new Division(quotient, remainder); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/adapter/example1/Rectangle.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.adapter.example1; 2 | 3 | public class Rectangle implements Shape { 4 | @Override 5 | public void draw() { 6 | System.out.println("Drawing Rectangle"); 7 | } 8 | 9 | @Override 10 | public void resize() { 11 | System.out.println("Resizing Rectangle"); 12 | } 13 | 14 | @Override 15 | public String description() { 16 | return "Rectangle object"; 17 | } 18 | 19 | @Override 20 | public boolean isHide() { 21 | return false; 22 | } 23 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/bridge/example2/Vehicle.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author Rohit Agarwal on 30/08/20 9:07 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public abstract class Vehicle { 11 | protected List workshops = new ArrayList<>(); 12 | 13 | public void joinWorkshop(Workshop workshop) { 14 | workshops.add(workshop); 15 | } 16 | 17 | protected abstract void manufacture(); 18 | 19 | protected abstract int minWorkTime(); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/builder/multi/PersonBuilder.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.builder.multi; 2 | 3 | /** 4 | * @author Rohit Agarwal on 23/08/20 9:10 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class PersonBuilder { 8 | protected Person person = new Person(); 9 | 10 | public PersonAddressBuilder address() { 11 | return new PersonAddressBuilder(person); 12 | } 13 | 14 | public PersonJobBuilder job() { 15 | return new PersonJobBuilder(person); 16 | } 17 | 18 | public Person build() { 19 | return person; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/yagni/BinarySearch.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.yagni; 2 | 3 | /** 4 | * @author Rohit Agarwal on 20/08/20 11:14 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class BinarySearch { 8 | 9 | private final SortAlgorithm sortAlgorithm; 10 | 11 | public BinarySearch(SortAlgorithm sortAlgorithm) { 12 | this.sortAlgorithm = sortAlgorithm; 13 | } 14 | 15 | public int search(int[] numbers, int numberToSearch) { 16 | int[] sortedArray = sortAlgorithm.sort(numbers); 17 | //TODO: Binary Search Implementation 18 | return -1; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/structural/proxy/ProxyClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.proxy; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | /** 6 | * @author Rohit Agarwal on 01/09/20 9:58 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class ProxyClient { 10 | 11 | @Test 12 | public void shouldCreateImage() { 13 | Image image = new ProxyImage("test_10mb.jpg"); 14 | 15 | //image will be loaded from disk 16 | image.display(); 17 | System.out.println(""); 18 | 19 | //image will not be loaded from disk 20 | image.display(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/factory/README.md: -------------------------------------------------------------------------------- 1 | # Factory Method 2 | 3 | Factory Method defines a method, which should be used for creating objects instead of direct constructor call (new operator). 4 | 5 | ## The pattern is present in core Java libraries: 6 | 7 | - java.util.Calendar#getInstance() 8 | - java.util.ResourceBundle#getBundle() 9 | - java.text.NumberFormat#getInstance() 10 | - java.nio.charset.Charset#forName() 11 | - java.net.URLStreamHandlerFactory#createURLStreamHandler(String) (Returns different singleton objects, depending on a protocol) 12 | - java.util.EnumSet#of() 13 | - javax.xml.bind.JAXBContext#createMarshaller() 14 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/testing/principles/example4/Division.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.principles.example4; 2 | 3 | /** 4 | * @author Rohit Agarwal on 06/09/20 9:10 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Division { 8 | private final int quotient; 9 | private final int remainder; 10 | 11 | public Division(int quotient, int remainder) { 12 | this.quotient = quotient; 13 | this.remainder = remainder; 14 | } 15 | 16 | public int getQuotient() { 17 | return quotient; 18 | } 19 | 20 | public int getRemainder() { 21 | return remainder; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/testing/tddbdd/example2/FizzBuzz.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.tddbdd.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 22/08/20 4:56 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class FizzBuzz { 8 | public String play(final int number) { 9 | if (number == 0) { 10 | throw new IllegalArgumentException("Number must be greater than 0"); 11 | } 12 | if (number % 3 == 0) { 13 | return "Fizz"; 14 | } 15 | if (number % 5 == 0) { 16 | return "Buzz"; 17 | } 18 | return String.valueOf(number); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/state/example1/Shipped.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.state.example1; 2 | 3 | public class Shipped implements PackageState 4 | { 5 | //Singleton 6 | private static Shipped instance = new Shipped(); 7 | 8 | private Shipped() {} 9 | 10 | public static Shipped instance() { 11 | return instance; 12 | } 13 | 14 | //Business logic and state transition 15 | @Override 16 | public void updateState(DeliveryContext ctx) 17 | { 18 | System.out.println("Package is shipped !!"); 19 | ctx.setCurrentState(InTransition.instance()); 20 | } 21 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/state/example2/Package.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.state.example2; 2 | 3 | public class Package { 4 | 5 | private PackageState state = new OrderedState(); 6 | 7 | public PackageState getState() { 8 | return state; 9 | } 10 | 11 | public void setState(PackageState state) { 12 | this.state = state; 13 | } 14 | 15 | public void previousState() { 16 | state.prev(this); 17 | } 18 | 19 | public void nextState() { 20 | state.next(this); 21 | } 22 | 23 | public void printStatus() { 24 | state.printStatus(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/abstractfactory/FactoryProvider.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.abstractfactory; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 8:56 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class FactoryProvider { 8 | 9 | public static AbstractFactory getFactory(String choice) { 10 | if ("color".equalsIgnoreCase(choice)) { 11 | return new ColorFactory(); 12 | } else if ("shape".equalsIgnoreCase(choice)) { 13 | return new ShapeFactory(); 14 | } 15 | throw new UnsupportedOperationException("Choice : " + choice + " not supported"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/builder/single/example2/CodeBuilder.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.builder.single.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 23/08/20 9:57 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class CodeBuilder { 8 | private Code code = new Code(); 9 | 10 | public CodeBuilder(String className) { 11 | code.setClassName(className); 12 | } 13 | 14 | public CodeBuilder addField(String name, String type) { 15 | code.getList().add(new Field(name, type)); 16 | return this; 17 | } 18 | 19 | public Code build() { 20 | return code; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/behavioral/state/example1/CourierClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.state.example1; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | /** 6 | * @author Rohit Agarwal on 09/09/20 9:04 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class CourierClient { 10 | 11 | @Test 12 | public void shouldDeliverPackage(){ 13 | DeliveryContext deliveryContext=new DeliveryContext(null,"123"); 14 | deliveryContext.update(); 15 | deliveryContext.update(); 16 | deliveryContext.update(); 17 | deliveryContext.update(); 18 | deliveryContext.update(); 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/testing/principles/example4/DivisionTest.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.principles.example4; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | 6 | /** 7 | * @author Rohit Agarwal on 06/09/20 10:00 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class DivisionTest { 11 | 12 | 13 | @Test 14 | public void shouldCreateDivisionObject() { 15 | Division division = new Division(10, 3); 16 | Assertions.assertNotNull(division); 17 | Assertions.assertEquals(10, division.getQuotient()); 18 | Assertions.assertEquals(3, division.getRemainder()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/state/example1/Acknowledged.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.state.example1; 2 | 3 | public class Acknowledged implements PackageState 4 | { 5 | //Singleton 6 | private static Acknowledged instance = new Acknowledged(); 7 | 8 | private Acknowledged() {} 9 | 10 | public static Acknowledged instance() { 11 | return instance; 12 | } 13 | 14 | //Business logic and state transition 15 | @Override 16 | public void updateState(DeliveryContext ctx) 17 | { 18 | System.out.println("Package is acknowledged !!"); 19 | ctx.setCurrentState(Shipped.instance()); 20 | } 21 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/visitor/example2/Book.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.visitor.example2; 2 | 3 | public class Book implements ItemElement { 4 | 5 | private final int price; 6 | private final String isbnNumber; 7 | 8 | public Book(int cost, String isbn) { 9 | this.price = cost; 10 | this.isbnNumber = isbn; 11 | } 12 | 13 | public int getPrice() { 14 | return price; 15 | } 16 | 17 | public String getIsbnNumber() { 18 | return isbnNumber; 19 | } 20 | 21 | @Override 22 | public int accept(ShoppingCartVisitor visitor) { 23 | return visitor.visit(this); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/dry/Calculator.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.dry; 2 | 3 | /** 4 | * @author Rohit Agarwal on 21/08/20 11:39 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Calculator { 8 | 9 | public int addition(final int num1, final int num2) { 10 | return num1 + num2; 11 | } 12 | 13 | public int subtraction(final int num1, final int num2) { 14 | return num1 - num2; 15 | } 16 | 17 | public int division(final int num1, final int num2) { 18 | return num1 / num2; 19 | } 20 | 21 | public int multiplication(final int num1, final int num2) { 22 | return num1 * num2; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/ocp/good/SizeSpecification.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.ocp.good; 2 | 3 | import com.javamultiplex.principle.solid.ocp.Product; 4 | import com.javamultiplex.principle.solid.ocp.Size; 5 | 6 | /** 7 | * @author Rohit Agarwal on 16/08/20 2:40 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class SizeSpecification implements Specification { 11 | 12 | private final Size size; 13 | 14 | public SizeSpecification(Size size) { 15 | this.size = size; 16 | } 17 | 18 | @Override 19 | public boolean isSatisfied(Product item) { 20 | return size == item.getSize(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/state/example1/InTransition.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.state.example1; 2 | 3 | public class InTransition implements PackageState 4 | { 5 | //Singleton 6 | private static InTransition instance = new InTransition(); 7 | 8 | private InTransition() {} 9 | 10 | public static InTransition instance() { 11 | return instance; 12 | } 13 | 14 | //Business logic and state transition 15 | @Override 16 | public void updateState(DeliveryContext ctx) 17 | { 18 | System.out.println("Package is in transition !!"); 19 | ctx.setCurrentState(OutForDelivery.instance()); 20 | } 21 | } -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/principle/yagni/BinarySearchTest.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.yagni; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | 6 | /** 7 | * @author Rohit Agarwal on 20/08/20 11:19 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class BinarySearchTest { 11 | 12 | @Test 13 | public void shouldSearchWithBubbleSortImplementation() { 14 | SortAlgorithm sortAlgorithm = new BubbleSort(); 15 | BinarySearch binarySearch = new BinarySearch(sortAlgorithm); 16 | int index = binarySearch.search(new int[]{1, 8, 3, 4, 9}, 8); 17 | Assertions.assertEquals(-1, index); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/singleton/BasicSingleton.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.singleton; 2 | 3 | /** 4 | * @author Rohit Agarwal on 27/08/20 11:59 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class BasicSingleton { 8 | private static final BasicSingleton INSTANCE = new BasicSingleton(); 9 | private int value; 10 | 11 | private BasicSingleton() { 12 | } 13 | 14 | public static BasicSingleton getInstance() { 15 | return INSTANCE; 16 | } 17 | 18 | public int getValue() { 19 | return value; 20 | } 21 | 22 | public void setValue(int value) { 23 | this.value = value; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/ocp/good/ColorSpecification.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.ocp.good; 2 | 3 | import com.javamultiplex.principle.solid.ocp.Color; 4 | import com.javamultiplex.principle.solid.ocp.Product; 5 | 6 | /** 7 | * @author Rohit Agarwal on 16/08/20 2:39 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class ColorSpecification implements Specification { 11 | 12 | private final Color color; 13 | 14 | public ColorSpecification(Color color) { 15 | this.color = color; 16 | } 17 | 18 | @Override 19 | public boolean isSatisfied(Product item) { 20 | return color == item.getColor(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/state/example1/OutForDelivery.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.state.example1; 2 | 3 | public class OutForDelivery implements PackageState 4 | { 5 | //Singleton 6 | private static OutForDelivery instance = new OutForDelivery(); 7 | 8 | private OutForDelivery() {} 9 | 10 | public static OutForDelivery instance() { 11 | return instance; 12 | } 13 | 14 | //Business logic and state transition 15 | @Override 16 | public void updateState(DeliveryContext ctx) 17 | { 18 | System.out.println("Package is out of delivery !!"); 19 | ctx.setCurrentState(Delivered.instance()); 20 | } 21 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/facade/README.md: -------------------------------------------------------------------------------- 1 | # Facade Pattern 2 | 3 | Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system. 4 | 5 | ## Class Diagram 6 | ![Facade pattern class diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/structural/uml/Facade_Design_Pattern_Class_Diagram_UML.svg) 7 | 8 | ## Sequence Diagram 9 | ![Facade pattern sequence diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/structural/uml/Facade_Design_Pattern_Sequence_Diagram_UML.svg) 10 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/behavioral/chainofresponsibility/ATMDispenserClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.chainofresponsibility; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | /** 6 | * @author Rohit Agarwal on 02/09/20 11:28 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class ATMDispenserClient { 10 | 11 | @Test 12 | public void shouldDispenseCurrency() { 13 | DispenseChain c1 = new Dollar50Dispenser(); 14 | DispenseChain c2 = new Dollar20Dispenser(); 15 | DispenseChain c3 = new Dollar10Dispenser(); 16 | c1.setNextChain(c2); 17 | c2.setNextChain(c3); 18 | 19 | c1.dispense(new Currency(130)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/testing/principles/example3/DateTimeService.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.principles.example3; 2 | 3 | import java.time.DayOfWeek; 4 | import java.time.LocalDate; 5 | 6 | /** 7 | * @author Rohit Agarwal on 05/09/20 11:27 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class DateTimeService { 11 | 12 | public String getWeekDay() { 13 | LocalDate currentDate = LocalDate.now(); 14 | DayOfWeek dayOfWeek = currentDate.getDayOfWeek(); 15 | int weekDay = dayOfWeek.getValue(); 16 | String[] weekdays = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; 17 | return weekdays[weekDay-1]; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/behavioral/command/example1/BrokerClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.command.example1; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | /** 6 | * @author Rohit Agarwal on 03/09/20 11:09 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class BrokerClient { 10 | 11 | @Test 12 | public void brokerShouldPlaceOrders() { 13 | Stock abcStock = new Stock(); 14 | Order buyStock = new BuyStock(abcStock); 15 | Order sellStock = new SellStock(abcStock); 16 | Broker broker = new Broker(); 17 | broker.takeOrder(buyStock); 18 | broker.takeOrder(sellStock); 19 | broker.placeOrders(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/kiss/Example1.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.kiss; 2 | 3 | import java.util.stream.IntStream; 4 | 5 | /** 6 | * @author Rohit Agarwal on 20/08/20 8:52 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | 10 | //Find sum of first n natural numbers 11 | public class Example1 { 12 | 13 | public int sum1(final int n) { 14 | int sum = 0; 15 | for (int i = 1; i <= n; i++) { 16 | sum += i; 17 | } 18 | return sum; 19 | } 20 | 21 | public int sum2(final int n) { 22 | return IntStream.range(1, n + 1).sum(); 23 | } 24 | 25 | public int sum3(final int n) { 26 | return n * (n + 1) / 2; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/multiton/Printer.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.multiton; 2 | 3 | import java.util.HashMap; 4 | 5 | /** 6 | * @author Rohit Agarwal on 29/08/20 11:55 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class Printer { 10 | private static HashMap hashMap = new HashMap<>(); 11 | 12 | private Printer() { 13 | 14 | } 15 | 16 | public static Printer get(SubSystem subSystem) { 17 | if (hashMap.containsKey(subSystem)) { 18 | return hashMap.get(subSystem); 19 | } 20 | Printer printer = new Printer(); 21 | hashMap.put(subSystem, printer); 22 | return printer; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/dip/bad/Project.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.dip.bad; 2 | 3 | /** 4 | * @author Rohit Agarwal on 20/08/20 8:20 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Project { 8 | private final BackendDeveloper backendDeveloper; 9 | private final FrontEndDeveloper frontEndDeveloper; 10 | 11 | public Project(BackendDeveloper backendDeveloper, FrontEndDeveloper frontEndDeveloper) { 12 | this.backendDeveloper = backendDeveloper; 13 | this.frontEndDeveloper = frontEndDeveloper; 14 | } 15 | 16 | public void implement() { 17 | backendDeveloper.writeJava(); 18 | frontEndDeveloper.writeJavascript(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/behavioral/observer/example1/NewsAgencyClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.observer.example1; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | 6 | /** 7 | * @author Rohit Agarwal on 13/09/20 11:07 am 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class NewsAgencyClient { 11 | 12 | @Test 13 | public void shouldNotifyObservers() { 14 | NewsAgency observable = new NewsAgency(); 15 | NewsChannel observer = new NewsChannel(); 16 | 17 | observable.addObserver(observer); 18 | observable.setNews("news"); 19 | Assertions.assertEquals(observer.getNews(), "news"); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/templatemethod/NetOrder.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.templatemethod; 2 | 3 | class NetOrder extends OrderProcessTemplate { 4 | @Override 5 | public void doSelect() { 6 | System.out.println("Item added to online shopping cart"); 7 | System.out.println("Get gift wrap preference"); 8 | System.out.println("Get delivery address."); 9 | } 10 | 11 | @Override 12 | public void doPayment() { 13 | System.out.println("Online Payment through Netbanking, card or Paytm"); 14 | } 15 | 16 | @Override 17 | public void doDelivery() { 18 | System.out.println("Ship the item through post to delivery address"); 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/lsp/bad/Rectangle.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.lsp.bad; 2 | 3 | /** 4 | * @author Rohit Agarwal on 18/08/20 11:48 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Rectangle { 8 | private int height; 9 | private int width; 10 | 11 | public int getHeight() { 12 | return height; 13 | } 14 | 15 | public void setHeight(int height) { 16 | this.height = height; 17 | } 18 | 19 | public int getWidth() { 20 | return width; 21 | } 22 | 23 | public void setWidth(int width) { 24 | this.width = width; 25 | } 26 | 27 | public int calculateArea() { 28 | return height * width; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/observer/example1/NewsAgency.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.observer.example1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class NewsAgency { 7 | private String news; 8 | private final List channels = new ArrayList<>(); 9 | 10 | public void addObserver(Channel channel) { 11 | this.channels.add(channel); 12 | } 13 | 14 | public void removeObserver(Channel channel) { 15 | this.channels.remove(channel); 16 | } 17 | 18 | public void setNews(String news) { 19 | this.news = news; 20 | for (Channel channel : this.channels) { 21 | channel.update(this.news); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/srp/example1/bad/Employee.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.srp.example1.bad; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * @author Rohit Agarwal on 20/08/20 8:02 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class Employee { 10 | private String employeeId; 11 | private String name; 12 | private String address; 13 | private Date dateOfJoining; 14 | 15 | public boolean isPromotionDueThisYear() { 16 | //TODO: promotion logic implementation 17 | return false; 18 | } 19 | 20 | public Double calcIncomeTaxForCurrentYear() { 21 | //TODO: income tax logic implementation 22 | return 0d; 23 | } 24 | 25 | //Getters & Setters 26 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/composite/example2/Manager.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.composite.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 01/09/20 9:27 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Manager implements Employee { 8 | private final String name; 9 | private final int empId; 10 | private final String position; 11 | 12 | public Manager(String name, int empId, String position) { 13 | this.name = name; 14 | this.empId = empId; 15 | this.position = position; 16 | } 17 | 18 | @Override 19 | public void showEmployeeDetails() { 20 | System.out.printf("Name: %s, EmpId: %d, Position: %s\n", name, empId, position); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/isp/bad/NewGenerationPrinter.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.isp.bad; 2 | 3 | import com.javamultiplex.principle.solid.isp.Document; 4 | 5 | /** 6 | * @author Rohit Agarwal on 19/08/20 11:33 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class NewGenerationPrinter implements Machine { 10 | 11 | @Override 12 | public void print(Document d) { 13 | System.out.println("Printing Document : " + d); 14 | } 15 | 16 | @Override 17 | public void scan(Document d) { 18 | System.out.println("Scanning Document : " + d); 19 | } 20 | 21 | @Override 22 | public void fax(Document d) { 23 | System.out.println("Scanning Document : " + d); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/behavioral/templatemethod/TemplateMethodClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.templatemethod; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | /** 6 | * @author Rohit Agarwal on 03/09/20 9:33 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class TemplateMethodClient { 10 | 11 | @Test 12 | public void shouldProcessOrderByInternet() { 13 | OrderProcessTemplate orderProcessTemplate=new NetOrder(); 14 | orderProcessTemplate.processOrder(true); 15 | } 16 | 17 | @Test 18 | public void shouldProcessOrderByVisitingStore() { 19 | OrderProcessTemplate orderProcessTemplate=new StoreOrder(); 20 | orderProcessTemplate.processOrder(false); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/abstractfactory/ColorFactory.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.abstractfactory; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 8:49 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class ColorFactory implements AbstractFactory { 8 | 9 | @Override 10 | public Color create(String type) { 11 | if ("black".equalsIgnoreCase(type)) { 12 | return new Black(); 13 | } else if ("white".equalsIgnoreCase(type)) { 14 | return new White(); 15 | } else if ("red".equalsIgnoreCase(type)) { 16 | return new Red(); 17 | } 18 | throw new UnsupportedOperationException("Type : " + type + " not supported."); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/composite/example2/Developer.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.composite.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 01/09/20 9:27 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Developer implements Employee { 8 | private final String name; 9 | private final int empId; 10 | private final String position; 11 | 12 | public Developer(String name, int empId, String position) { 13 | this.name = name; 14 | this.empId = empId; 15 | this.position = position; 16 | } 17 | 18 | @Override 19 | public void showEmployeeDetails() { 20 | System.out.printf("Name: %s, EmpId: %d, Position: %s\n", name, empId, position); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/structural/adapter/example1/DrawingClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.adapter.example1; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | /** 6 | * @author Rohit Agarwal on 30/08/20 7:21 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class DrawingClient { 10 | 11 | @Test 12 | public void shouldCreateAndResizeDrawings() { 13 | Drawing drawing = new Drawing(); 14 | drawing.addShape(new Rectangle()); 15 | drawing.addShape(new Circle()); 16 | drawing.addShape(new GeometricShapeObjectAdapter(new Triangle())); 17 | drawing.addShape(new GeometricShapeObjectAdapter(new Rhombus())); 18 | drawing.draw(); 19 | drawing.resize(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/principle/solid/dip/bad/ProjectTest.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.dip.bad; 2 | 3 | import org.junit.jupiter.api.BeforeEach; 4 | import org.junit.jupiter.api.Test; 5 | 6 | /** 7 | * @author Rohit Agarwal on 20/08/20 8:29 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class ProjectTest { 11 | private Project project; 12 | 13 | @BeforeEach 14 | void setUp() { 15 | BackendDeveloper backendDeveloper = new BackendDeveloper(); 16 | FrontEndDeveloper frontEndDeveloper = new FrontEndDeveloper(); 17 | project = new Project(backendDeveloper, frontEndDeveloper); 18 | } 19 | 20 | @Test 21 | public void shouldImplement() { 22 | project.implement(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/visitor/example1/ComputerPartDisplayVisitor.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.visitor.example1; 2 | 3 | public class ComputerPartDisplayVisitor implements ComputerPartVisitor { 4 | 5 | @Override 6 | public void visit(Computer computer) { 7 | System.out.println("Displaying Computer."); 8 | } 9 | 10 | @Override 11 | public void visit(Mouse mouse) { 12 | System.out.println("Displaying Mouse."); 13 | } 14 | 15 | @Override 16 | public void visit(Keyboard keyboard) { 17 | System.out.println("Displaying Keyboard."); 18 | } 19 | 20 | @Override 21 | public void visit(Monitor monitor) { 22 | System.out.println("Displaying Monitor."); 23 | } 24 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/flyweight/CounterTerrorist.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.flyweight; 2 | 3 | class CounterTerrorist implements Player 4 | { 5 | // Intrinsic Attribute 6 | private final String TASK; 7 | 8 | // Extrinsic Attribute 9 | private String weapon; 10 | 11 | public CounterTerrorist() 12 | { 13 | TASK = "DIFFUSE BOMB"; 14 | System.out.println("Counter Terrorist"); 15 | } 16 | public void assignWeapon(String weapon) 17 | { 18 | this.weapon = weapon; 19 | } 20 | public void mission() 21 | { 22 | System.out.println("Counter Terrorist with weapon " 23 | + weapon + "|" + " Task is " + TASK); 24 | } 25 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/abstractfactory/ShapeFactory.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.abstractfactory; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 8:52 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class ShapeFactory implements AbstractFactory { 8 | @Override 9 | public Shape create(String type) { 10 | if ("rectangle".equalsIgnoreCase(type)) { 11 | return new Rectangle(); 12 | } else if ("circle".equalsIgnoreCase(type)) { 13 | return new Circle(); 14 | } else if ("square".equalsIgnoreCase(type)) { 15 | return new Square(); 16 | } 17 | throw new UnsupportedOperationException("Type : " + type + " not supported"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/isp/good/NewGenerationPrinter.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.isp.good; 2 | 3 | import com.javamultiplex.principle.solid.isp.Document; 4 | 5 | /** 6 | * @author Rohit Agarwal on 19/08/20 11:33 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class NewGenerationPrinter implements Printer, Scanner, Fax { 10 | 11 | @Override 12 | public void print(Document d) { 13 | System.out.println("Printing Document : " + d); 14 | } 15 | 16 | @Override 17 | public void scan(Document d) { 18 | System.out.println("Scanning Document : " + d); 19 | } 20 | 21 | @Override 22 | public void fax(Document d) { 23 | System.out.println("Scanning Document : " + d); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/facade/ShopKeeper.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.facade; 2 | 3 | public class ShopKeeper { 4 | private final MobileShop iphone; 5 | private final MobileShop samsung; 6 | private final MobileShop blackberry; 7 | 8 | public ShopKeeper() { 9 | iphone = new Iphone(); 10 | samsung = new Samsung(); 11 | blackberry = new Blackberry(); 12 | } 13 | 14 | public void iphoneSale() { 15 | iphone.modelNo(); 16 | iphone.price(); 17 | } 18 | 19 | public void samsungSale() { 20 | samsung.modelNo(); 21 | samsung.price(); 22 | } 23 | 24 | public void blackberrySale() { 25 | blackberry.modelNo(); 26 | blackberry.price(); 27 | } 28 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/testing/tddbdd/example1/OperationFactory.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.tddbdd.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 22/08/20 8:26 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public final class OperationFactory { 8 | public static Operation getInstance(final Operator operator) { 9 | Operation operation; 10 | switch (operator) { 11 | case ADD: 12 | operation = new Addition(); 13 | break; 14 | case SUB: 15 | operation = new Subtraction(); 16 | break; 17 | default: 18 | throw new UnsupportedOperationException(operator + " not supported"); 19 | } 20 | return operation; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/structural/composite/example1/DepartmentClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.composite.example1; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | /** 6 | * @author Rohit Agarwal on 01/09/20 9:20 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class DepartmentClient { 10 | 11 | @Test 12 | public void shouldCreateDepartments() { 13 | Department financeDepartment = new FinanceDepartment(); 14 | Department salesDepartment = new SalesDepartment(); 15 | HeadDepartment headDepartment = new HeadDepartment(); 16 | headDepartment.addDepartment(financeDepartment); 17 | headDepartment.addDepartment(salesDepartment); 18 | headDepartment.printDepartmentName(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/principle/dry/WebsiteTest.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.dry; 2 | 3 | import org.junit.jupiter.api.BeforeEach; 4 | import org.junit.jupiter.api.Test; 5 | 6 | /** 7 | * @author Rohit Agarwal on 20/08/20 11:55 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class WebsiteTest { 11 | private Website website; 12 | 13 | @BeforeEach 14 | void setUp() { 15 | website = new Website(); 16 | } 17 | 18 | @Test 19 | public void shouldPrintHomePage() { 20 | website.homePage(); 21 | } 22 | 23 | @Test 24 | public void shouldPrintAboutPage() { 25 | website.aboutPage(); 26 | } 27 | 28 | @Test 29 | public void shouldPrintContactPage() { 30 | website.contactPage(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/builder/single/example2/Field.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.builder.single.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 23/08/20 9:54 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Field { 8 | private final String name; 9 | private final String type; 10 | 11 | public Field(String name, String type) { 12 | this.name = name; 13 | this.type = type; 14 | } 15 | 16 | public String getName() { 17 | return name; 18 | } 19 | 20 | public String getType() { 21 | return type; 22 | } 23 | 24 | @Override 25 | public String toString() { 26 | return String.format(" public %s %s;" + System.lineSeparator(), type, name); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/composite/example2/Director.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.composite.example2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author Rohit Agarwal on 01/09/20 9:30 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class Director implements Employee { 11 | 12 | private final List employees = new ArrayList<>(); 13 | 14 | @Override 15 | public void showEmployeeDetails() { 16 | employees.forEach(Employee::showEmployeeDetails); 17 | } 18 | 19 | public void addEmployee(Employee employee) { 20 | employees.add(employee); 21 | } 22 | 23 | public void removeEmployee(Employee employee) { 24 | employees.remove(employee); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/flyweight/Terrorist.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.flyweight; 2 | 3 | class Terrorist implements Player 4 | { 5 | // Intrinsic Attribute 6 | private final String TASK; 7 | 8 | // Extrinsic Attribute 9 | private String weapon; 10 | 11 | public Terrorist() 12 | { 13 | TASK = "PLANT A BOMB"; 14 | System.out.println("Terrorist"); 15 | } 16 | public void assignWeapon(String weapon) 17 | { 18 | // Assign a weapon 19 | this.weapon = weapon; 20 | } 21 | public void mission() 22 | { 23 | //Work on the Mission 24 | System.out.println("Terrorist with weapon " 25 | + weapon + "|" + " Task is " + TASK); 26 | } 27 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/isp/bad/OldGenerationPrinter.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.isp.bad; 2 | 3 | import com.javamultiplex.principle.solid.isp.Document; 4 | 5 | /** 6 | * @author Rohit Agarwal on 19/08/20 11:37 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class OldGenerationPrinter implements Machine { 10 | 11 | @Override 12 | public void print(Document d) { 13 | System.out.println("Printing Document : " + d); 14 | } 15 | 16 | @Override 17 | public void scan(Document d) { 18 | throw new UnsupportedOperationException("Scanning not supported"); 19 | } 20 | 21 | @Override 22 | public void fax(Document d) { 23 | throw new UnsupportedOperationException("Fax operation not supported"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/singleton/SingletonV2.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.singleton; 2 | 3 | /** 4 | * @author Rohit Agarwal on 27/08/20 11:59 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class SingletonV2 { 8 | private static final SingletonV2 INSTANCE = new SingletonV2(); 9 | private int value; 10 | 11 | private SingletonV2() { 12 | if (INSTANCE != null) { 13 | throw new InstantiationError("Object Creation is not allowed."); 14 | } 15 | } 16 | 17 | public static SingletonV2 getInstance() { 18 | return INSTANCE; 19 | } 20 | 21 | public int getValue() { 22 | return value; 23 | } 24 | 25 | public void setValue(int value) { 26 | this.value = value; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/testing/principles/example3/DateTimeServiceV2.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.principles.example3; 2 | 3 | import java.time.DayOfWeek; 4 | import java.time.LocalDate; 5 | 6 | /** 7 | * @author Rohit Agarwal on 05/09/20 11:27 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class DateTimeServiceV2 { 11 | 12 | public String getWeekDay(LocalDate localDate) { 13 | if (localDate == null) { 14 | throw new IllegalArgumentException("LocalDate is null."); 15 | } 16 | DayOfWeek dayOfWeek = localDate.getDayOfWeek(); 17 | int weekDay = dayOfWeek.getValue(); 18 | String[] weekdays = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; 19 | return weekdays[weekDay - 1]; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/principle/solid/lsp/good/AreaCalculatorTest.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.lsp.good; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | 6 | /** 7 | * @author Rohit Agarwal on 19/08/20 6:52 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | 11 | public class AreaCalculatorTest { 12 | 13 | @Test 14 | void shouldCalculateAreaOfRectangle() { 15 | Shape shape = new Rectangle(10, 20); 16 | int area = shape.calculateArea(); 17 | Assertions.assertEquals(200, area); 18 | } 19 | 20 | 21 | @Test 22 | void shouldCalculateAreaOfSquare() { 23 | Shape shape = new Square(10); 24 | int area = shape.calculateArea(); 25 | Assertions.assertEquals(100, area); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/mediator/ChatMediatorImpl.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.mediator; 2 | 3 | 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | public class ChatMediatorImpl implements ChatMediator { 8 | 9 | private List users; 10 | 11 | public ChatMediatorImpl() { 12 | this.users = new ArrayList<>(); 13 | } 14 | 15 | @Override 16 | public void addUser(User user) { 17 | this.users.add(user); 18 | } 19 | 20 | @Override 21 | public void sendMessage(String msg, User user) { 22 | for (User u : this.users) { 23 | //message should not be received by the user sending it 24 | if (u != user) { 25 | u.receive(msg); 26 | } 27 | } 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/adapter/example2/SocketObjectAdapterImpl.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.adapter.example2; 2 | 3 | public class SocketObjectAdapterImpl implements SocketAdapter { 4 | 5 | //Using Composition for adapter pattern 6 | private Socket sock = new Socket(); 7 | 8 | @Override 9 | public Volt get120Volt() { 10 | return sock.getVolt(); 11 | } 12 | 13 | @Override 14 | public Volt get12Volt() { 15 | Volt v = sock.getVolt(); 16 | return convertVolt(v, 10); 17 | } 18 | 19 | @Override 20 | public Volt get3Volt() { 21 | Volt v = sock.getVolt(); 22 | return convertVolt(v, 40); 23 | } 24 | 25 | private Volt convertVolt(Volt v, int i) { 26 | return new Volt(v.getVolts() / i); 27 | } 28 | } -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/testing/principles/example4/CalculatorV1Test.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.principles.example4; 2 | 3 | import org.junit.jupiter.api.BeforeEach; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | /** 9 | * @author Rohit Agarwal on 06/09/20 8:20 pm 10 | * @copyright www.javamultiplex.com 11 | */ 12 | public class CalculatorV1Test { 13 | private CalculatorV1 calculator; 14 | 15 | @BeforeEach 16 | void setUp() { 17 | calculator = new CalculatorV1(); 18 | } 19 | 20 | @Test 21 | public void shouldDivideTwoNumbers() { 22 | Division result = calculator.division(10, 5); 23 | assertEquals(2, result.getQuotient()); 24 | assertEquals(0, result.getRemainder()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/templatemethod/OrderProcessTemplate.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.templatemethod; 2 | 3 | abstract class OrderProcessTemplate { 4 | public boolean isGift; 5 | 6 | public abstract void doSelect(); 7 | 8 | public abstract void doPayment(); 9 | 10 | public final void giftWrap() { 11 | try { 12 | System.out.println("Gift wrap successful"); 13 | } catch (Exception e) { 14 | System.out.println("Gift wrap unsuccessful"); 15 | } 16 | } 17 | 18 | public abstract void doDelivery(); 19 | 20 | public final void processOrder(boolean isGift) { 21 | doSelect(); 22 | doPayment(); 23 | if (isGift) { 24 | giftWrap(); 25 | } 26 | doDelivery(); 27 | } 28 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/testing/principles/example1/Calculator.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.principles.example1; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * @author Rohit Agarwal on 04/09/20 9:56 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class Calculator { 10 | public T addition(List numbers, Adder adder) { 11 | T result = adder.zero(); 12 | for (T number : numbers) { 13 | result = adder.add(result, number); 14 | } 15 | return result; 16 | } 17 | 18 | public T multiply(List numbers, Multiplier multiplier) { 19 | T result = multiplier.one(); 20 | for (T number : numbers) { 21 | result = multiplier.multiply(result, number); 22 | } 23 | return result; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/principle/delegation/example1/TravelAgentTest.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.delegation.example1; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | /** 6 | * @author Rohit Agarwal on 22/08/20 12:01 am 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class TravelAgentTest { 10 | 11 | @Test 12 | public void shouldBookTrainTickets() { 13 | TravelBooking travelBooking = new TrainBooking(); 14 | TravelAgent travelAgent = new TravelAgent(travelBooking); 15 | travelAgent.bookTicket(); 16 | 17 | } 18 | 19 | @Test 20 | public void shouldBookAirTickets() { 21 | TravelBooking travelBooking = new AirBooking(); 22 | TravelAgent travelAgent = new TravelAgent(travelBooking); 23 | travelAgent.bookTicket(); 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/adapter/example1/Rhombus.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.adapter.example1; 2 | 3 | public class Rhombus implements GeometricShape { 4 | private final double a; 5 | private final double b; 6 | 7 | public Rhombus() { 8 | this(1.0d, 1.0d); 9 | } 10 | 11 | public Rhombus(double a, double b) { 12 | this.a = a; 13 | this.b = b; 14 | } 15 | 16 | @Override 17 | public double area() { 18 | double s = a * b; 19 | return s; 20 | } 21 | 22 | @Override 23 | public double perimeter() { 24 | return 2 * (a + b); 25 | } 26 | 27 | @Override 28 | public void drawShape() { 29 | System.out.println("Drawing Rhombus with area: " + area() + " and perimeter: " + perimeter()); 30 | } 31 | } -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/structural/facade/ShopKeeperClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.facade; 2 | 3 | import org.junit.jupiter.api.BeforeEach; 4 | import org.junit.jupiter.api.Test; 5 | 6 | /** 7 | * @author Rohit Agarwal on 30/08/20 11:49 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class ShopKeeperClient { 11 | private ShopKeeper shopKeeper; 12 | 13 | @BeforeEach 14 | void setUp() { 15 | shopKeeper = new ShopKeeper(); 16 | } 17 | 18 | @Test 19 | public void shouldGetIphone() { 20 | shopKeeper.iphoneSale(); 21 | } 22 | 23 | @Test 24 | public void shouldGetSamsung() { 25 | shopKeeper.samsungSale(); 26 | } 27 | 28 | @Test 29 | public void shouldGetBlackberry() { 30 | shopKeeper.blackberrySale(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/visitor/example2/Fruit.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.visitor.example2; 2 | 3 | public class Fruit implements ItemElement { 4 | 5 | private final int pricePerKg; 6 | private final int weight; 7 | private final String name; 8 | 9 | public Fruit(int priceKg, int wt, String nm) { 10 | this.pricePerKg = priceKg; 11 | this.weight = wt; 12 | this.name = nm; 13 | } 14 | 15 | public int getPricePerKg() { 16 | return pricePerKg; 17 | } 18 | 19 | 20 | public int getWeight() { 21 | return weight; 22 | } 23 | 24 | public String getName() { 25 | return this.name; 26 | } 27 | 28 | @Override 29 | public int accept(ShoppingCartVisitor visitor) { 30 | return visitor.visit(this); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/testing/principles/example3/DateTimeServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.principles.example3; 2 | 3 | import com.javamultiplex.testing.principles.example3.DateTimeService; 4 | import org.junit.jupiter.api.Assertions; 5 | import org.junit.jupiter.api.BeforeEach; 6 | import org.junit.jupiter.api.Test; 7 | 8 | /** 9 | * @author Rohit Agarwal on 05/09/20 11:32 pm 10 | * @copyright www.javamultiplex.com 11 | */ 12 | public class DateTimeServiceTest { 13 | 14 | private DateTimeService dateTimeService; 15 | 16 | @BeforeEach 17 | void setUp() { 18 | dateTimeService = new DateTimeService(); 19 | } 20 | 21 | @Test 22 | public void shouldGetDayOfWeek() { 23 | String weekDay = dateTimeService.getWeekDay(); 24 | Assertions.assertEquals("Saturday", weekDay); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/builder/multi/PersonJobBuilder.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.builder.multi; 2 | 3 | /** 4 | * @author Rohit Agarwal on 23/08/20 9:18 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class PersonJobBuilder extends PersonBuilder { 8 | public PersonJobBuilder(Person person) { 9 | this.person = person; 10 | } 11 | 12 | public PersonJobBuilder companyName(String companyName) { 13 | this.person.setCompanyName(companyName); 14 | return this; 15 | } 16 | 17 | public PersonJobBuilder position(String position) { 18 | this.person.setPosition(position); 19 | return this; 20 | } 21 | 22 | public PersonJobBuilder salary(int salary) { 23 | this.person.setAnnualSalary(salary); 24 | return this; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/visitor/example2/ShoppingCartVisitorImpl.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.visitor.example2; 2 | 3 | public class ShoppingCartVisitorImpl implements ShoppingCartVisitor { 4 | 5 | @Override 6 | public int visit(Book book) { 7 | int cost = 0; 8 | //apply 5$ discount if book price is greater than 50 9 | if (book.getPrice() > 50) { 10 | cost = book.getPrice() - 5; 11 | } else cost = book.getPrice(); 12 | System.out.println("Book ISBN::" + book.getIsbnNumber() + " cost =" + cost); 13 | return cost; 14 | } 15 | 16 | @Override 17 | public int visit(Fruit fruit) { 18 | int cost = fruit.getPricePerKg() * fruit.getWeight(); 19 | System.out.println(fruit.getName() + " cost = " + cost); 20 | return cost; 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/behavioral/mediator/ChatClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.mediator; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | /** 6 | * @author Rohit Agarwal on 11/09/20 11:50 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class ChatClient { 10 | 11 | @Test 12 | public void shouldSendMessageToEveryone() { 13 | ChatMediator mediator = new ChatMediatorImpl(); 14 | User user1 = new UserImpl(mediator, "Pankaj"); 15 | User user2 = new UserImpl(mediator, "Lisa"); 16 | User user3 = new UserImpl(mediator, "Saurabh"); 17 | User user4 = new UserImpl(mediator, "David"); 18 | mediator.addUser(user1); 19 | mediator.addUser(user2); 20 | mediator.addUser(user3); 21 | mediator.addUser(user4); 22 | user1.send("Hi All"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/singleton/StaticBlockSingleton.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.singleton; 2 | 3 | import java.io.IOException; 4 | import java.nio.file.Files; 5 | 6 | /** 7 | * @author Rohit Agarwal on 29/08/20 7:23 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class StaticBlockSingleton { 11 | 12 | private static StaticBlockSingleton instance; 13 | 14 | static { 15 | try { 16 | instance = new StaticBlockSingleton(); 17 | } catch (IOException e) { 18 | System.err.println("Error occurred in instance creation"); 19 | } 20 | } 21 | 22 | private StaticBlockSingleton() throws IOException { 23 | Files.createTempFile(".", "."); 24 | } 25 | 26 | public static StaticBlockSingleton getInstance() { 27 | return instance; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/builder/multi/PersonAddressBuilder.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.builder.multi; 2 | 3 | /** 4 | * @author Rohit Agarwal on 23/08/20 9:11 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class PersonAddressBuilder extends PersonBuilder { 8 | 9 | public PersonAddressBuilder(Person person) { 10 | this.person = person; 11 | } 12 | 13 | public PersonAddressBuilder streetAddress(String streetAddress) { 14 | this.person.setStreetAddress(streetAddress); 15 | return this; 16 | } 17 | 18 | public PersonAddressBuilder city(String city) { 19 | this.person.setCity(city); 20 | return this; 21 | } 22 | 23 | public PersonAddressBuilder postcode(String postcode) { 24 | this.person.setPostcode(postcode); 25 | return this; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/factory/example2/NotificationFactory.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.factory.example2; 2 | 3 | /** 4 | * @author Rohit Agarwal on 25/08/20 12:20 am 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public final class NotificationFactory { 8 | 9 | public static Notification createNotification(final NotificationChannel notificationChannel) { 10 | if (notificationChannel == NotificationChannel.SMS) { 11 | return new SMSNotification(); 12 | } else if (notificationChannel == NotificationChannel.EMAIL) { 13 | return new EmailNotification(); 14 | } else if (notificationChannel == NotificationChannel.PUSH) { 15 | return new PushNotification(); 16 | } 17 | throw new UnsupportedOperationException("Notification channel not supported"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/observer/example2/Subject.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.observer.example2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Subject { 7 | 8 | private final List observers = new ArrayList(); 9 | private int state; 10 | 11 | public int getState() { 12 | return state; 13 | } 14 | 15 | public void setState(int state) { 16 | this.state = state; 17 | notifyAllObservers(); 18 | } 19 | 20 | public void attach(Observer observer) { 21 | observers.add(observer); 22 | } 23 | 24 | public void detach(Observer observer) { 25 | observers.remove(observer); 26 | } 27 | 28 | public void notifyAllObservers() { 29 | for (Observer observer : observers) { 30 | observer.update(); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/ocp/good/AndSpecification.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.ocp.good; 2 | 3 | import com.javamultiplex.principle.solid.ocp.Product; 4 | 5 | /** 6 | * @author Rohit Agarwal on 16/08/20 2:49 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class AndSpecification implements Specification { 10 | 11 | private final ColorSpecification colorSpecification; 12 | private final SizeSpecification sizeSpecification; 13 | 14 | public AndSpecification(ColorSpecification colorSpecification, SizeSpecification sizeSpecification) { 15 | this.colorSpecification = colorSpecification; 16 | this.sizeSpecification = sizeSpecification; 17 | } 18 | 19 | @Override 20 | public boolean isSatisfied(Product item) { 21 | return colorSpecification.isSatisfied(item) && sizeSpecification.isSatisfied(item); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/srp/example2/good/Journal.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.srp.example2.good; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author Rohit Agarwal on 16/08/20 3:14 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class Journal { 11 | private final List entries; 12 | 13 | public Journal() { 14 | this.entries = new ArrayList<>(); 15 | } 16 | 17 | public void addEntry(String entry) { 18 | entries.add(entry); 19 | } 20 | 21 | public void addEntries(List entryList) { 22 | entries.addAll(entryList); 23 | } 24 | 25 | public void removeEntry(String entry) { 26 | entries.remove(entry); 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return String.join(System.lineSeparator(), entries); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/creational/factory/example1/PointClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.factory.example1; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | 6 | /** 7 | * @author Rohit Agarwal on 24/08/20 11:46 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class PointClient { 11 | 12 | @Test 13 | public void shouldCreateCartesianPoint() { 14 | Point point = Point.Factory.newCartesianPoint(10, 20); 15 | Assertions.assertEquals(10, point.getX()); 16 | Assertions.assertEquals(20, point.getY()); 17 | } 18 | 19 | @Test 20 | public void shouldCreatePolarPoint() { 21 | Point point = Point.Factory.newPolarPoint(10, 20); 22 | Assertions.assertEquals(10 * Math.sin(20), point.getX()); 23 | Assertions.assertEquals(10 * Math.cos(20), point.getY()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/iterator/TopicIterator.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.iterator; 2 | 3 | /** 4 | * @author Rohit Agarwal on 09/09/20 9:54 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class TopicIterator implements Iterator { 8 | 9 | private int position; 10 | private final Topic[] topics; 11 | 12 | public TopicIterator(Topic[] topics) { 13 | this.position = 0; 14 | this.topics = topics; 15 | } 16 | 17 | @Override 18 | public Topic next() { 19 | return topics[position++]; 20 | } 21 | 22 | @Override 23 | public boolean hasNext() { 24 | return position < topics.length; 25 | } 26 | 27 | @Override 28 | public void reset() { 29 | position = 0; 30 | } 31 | 32 | @Override 33 | public Topic currentItem() { 34 | return topics[position]; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/behavioral/iterator/IteratorClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.iterator; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | /** 6 | * @author Rohit Agarwal on 09/09/20 10:01 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class IteratorClient { 10 | 11 | @Test 12 | public void shouldIteratorTopic(){ 13 | Topic[] topics={ 14 | new Topic("topic1"), 15 | new Topic("topic2"), 16 | new Topic("topic3"), 17 | new Topic("topic4"), 18 | new Topic("topic5") 19 | }; 20 | 21 | List topicList=new TopicList(topics); 22 | Iterator iterator = topicList.iterator(); 23 | while (iterator.hasNext()){ 24 | Topic topic = iterator.next(); 25 | System.out.println(topic.getName()); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/testing/tddbdd/example2/FizzBuzzTest.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.tddbdd.example2; 2 | 3 | import org.junit.jupiter.api.BeforeEach; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | /** 9 | * @author Rohit Agarwal on 04/09/20 9:42 pm 10 | * @copyright www.javamultiplex.com 11 | */ 12 | public class FizzBuzzTest { 13 | 14 | private FizzBuzz fizzBuzz; 15 | 16 | @BeforeEach 17 | void setUp() { 18 | fizzBuzz = new FizzBuzz(); 19 | } 20 | 21 | @Test 22 | public void shouldReturnFizz_NumberIsMultipleOf3() { 23 | String result = fizzBuzz.play(6); 24 | assertEquals("Fizz", result); 25 | } 26 | 27 | @Test 28 | public void shouldReturnBuzz_NumberIsMultipleOf5() { 29 | String result = fizzBuzz.play(10); 30 | assertEquals("Buzz", result); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/builder/README.md: -------------------------------------------------------------------------------- 1 | # Builder Pattern 2 | 3 | Separate the construction of an object from its representation. 4 | 5 | ## Important Links 6 | - https://medium.com/@hazraarka072/fluent-builder-and-powering-it-up-with-recursive-generics-in-java-483005a85fcd 7 | 8 | - https://www.sitepoint.com/self-types-with-javas-generics/ 9 | 10 | ## Builder is widely used in Java core libraries: 11 | 12 | - java.lang.StringBuilder#append() (unsynchronized) 13 | - java.lang.StringBuffer#append() (synchronized) 14 | - java.nio.ByteBuffer#put() (also in CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer and DoubleBuffer) 15 | - javax.swing.GroupLayout.Group#addComponent() 16 | 17 | ## UML Diagram 18 | ![Builder pattern UML diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/creational/uml/Builder_Design_Pattern_UML.jpg) 19 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/testing/tddbdd/example1/CalculatorTest.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.tddbdd.example1; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.BeforeEach; 5 | import org.junit.jupiter.api.Test; 6 | 7 | /** 8 | * @author Rohit Agarwal on 22/08/20 8:45 pm 9 | * @copyright www.javamultiplex.com 10 | */ 11 | public class CalculatorTest { 12 | 13 | private Calculator calculator; 14 | 15 | @BeforeEach 16 | void setUp() { 17 | calculator = new Calculator(); 18 | } 19 | 20 | @Test 21 | public void shouldPerformAddition() { 22 | int result = calculator.calculate(Operator.ADD, 12, 2); 23 | Assertions.assertEquals(14, result); 24 | } 25 | 26 | @Test 27 | public void shouldPerformSubtraction() { 28 | int result = calculator.calculate(Operator.SUB, 12, 2); 29 | Assertions.assertEquals(10, result); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/factory/example1/Point.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.factory.example1; 2 | 3 | /** 4 | * @author Rohit Agarwal on 24/08/20 11:40 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Point { 8 | private final double x; 9 | private final double y; 10 | 11 | private Point(double x, double y) { 12 | this.x = x; 13 | this.y = y; 14 | } 15 | 16 | public double getX() { 17 | return x; 18 | } 19 | 20 | public double getY() { 21 | return y; 22 | } 23 | 24 | public static class Factory { 25 | public static Point newCartesianPoint(double x, double y) { 26 | return new Point(x, y); 27 | } 28 | 29 | public static Point newPolarPoint(double rho, double theta) { 30 | return new Point(rho * Math.sin(theta), rho * Math.cos(theta)); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/composite/example1/HeadDepartment.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.composite.example1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author Rohit Agarwal on 01/09/20 9:16 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class HeadDepartment implements Department { 11 | 12 | private final List childDepartments; 13 | 14 | public HeadDepartment() { 15 | childDepartments = new ArrayList<>(); 16 | } 17 | 18 | @Override 19 | public void printDepartmentName() { 20 | childDepartments.forEach(Department::printDepartmentName); 21 | } 22 | 23 | public void addDepartment(Department department) { 24 | childDepartments.add(department); 25 | } 26 | 27 | public void removeDepartment(Department department) { 28 | childDepartments.remove(department); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/structural/decorator/example2/CarClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.decorator.example2; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | /** 6 | * @author Rohit Agarwal on 01/09/20 11:50 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class CarClient { 10 | @Test 11 | public void ShouldCreateBasicCar() { 12 | Car car = new BasicCar(); 13 | car.assemble(); 14 | } 15 | 16 | @Test 17 | public void ShouldCreateLuxuryCar() { 18 | Car basicCar = new BasicCar(); 19 | Car luxuryCar = new LuxuryCar(basicCar); 20 | luxuryCar.assemble(); 21 | } 22 | 23 | @Test 24 | public void ShouldCreateSportsLuxuryCar() { 25 | Car basicCar = new BasicCar(); 26 | Car luxuryCar = new LuxuryCar(basicCar); 27 | Car sportsCar = new SportsCar(luxuryCar); 28 | sportsCar.assemble(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/behavioral/observer/example2/NumberSystemClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.observer.example2; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | /** 6 | * @author Rohit Agarwal on 13/09/20 11:14 am 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class NumberSystemClient { 10 | 11 | @Test 12 | public void shouldNotifyObservers() { 13 | Subject subject = new Subject(); 14 | 15 | HexaObserver hexaObserver = new HexaObserver(subject); 16 | new OctalObserver(subject); 17 | new BinaryObserver(subject); 18 | 19 | System.out.println("First state change: 15"); 20 | subject.setState(15); 21 | System.out.println("Second state change: 10"); 22 | subject.setState(10); 23 | 24 | subject.detach(hexaObserver); 25 | System.out.println("Third state change: 15"); 26 | subject.setState(5); 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/principle/solid/isp/good/OldGenerationPrinterTest.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.isp.good; 2 | 3 | 4 | import com.javamultiplex.principle.solid.isp.Document; 5 | import org.junit.jupiter.api.BeforeEach; 6 | import org.junit.jupiter.api.Test; 7 | 8 | /** 9 | * @author Rohit Agarwal on 19/08/20 11:43 pm 10 | * @copyright www.javamultiplex.com 11 | */ 12 | public class OldGenerationPrinterTest { 13 | private OldGenerationPrinter oldGenerationPrinter; 14 | private Document document; 15 | 16 | @BeforeEach 17 | void setUp() { 18 | oldGenerationPrinter = new OldGenerationPrinter(); 19 | document = new Document(); 20 | document.setName("Sample.txt"); 21 | document.setSize("123MB"); 22 | document.setContent("Hello World".getBytes()); 23 | } 24 | 25 | @Test 26 | public void shouldPrintDocument() { 27 | oldGenerationPrinter.print(document); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/principle/solid/lsp/bad/AreaCalculatorTest.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.lsp.bad; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | 6 | /** 7 | * @author Rohit Agarwal on 19/08/20 6:52 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | 11 | public class AreaCalculatorTest { 12 | 13 | @Test 14 | void shouldCalculateAreaOfRectangle() { 15 | Rectangle rectangle = new Rectangle(); 16 | rectangle.setWidth(10); 17 | rectangle.setHeight(20); 18 | int area = rectangle.calculateArea(); 19 | Assertions.assertEquals(200, area); 20 | } 21 | 22 | 23 | @Test 24 | void shouldCalculateAreaOfSquare() { 25 | Rectangle square = new Square(); 26 | square.setWidth(10); 27 | square.setHeight(20); 28 | int area = square.calculateArea(); 29 | Assertions.assertNotEquals(200, area); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/testing/tddbdd/example2/FizzBuzzStepdefs.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.tddbdd.example2; 2 | 3 | import io.cucumber.java.en.Given; 4 | import io.cucumber.java.en.Then; 5 | import io.cucumber.java.en.When; 6 | import org.junit.jupiter.api.Assertions; 7 | 8 | /** 9 | * @author Rohit Agarwal on 22/08/20 5:14 pm 10 | * @copyright www.javamultiplex.com 11 | */ 12 | public class FizzBuzzStepdefs { 13 | private FizzBuzz fizzBuzz; 14 | private String result; 15 | 16 | @Given("Create a FizzBuzz Game Play") 17 | public void createAFizzBuzzGamePlay() { 18 | fizzBuzz = new FizzBuzz(); 19 | } 20 | 21 | @When("I play with number {int}") 22 | public void iPlayWithNumber(int number) { 23 | result = fizzBuzz.play(number); 24 | } 25 | 26 | @Then("The result is {string}") 27 | public void theResultIs(String resultString) { 28 | Assertions.assertEquals(resultString, result); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/adapter/example1/Drawing.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.adapter.example1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Drawing { 7 | List shapes = new ArrayList<>(); 8 | 9 | public Drawing() { 10 | super(); 11 | } 12 | 13 | public void addShape(Shape shape) { 14 | shapes.add(shape); 15 | } 16 | 17 | public List getShapes() { 18 | return new ArrayList<>(shapes); 19 | } 20 | 21 | public void draw() { 22 | if (shapes.isEmpty()) { 23 | System.out.println("Nothing to draw!"); 24 | } else { 25 | shapes.forEach(Shape::draw); 26 | } 27 | } 28 | 29 | public void resize() { 30 | if (shapes.isEmpty()) { 31 | System.out.println("Nothing to resize!"); 32 | } else { 33 | shapes.forEach(Shape::resize); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/structural/bridge/example1/ShapeClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.bridge.example1; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | /** 6 | * @author Rohit Agarwal on 30/08/20 8:49 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class ShapeClient { 10 | 11 | @Test 12 | public void shouldFillRectangleWithBlueColor() { 13 | Shape shape = new Rectangle(new Blue()); 14 | shape.draw(); 15 | } 16 | 17 | @Test 18 | public void shouldFillRectangleWithRedColor() { 19 | Shape shape = new Rectangle(new Red()); 20 | shape.draw(); 21 | } 22 | 23 | @Test 24 | public void shouldFillTriangleWithBlueColor() { 25 | Shape shape = new Triangle(new Blue()); 26 | shape.draw(); 27 | } 28 | 29 | @Test 30 | public void shouldFillTriangleWithRedColor() { 31 | Shape shape = new Triangle(new Red()); 32 | shape.draw(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/util/SerializationUtil.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.util; 2 | 3 | import java.io.*; 4 | 5 | /** 6 | * @author Rohit Agarwal on 29/08/20 6:58 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public final class SerializationUtil { 10 | public static void serialize(Object object, String fileName) throws IOException { 11 | try (OutputStream outputStream = new FileOutputStream(new File(fileName)); 12 | ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)) { 13 | objectOutputStream.writeObject(object); 14 | } 15 | } 16 | 17 | 18 | public static Object deserialize(String fileName) throws IOException, ClassNotFoundException { 19 | try (InputStream inputStream = new FileInputStream(new File(fileName)); 20 | ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) { 21 | return objectInputStream.readObject(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/creational/builder/single/example1/PersonClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.builder.single.example1; 2 | 3 | import com.javamultiplex.pattern.creational.builder.single.example1.Person.PersonBuilder; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | /** 9 | * @author Rohit Agarwal on 23/08/20 7:24 pm 10 | * @copyright www.javamultiplex.com 11 | */ 12 | public class PersonClient { 13 | 14 | @Test 15 | public void shouldCreatePersonObjectWithAllAttributes() { 16 | PersonBuilder personBuilder = new PersonBuilder("Rohit", "Agarwal"); 17 | Person person = personBuilder.withCity("Rudrapur").withAadhar(1234).build(); 18 | assertEquals("Rohit", person.getFirstName()); 19 | assertEquals("Agarwal", person.getLastName()); 20 | assertEquals("Rudrapur", person.getCity()); 21 | assertEquals(1234, person.getAadharId()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/principle/kiss/Example1Test.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.kiss; 2 | 3 | import org.junit.jupiter.api.BeforeEach; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | /** 9 | * @author Rohit Agarwal on 20/08/20 8:56 pm 10 | * @copyright www.javamultiplex.com 11 | */ 12 | public class Example1Test { 13 | private Example1 example1; 14 | 15 | @BeforeEach 16 | void setUp() { 17 | example1 = new Example1(); 18 | } 19 | 20 | @Test 21 | public void shouldCalculateSum1() { 22 | int sum = example1.sum1(10); 23 | assertEquals(55, sum); 24 | } 25 | 26 | @Test 27 | public void shouldCalculateSum2() { 28 | int sum = example1.sum2(10); 29 | assertEquals(55, sum); 30 | } 31 | 32 | @Test 33 | public void shouldCalculateSum3() { 34 | int sum = example1.sum3(10); 35 | assertEquals(55, sum); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/ocp/Product.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.ocp; 2 | 3 | /** 4 | * @author Rohit Agarwal on 16/08/20 2:21 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Product { 8 | private String name; 9 | private Color color; 10 | private Size size; 11 | 12 | public Product(String name, Color color, Size size) { 13 | this.name = name; 14 | this.color = color; 15 | this.size = size; 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | 26 | public Color getColor() { 27 | return color; 28 | } 29 | 30 | public void setColor(Color color) { 31 | this.color = color; 32 | } 33 | 34 | public Size getSize() { 35 | return size; 36 | } 37 | 38 | public void setSize(Size size) { 39 | this.size = size; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/creational/builder/single/example2/CodeClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.builder.single.example2; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | 6 | /** 7 | * @author Rohit Agarwal on 23/08/20 11:08 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class CodeClient { 11 | @Test 12 | public void shouldBuildCode() { 13 | CodeBuilder codeBuilder = new CodeBuilder("Person"); 14 | Code code = codeBuilder.addField("name", "String") 15 | .addField("age", "int") 16 | .build(); 17 | String expected = "public class Person" + System.lineSeparator() + 18 | "{" + System.lineSeparator() + 19 | " public String name;" + System.lineSeparator() + 20 | " public int age;" + System.lineSeparator() + 21 | "}"; 22 | Assertions.assertEquals(expected, code.toString()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/creational/singleton/SingletonV3.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.singleton; 2 | 3 | /** 4 | * @author Rohit Agarwal on 27/08/20 11:59 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class SingletonV3 implements Cloneable { 8 | private static final SingletonV3 INSTANCE = new SingletonV3(); 9 | private int value; 10 | 11 | private SingletonV3() { 12 | if (INSTANCE != null) { 13 | throw new InstantiationError("Object Creation is not allowed."); 14 | } 15 | } 16 | 17 | public static SingletonV3 getInstance() { 18 | return INSTANCE; 19 | } 20 | 21 | public int getValue() { 22 | return value; 23 | } 24 | 25 | public void setValue(int value) { 26 | this.value = value; 27 | } 28 | 29 | @Override 30 | protected Object clone() throws CloneNotSupportedException { 31 | throw new CloneNotSupportedException("Object cloning not supported."); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/creational/singleton/InnerStaticSingletonClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.singleton; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.concurrent.ExecutorService; 6 | import java.util.concurrent.Executors; 7 | 8 | /** 9 | * @author Rohit Agarwal on 29/08/20 9:34 pm 10 | * @copyright www.javamultiplex.com 11 | */ 12 | public class InnerStaticSingletonClient { 13 | @Test 14 | public void shouldNotCreateTwoInstances() { 15 | ExecutorService executorService = Executors.newFixedThreadPool(2); 16 | executorService.execute(() -> { 17 | InnerStaticSingleton innerStaticSingleton = InnerStaticSingleton.getInstance(); 18 | System.out.println(innerStaticSingleton); 19 | }); 20 | executorService.execute(() -> { 21 | InnerStaticSingleton innerStaticSingleton = InnerStaticSingleton.getInstance(); 22 | System.out.println(innerStaticSingleton); 23 | }); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/chainofresponsibility/Dollar10Dispenser.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.chainofresponsibility; 2 | 3 | /** 4 | * @author Rohit Agarwal on 02/09/20 11:19 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Dollar10Dispenser implements DispenseChain { 8 | 9 | private DispenseChain nextChain; 10 | 11 | @Override 12 | public void setNextChain(DispenseChain nextChain) { 13 | this.nextChain = nextChain; 14 | } 15 | 16 | @Override 17 | public void dispense(Currency currency) { 18 | int amount = currency.getAmount(); 19 | if (amount >= 10) { 20 | int num = amount / 10; 21 | int remainder = amount % 10; 22 | System.out.printf("Dispensing %d 10$ notes\n", num); 23 | if (remainder != 0) { 24 | nextChain.dispense(new Currency(remainder)); 25 | } 26 | } else { 27 | nextChain.dispense(currency); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/chainofresponsibility/Dollar20Dispenser.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.chainofresponsibility; 2 | 3 | /** 4 | * @author Rohit Agarwal on 02/09/20 11:19 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Dollar20Dispenser implements DispenseChain { 8 | 9 | private DispenseChain nextChain; 10 | 11 | @Override 12 | public void setNextChain(DispenseChain nextChain) { 13 | this.nextChain = nextChain; 14 | } 15 | 16 | @Override 17 | public void dispense(Currency currency) { 18 | int amount = currency.getAmount(); 19 | if (amount >= 20) { 20 | int num = amount / 20; 21 | int remainder = amount % 20; 22 | System.out.printf("Dispensing %d 20$ notes\n", num); 23 | if (remainder != 0) { 24 | nextChain.dispense(new Currency(remainder)); 25 | } 26 | } else { 27 | nextChain.dispense(currency); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/chainofresponsibility/Dollar50Dispenser.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.chainofresponsibility; 2 | 3 | /** 4 | * @author Rohit Agarwal on 02/09/20 11:19 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Dollar50Dispenser implements DispenseChain { 8 | 9 | private DispenseChain nextChain; 10 | 11 | @Override 12 | public void setNextChain(DispenseChain nextChain) { 13 | this.nextChain = nextChain; 14 | } 15 | 16 | @Override 17 | public void dispense(Currency currency) { 18 | int amount = currency.getAmount(); 19 | if (amount >= 50) { 20 | int num = amount / 50; 21 | int remainder = amount % 50; 22 | System.out.printf("Dispensing %d 50$ notes\n", num); 23 | if (remainder != 0) { 24 | nextChain.dispense(new Currency(remainder)); 25 | } 26 | } else { 27 | nextChain.dispense(currency); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/adapter/README.md: -------------------------------------------------------------------------------- 1 | # Adapter Pattern 2 | 3 | The adapter design pattern is a structural design pattern that allows two unrelated/uncommon interfaces to work together. In other words, the adapter pattern makes two incompatible interfaces compatible without changing their existing code. 4 | 5 | 6 | - **Target** - This defines the domain-specific interface that the client uses. This is the Shape interface in our example. 7 | - **Adapter** - This adapts the interface from the adaptee to the target interface. 8 | - **Adaptee** - This defines an existing interface that needs adapting. This is the GeometricShape interface in our example. 9 | - **Client** - This collaborates with objects conforming to the Target interface. The Drawing class is the client in our example. 10 | 11 | ## UML Diagram 12 | ![Adapter pattern UML diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/structural/uml/Adapter_Design_Pattern_UML.jpg) 13 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/creational/factory/example2/NotificationClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.factory.example2; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | /** 6 | * @author Rohit Agarwal on 25/08/20 12:24 am 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class NotificationClient { 10 | 11 | @Test 12 | public void shouldCreatePushNotification() { 13 | Notification notification = NotificationFactory.createNotification(NotificationChannel.PUSH); 14 | notification.notifyUser(); 15 | } 16 | 17 | @Test 18 | public void shouldCreateSMSNotification() { 19 | Notification notification = NotificationFactory.createNotification(NotificationChannel.SMS); 20 | notification.notifyUser(); 21 | } 22 | 23 | @Test 24 | public void shouldCreateEmailNotification() { 25 | Notification notification = NotificationFactory.createNotification(NotificationChannel.EMAIL); 26 | notification.notifyUser(); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/srp/example2/good/JournalPersistence.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.srp.example2.good; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.nio.file.Files; 6 | import java.nio.file.Paths; 7 | import java.nio.file.StandardOpenOption; 8 | import java.util.List; 9 | 10 | /** 11 | * @author Rohit Agarwal on 16/08/20 3:55 pm 12 | * @copyright www.javamultiplex.com 13 | */ 14 | public class JournalPersistence { 15 | 16 | public void save(String fileName, List entries) throws IOException { 17 | for (String entry : entries) { 18 | save(fileName, entry); 19 | } 20 | } 21 | 22 | public void save(String fileName, String entry) throws IOException { 23 | File file = new File(fileName); 24 | Files.write(file.toPath(), entry.getBytes(), StandardOpenOption.APPEND); 25 | } 26 | 27 | public List load(String fileName) throws IOException { 28 | return Files.readAllLines(Paths.get(fileName)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/structural/adapter/example2/VoltClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.adapter.example2; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | 6 | /** 7 | * @author Rohit Agarwal on 30/08/20 8:00 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class VoltClient { 11 | 12 | @Test 13 | public void shouldGet3VoltSupply() { 14 | SocketAdapter socketAdapter = new SocketObjectAdapterImpl(); 15 | Assertions.assertEquals(3, socketAdapter.get3Volt().getVolts()); 16 | } 17 | 18 | @Test 19 | public void shouldGet12VoltSupply() { 20 | SocketAdapter socketAdapter = new SocketObjectAdapterImpl(); 21 | Assertions.assertEquals(12, socketAdapter.get12Volt().getVolts()); 22 | } 23 | 24 | 25 | @Test 26 | public void shouldGet120VoltSupply() { 27 | SocketAdapter socketAdapter = new SocketObjectAdapterImpl(); 28 | Assertions.assertEquals(120, socketAdapter.get120Volt().getVolts()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/behavioral/visitor/example2/ShoppingCartClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.visitor.example2; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | 6 | /** 7 | * @author Rohit Agarwal on 10/09/20 11:54 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class ShoppingCartClient { 11 | 12 | @Test 13 | public void shouldCalculateTotalPrice() { 14 | ItemElement[] items = new ItemElement[]{new Book(20, "1234"), new Book(100, "5678"), 15 | new Fruit(10, 2, "Banana"), new Fruit(5, 5, "Apple")}; 16 | 17 | int total = calculatePrice(items); 18 | Assertions.assertEquals(160, total); 19 | 20 | } 21 | 22 | private static int calculatePrice(ItemElement[] items) { 23 | ShoppingCartVisitor visitor = new ShoppingCartVisitorImpl(); 24 | int sum = 0; 25 | for (ItemElement item : items) { 26 | sum = sum + item.accept(visitor); 27 | } 28 | return sum; 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/isp/Document.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.isp; 2 | 3 | /** 4 | * @author Rohit Agarwal on 19/08/20 11:30 pm 5 | * @copyright www.javamultiplex.com 6 | */ 7 | public class Document { 8 | private String name; 9 | private String size; 10 | private byte[] content; 11 | 12 | public String getName() { 13 | return name; 14 | } 15 | 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | 20 | public String getSize() { 21 | return size; 22 | } 23 | 24 | public void setSize(String size) { 25 | this.size = size; 26 | } 27 | 28 | public byte[] getContent() { 29 | return content; 30 | } 31 | 32 | public void setContent(byte[] content) { 33 | this.content = content; 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return "Document{" + 39 | "name='" + name + '\'' + 40 | ", size='" + size + '\'' + 41 | '}'; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/composite/README.md: -------------------------------------------------------------------------------- 1 | # Composite Pattern 2 | 3 | Composite pattern is used where we need to treat a group of objects in similar way as a single object. Composite pattern composes objects in term of a tree structure to represent part as well as whole hierarchy. 4 | 5 | We can break the pattern down into: 6 | 7 | - component – is the base interface for all the objects in the composition. It should be either an interface or an abstract class with the common methods to manage the child composites. 8 | - leaf – implements the default behavior of the base component. It doesn't contain a reference to the other objects. 9 | - composite – has leaf elements. It implements the base component methods and defines the child-related operations. 10 | - client – has access to the composition elements by using the base component object. 11 | 12 | ## UML Diagram 13 | 14 | ![Composite pattern UML diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/structural/uml/Composite_Design_Pattern_UML.jpg) 15 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/testing/tddbdd/example1/CalculatorStepdefs.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.tddbdd.example1; 2 | 3 | import io.cucumber.java.en.Given; 4 | import io.cucumber.java.en.Then; 5 | import io.cucumber.java.en.When; 6 | import org.junit.jupiter.api.Assertions; 7 | 8 | /** 9 | * @author Rohit Agarwal on 22/08/20 9:07 pm 10 | * @copyright www.javamultiplex.com 11 | */ 12 | public class CalculatorStepdefs { 13 | 14 | private Calculator calculator; 15 | private int result; 16 | 17 | @Given("You have a calculator") 18 | public void youHaveACalculator() { 19 | calculator = new Calculator(); 20 | } 21 | 22 | @When("Operator is {string}, first number is {int} and second number is {int}") 23 | public void operatorIsFirstNumberIsAndSecondNumberIs(String operator, int number1, int number2) { 24 | result = calculator.calculate(Operator.valueOf(operator), number1, number2); 25 | } 26 | 27 | @Then("The result is {int}") 28 | public void theResultIs(int output) { 29 | Assertions.assertEquals(output, result); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/mediator/README.md: -------------------------------------------------------------------------------- 1 | # Mediator Pattern 2 | 3 | Mediator design pattern is one of the important and widely used behavioral design pattern. Mediator enables decoupling of objects by introducing a layer in between so that the interaction between objects happen via the layer. If the objects interact with each other directly, the system components are tightly-coupled with each other that makes higher maintainability cost and not hard to extend. Mediator pattern focuses on providing a mediator between objects for communication and help in implementing lose-coupling between objects. 4 | 5 | 6 | Air traffic controller is a great example of mediator pattern where the airport control room works as a mediator for communication between different flights. Mediator works as a router between objects and it can have it’s own logic to provide way of communication. 7 | 8 | ## UML Diagram 9 | ![Mediator Pattern UML Diagram](https://github.com/javamultiplex/clean-code-principles-and-patterns/blob/master/src/main/java/com/javamultiplex/pattern/behavioral/uml/Mediator_Design_Pattern_UML.jpg) 10 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/kiss/Example2.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.kiss; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.File; 5 | import java.io.FileReader; 6 | import java.io.IOException; 7 | import java.nio.file.Files; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | /** 12 | * @author Rohit Agarwal on 20/08/20 9:00 pm 13 | * @copyright www.javamultiplex.com 14 | */ 15 | 16 | //Read file and store in list 17 | public class Example2 { 18 | 19 | public List read1(final File file) throws IOException { 20 | List lines = new ArrayList<>(); 21 | try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) { 22 | String line = bufferedReader.readLine(); 23 | while (line != null) { 24 | lines.add(line); 25 | line = bufferedReader.readLine(); 26 | } 27 | } 28 | return lines; 29 | } 30 | 31 | public List read2(final File file) throws IOException { 32 | return Files.readAllLines(file.toPath()); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/principle/delegation/example2/PrinterControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.delegation.example2; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | /** 6 | * @author Rohit Agarwal on 22/08/20 1:24 pm 7 | * @copyright www.javamultiplex.com 8 | */ 9 | public class PrinterControllerTest { 10 | @Test 11 | public void shouldPrintWithCanonPrinter() { 12 | Printer printer = new CanonPrinter(); 13 | PrinterController printerController = new PrinterController(printer); 14 | printerController.print("Hello World"); 15 | } 16 | 17 | @Test 18 | public void shouldPrintWithHPPrinter() { 19 | Printer printer = new HPPrinter(); 20 | PrinterController printerController = new PrinterController(printer); 21 | printerController.print("Hello World"); 22 | } 23 | 24 | @Test 25 | public void shouldPrintWithEpsonPrinter() { 26 | Printer printer = new EpsonPrinter(); 27 | PrinterController printerController = new PrinterController(printer); 28 | printerController.print("Hello World"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/behavioral/state/example1/DeliveryContext.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.behavioral.state.example1; 2 | 3 | public class DeliveryContext { 4 | 5 | private PackageState currentState; 6 | private String packageId; 7 | 8 | public DeliveryContext(PackageState currentState, String packageId) 9 | { 10 | super(); 11 | this.currentState = currentState; 12 | this.packageId = packageId; 13 | 14 | if(currentState == null) { 15 | this.currentState = Acknowledged.instance(); 16 | } 17 | } 18 | 19 | public PackageState getCurrentState() { 20 | return currentState; 21 | } 22 | 23 | public void setCurrentState(PackageState currentState) { 24 | this.currentState = currentState; 25 | } 26 | 27 | public String getPackageId() { 28 | return packageId; 29 | } 30 | 31 | public void setPackageId(String packageId) { 32 | this.packageId = packageId; 33 | } 34 | 35 | public void update() { 36 | currentState.updateState(this); 37 | } 38 | } -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/pattern/structural/flyweight/PlayerFactory.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.structural.flyweight; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * @author Rohit Agarwal on 31/08/20 11:29 pm 8 | * @copyright www.javamultiplex.com 9 | */ 10 | public class PlayerFactory { 11 | private static final Map playerMap = new HashMap<>(); 12 | 13 | public static Player getPlayer(PlayerType type) { 14 | Player p = null; 15 | if (playerMap.containsKey(type)) { 16 | p = playerMap.get(type); 17 | } else { 18 | switch (type) { 19 | case TERRORIST: 20 | p = new Terrorist(); 21 | break; 22 | case COUNTER_TERRORIST: 23 | p = new CounterTerrorist(); 24 | break; 25 | default: 26 | throw new IllegalArgumentException("Player type : " + type + " is invalid"); 27 | } 28 | playerMap.put(type, p); 29 | } 30 | return p; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/testing/principles/example3/DateTimeServiceV2Test.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.testing.principles.example3; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.BeforeEach; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import java.time.LocalDate; 8 | 9 | /** 10 | * @author Rohit Agarwal on 05/09/20 11:32 pm 11 | * @copyright www.javamultiplex.com 12 | */ 13 | public class DateTimeServiceV2Test { 14 | 15 | private DateTimeServiceV2 dateTimeService; 16 | 17 | @BeforeEach 18 | void setUp() { 19 | dateTimeService = new DateTimeServiceV2(); 20 | } 21 | 22 | @Test 23 | public void shouldThrowIllegalArgumentException_LocalDateIsNull() { 24 | Assertions.assertThrows(IllegalArgumentException.class, 25 | () -> dateTimeService.getWeekDay(null)); 26 | } 27 | 28 | @Test 29 | public void shouldGetDayOfWeek() { 30 | LocalDate localDate = LocalDate.of(2020, 6, 11); 31 | String weekDay = dateTimeService.getWeekDay(localDate); 32 | Assertions.assertEquals("Thursday", weekDay); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/creational/builder/multi/PersonClient.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.builder.multi; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | /** 8 | * @author Rohit Agarwal on 23/08/20 9:24 pm 9 | * @copyright www.javamultiplex.com 10 | */ 11 | public class PersonClient { 12 | 13 | @Test 14 | public void shouldCreatePersonObject() { 15 | PersonBuilder personBuilder = new PersonBuilder(); 16 | Person person = personBuilder 17 | .address().streetAddress("Adarsh Colony").city("Rudrapur").postcode("263153") 18 | .job().companyName("FIL").position("SAP").salary(1000) 19 | .build(); 20 | assertEquals("Adarsh Colony", person.getStreetAddress()); 21 | assertEquals("Rudrapur", person.getCity()); 22 | assertEquals("263153", person.getPostcode()); 23 | assertEquals("FIL", person.getCompanyName()); 24 | assertEquals("SAP", person.getPosition()); 25 | assertEquals(1000, person.getAnnualSalary()); 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/principle/solid/isp/bad/NewGenerationPrinterTest.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.isp.bad; 2 | 3 | import com.javamultiplex.principle.solid.isp.Document; 4 | import org.junit.jupiter.api.BeforeEach; 5 | import org.junit.jupiter.api.Test; 6 | 7 | /** 8 | * @author Rohit Agarwal on 19/08/20 11:39 pm 9 | * @copyright www.javamultiplex.com 10 | */ 11 | public class NewGenerationPrinterTest { 12 | 13 | private Machine machine; 14 | private Document document; 15 | 16 | @BeforeEach 17 | void setUp() { 18 | machine = new NewGenerationPrinter(); 19 | document = new Document(); 20 | document.setName("Sample.txt"); 21 | document.setSize("123MB"); 22 | document.setContent("Hello World".getBytes()); 23 | } 24 | 25 | @Test 26 | public void shouldPrintDocument() { 27 | machine.print(document); 28 | } 29 | 30 | @Test 31 | public void shouldScanDocument() { 32 | machine.scan(document); 33 | } 34 | 35 | @Test 36 | public void shouldFaxDocument() { 37 | machine.fax(document); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/javamultiplex/principle/solid/ocp/bad/ProductFilter.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.principle.solid.ocp.bad; 2 | 3 | import com.javamultiplex.principle.solid.ocp.Color; 4 | import com.javamultiplex.principle.solid.ocp.Product; 5 | import com.javamultiplex.principle.solid.ocp.Size; 6 | 7 | import java.util.List; 8 | import java.util.stream.Stream; 9 | 10 | /** 11 | * @author Rohit Agarwal on 16/08/20 2:22 pm 12 | * @copyright www.javamultiplex.com 13 | */ 14 | public class ProductFilter { 15 | 16 | public Stream filterByColor(final List products, final Color color) { 17 | return products.stream().filter(product -> product.getColor() == color); 18 | } 19 | 20 | public Stream filterBySize(final List products, final Size size) { 21 | return products.stream().filter(product -> product.getSize() == size); 22 | } 23 | 24 | public Stream filterByColorAndSize(final List products, final Color color, final Size size) { 25 | return products.stream().filter(product -> product.getColor() == color && product.getSize() == size); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/com/javamultiplex/pattern/creational/singleton/SingletonV4Client.java: -------------------------------------------------------------------------------- 1 | package com.javamultiplex.pattern.creational.singleton; 2 | 3 | import com.javamultiplex.util.SerializationUtil; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.io.IOException; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; 9 | import static org.junit.jupiter.api.Assertions.assertSame; 10 | 11 | /** 12 | * @author Rohit Agarwal on 28/08/20 12:04 am 13 | * @copyright www.javamultiplex.com 14 | */ 15 | public class SingletonV4Client { 16 | 17 | @Test 18 | public void shouldNotCreateTwoInstancesUsingSerialization() throws IOException, ClassNotFoundException { 19 | SingletonV4 singletonV4 = SingletonV4.getInstance(); 20 | singletonV4.setValue(110); 21 | SerializationUtil.serialize(singletonV4, "singleton.ser"); 22 | SingletonV4 another = (SingletonV4) SerializationUtil.deserialize("singleton.ser"); 23 | another.setValue(11); 24 | assertSame(singletonV4, another); 25 | assertEquals(11, singletonV4.getValue()); 26 | assertEquals(11, another.getValue()); 27 | } 28 | } 29 | --------------------------------------------------------------------------------