├── .gitignore ├── README.md ├── pom.xml └── src ├── main └── java │ ├── abstractFactory │ ├── AbstractFactory.java │ ├── AmulIceCreamFactory.java │ ├── Client.java │ ├── DairyQueenCreamFactory.java │ ├── FactoryCreator.java │ ├── abstract-factory-class-diagram.png │ └── iceCream │ │ ├── AmulChocolateIceCream.java │ │ ├── AmulStrawberryIceCream.java │ │ ├── DairyQueenChocolateIceCream.java │ │ ├── DairyQueenStrawberryIceCream.java │ │ └── IceCream.java │ ├── adapter │ ├── Bird.java │ ├── BirdAdapter.java │ ├── Client.java │ ├── PlasticToyDuck.java │ ├── Sparrow.java │ ├── ToyDuck.java │ └── adapter-class-diagram.png │ ├── bridge │ ├── Client.java │ ├── bridge-class-diagram.png │ ├── renderer │ │ ├── RasterRenderer.java │ │ ├── Renderer.java │ │ └── VectorRenderer.java │ └── shape │ │ ├── Circle.java │ │ ├── Rectangle.java │ │ ├── Shape.java │ │ ├── Square.java │ │ └── Triangle.java │ ├── builder │ ├── CodeBuilder.java │ ├── Person.java │ ├── PersonAddressBuilder.java │ ├── PersonBuilder.java │ ├── PersonDemo.java │ ├── PersonOccupancyBuilder.java │ ├── builder-class-diagram.png │ └── builder-example.jpg │ ├── composite │ ├── Circle.java │ ├── Client.java │ ├── GraphicObject.java │ ├── Square.java │ └── composite-class-diagram.png │ ├── decorator │ ├── Circle.java │ ├── Client.java │ ├── ColoredShape.java │ ├── Shape.java │ ├── Square.java │ ├── TransparentShape.java │ └── decorator-class-diagram.png │ ├── factory │ ├── ChocolateIceCream.java │ ├── Client.java │ ├── IceCream.java │ ├── IceCreamFactory.java │ ├── Point.java │ ├── StrawberryIceCream.java │ └── factory-class-diagram.jpg │ ├── prototype │ ├── BlackColor.java │ ├── BlueColor.java │ ├── Client.java │ ├── Color.java │ ├── ColorStore.java │ └── prototype-class-diagram.png │ └── singleton │ ├── Client.java │ ├── Singleton.java │ └── singleton-class-diagram.png └── test └── java ├── abstractFactory └── FactoryCreatorTest.java ├── adapter └── BirdAdapterTest.java ├── builder ├── CodeBuilderTest.java └── PersonBuilderTest.java ├── composite └── GraphicObjectTest.java ├── decorator └── TransparentShapeTest.java ├── factory ├── IceCreamFactoryTest.java └── PointTest.java ├── prototype └── ColorStoreTest.java └── singleton └── SingletonTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | Skip to content 2 | Search or jump to… 3 | 4 | Pull requests 5 | Issues 6 | Marketplace 7 | Explore 8 | 9 | @ankitech 10 | 0 11 | 01ankitech/sfg-pet-clinic 12 | Code Issues 19 Pull requests 0 Actions Projects 1 Wiki Security Insights Settings 13 | sfg-pet-clinic/.gitignore 14 | @ankitech ankitech initial spring project files commit 15 | 06ece79 on Jul 13 16 | 141 lines (118 sloc) 1.83 KB 17 | 18 | ###################### 19 | # Project Specific 20 | ###################### 21 | /build/www/** 22 | /src/test/javascript/coverage/ 23 | /src/test/javascript/PhantomJS*/ 24 | 25 | ###################### 26 | # Node 27 | ###################### 28 | /node/ 29 | node_tmp/ 30 | node_modules/ 31 | npm-debug.log.* 32 | 33 | ###################### 34 | # SASS 35 | ###################### 36 | .sass-cache/ 37 | 38 | ###################### 39 | # Eclipse 40 | ###################### 41 | *.pydevproject 42 | .project 43 | .metadata 44 | tmp/ 45 | tmp/**/* 46 | *.tmp 47 | *.bak 48 | *.swp 49 | *~.nib 50 | local.properties 51 | .classpath 52 | .settings/ 53 | .loadpath 54 | .factorypath 55 | /src/main/resources/rebel.xml 56 | 57 | # External tool builders 58 | .externalToolBuilders/** 59 | 60 | # Locally stored "Eclipse launch configurations" 61 | *.launch 62 | 63 | # CDT-specific 64 | .cproject 65 | 66 | # PDT-specific 67 | .buildpath 68 | 69 | ###################### 70 | # Intellij 71 | ###################### 72 | .idea/ 73 | *.iml 74 | *.iws 75 | *.ipr 76 | *.ids 77 | *.orig 78 | 79 | ###################### 80 | # Visual Studio Code 81 | ###################### 82 | .vscode/ 83 | 84 | ###################### 85 | # Maven 86 | ###################### 87 | /log/ 88 | /target/ 89 | 90 | ###################### 91 | # Gradle 92 | ###################### 93 | .gradle/ 94 | /build/ 95 | 96 | ###################### 97 | # Package Files 98 | ###################### 99 | *.jar 100 | *.war 101 | *.ear 102 | *.db 103 | *.plantuml 104 | 105 | ###################### 106 | # Windows 107 | ###################### 108 | # Windows image file caches 109 | Thumbs.db 110 | 111 | # Folder config file 112 | Desktop.ini 113 | 114 | ###################### 115 | # Mac OSX 116 | ###################### 117 | .DS_Store 118 | .svn 119 | 120 | # Thumbnails 121 | ._* 122 | 123 | # Files that might appear on external disk 124 | .Spotlight-V100 125 | .Trashes 126 | 127 | ###################### 128 | # Directories 129 | ###################### 130 | /bin/ 131 | /deploy/ 132 | 133 | ###################### 134 | # Logs 135 | ###################### 136 | *.log 137 | 138 | ###################### 139 | # Others 140 | ###################### 141 | *.class 142 | *.*~ 143 | *~ 144 | .merge_file* 145 | 146 | ###################### 147 | # Gradle Wrapper 148 | ###################### 149 | !gradle/wrapper/gradle-wrapper.jar 150 | 151 | ###################### 152 | # Maven Wrapper 153 | ###################### 154 | !.mvn/wrapper/maven-wrapper.jar 155 | 156 | ###################### 157 | # ESLint 158 | ###################### 159 | .eslintcache 160 | © 2019 GitHub, Inc. 161 | Terms 162 | Privacy 163 | Security 164 | Status 165 | Help 166 | Contact GitHub 167 | Pricing 168 | API 169 | Training 170 | Blog 171 | About 172 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Design Patterns 2 | + Creational 3 | 1. [Builder pattern](#1builder-pattern) 4 | 2. [Factory pattern](#2factory-pattern) 5 | 3. [Abstract Factory pattern](#3abstract-factory-pattern) 6 | 4. [Prototype pattern](#4prototype-pattern) 7 | 5. [Singleton pattern](#5singleton-pattern) 8 | 9 | + Structural 10 | 6. [Adapter pattern](#6adapter-pattern) 11 | 7. [Bridge pattern](#7bridge-pattern) 12 | 8. [Composite pattern](#8composite-pattern) 13 | 9. [Decorator pattern](#9decorator-pattern) 14 | 10. [Facade pattern](#10facade-pattern) 15 | 11. [Flyweight pattern](#11flyweight-pattern) 16 | 12. [Proxy pattern](#12proxy-pattern) 17 | 18 | + Behavioral 19 | 13. [Chain of responsibility pattern](#13chain-of-responsibility-pattern) 20 | 14. [Command pattern](#14command-pattern) 21 | 15. [Interpreter pattern](#15interpreter-pattern) 22 | 16. [Iterator pattern](#16iterator-pattern) 23 | 17. [Mediator pattern](#17mediator-pattern) 24 | 18. [Memento pattern](#18memento-pattern) 25 | 19. [Observer pattern](#19observer-pattern) 26 | 20. [State pattern](#20state-pattern) 27 | 21. [Strategy pattern](#21strategy-pattern) 28 | 22. [Template pattern](#22template-pattern) 29 | 23. [Visitor pattern](#23visitor-pattern) 30 | 31 | ## 1.Builder Pattern 32 | **Builder pattern** aims to “Separate the construction of a complex object from its representation so that the same 33 | construction process can create different representations.” It is used to construct a complex object step by step 34 | and the final step will return the object. The process of constructing an object should be generic so that it can 35 | be used to create different representations of the same object. 36 | ![UML](https://media.geeksforgeeks.org/wp-content/uploads/uml-of-builedr.jpg) 37 | 38 | 39 | ![Builder pattern class Diagram](https://github.com/ankitech/design-pattern/blob/master/src/main/java/builder/builder-class-diagram.png) 40 | UML diagram for the classes in code example: [src.main.java.builder](https://github.com/ankitech/design-pattern/tree/master/src/main/java/builder) 41 | 42 | **For example** : if we want to generate a builder for house **(House Builder)** we House a House builder which can act 43 | as a facade for multiple concrete builder classes which can be part of the house builder interface. 44 | ![Builder example](https://github.com/ankitech/design-pattern/blob/master/src/main/java/builder/builder-example.jpg) 45 | 46 | ## 2.Factory Pattern 47 | Factory design pattern is used when we have a super class with multiple sub-classes and based on input, we 48 | need to return one of the sub-class. This pattern take out the responsibility of instantiation of a class from 49 | client program to the factory class.Factory pattern introduces loose coupling between classes which is the most 50 | important principle one should consider and apply while designing the application architecture. Loose coupling can 51 | be introduced in application architecture by programming against abstract entities rather than concrete implementations. 52 | ![UML](https://www.tutorialspoint.com/design_pattern/images/factory_pattern_uml_diagram.jpg) 53 | 54 | ![Factory pattern class Diagram](https://github.com/ankitech/design-pattern/blob/master/src/main/java/factory/factory-class-diagram.jpg) 55 | UML diagram for the classes in code example: [src.main.java.factory](https://github.com/ankitech/design-pattern/tree/master/src/main/java/factory) 56 | 57 | ## 3.Abstract Factory Pattern 58 | **Abstract Factory design** pattern is one of the _Creational_ pattern. Abstract Factory pattern is almost similar to 59 | Factory Pattern is considered as another layer of abstraction over factory pattern. Abstract Factory patterns 60 | work around a **_super-factory_** which creates other factories. Abstract factory pattern implementation provides us a 61 | framework that allows us to create objects that follow a general pattern. So at runtime, abstract factory is 62 | coupled with any desired concrete factory which can create objects of desired type. 63 | ![UML](https://media.geeksforgeeks.org/wp-content/uploads/AbstractFactoryPattern-2.png) 64 | 65 | ![Abstract Factory pattern class Diagram](https://github.com/ankitech/design-pattern/blob/master/src/main/java/abstractFactory/abstract-factory-class-diagram.png) 66 | UML diagram for the classes in code example: [src.main.java.abstractFactory](https://github.com/ankitech/design-pattern/tree/master/src/main/java/abstractFactory) 67 | 68 | ## 4.Prototype Pattern 69 | **Prototype** allows us to hide the complexity of making new instances from the client. The concept is to copy an existing 70 | object rather than creating a new instance from scratch, something that may include costly operations. The existing 71 | object acts as a _prototype_ and contains the state of the object. The newly copied object may change same properties 72 | only if required. This approach saves costly resources and time, especially when the object creation is a heavy process. 73 | The prototype pattern is a _creational_ design pattern. Prototype patterns is required, when object creation is time consuming, 74 | and costly operation, so we create object with existing object itself. One of the best available way to create object 75 | from existing objects are `clone()` method. Clone is the simplest approach to implement prototype pattern. However, 76 | it is your call to decide how to copy existing object based on your business model. 77 | ![UML](https://media.geeksforgeeks.org/wp-content/uploads/download-1.png) 78 | 79 | ![Prototype pattern class Diagram](https://github.com/ankitech/design-pattern/blob/master/src/main/java/prototype/prototype-class-diagram.png) 80 | UML diagram for the classes in code example: [src.main.java.prototype](https://github.com/ankitech/design-pattern/tree/master/src/main/java/prototype) 81 | 82 | ## 5.Singleton Pattern 83 | The **singleton pattern** is one of the simplest design patterns. Sometimes we need to have only one instance of our class 84 | for example a single DB connection shared by multiple objects as creating a separate DB connection for every object may 85 | be costly. Similarly, there can be a single configuration manager or error manager in an application that handles 86 | all problems instead of creating multiple managers._The singleton pattern is a design pattern that restricts the 87 | instantiation of a class to one object._ 88 | 89 | ![singleton pattern class Diagram](https://github.com/ankitech/design-pattern/blob/master/src/main/java/singleton/singleton-class-diagram.png) 90 | UML diagram for the classes in code example: [src.main.java.singleton](https://github.com/ankitech/design-pattern/tree/master/src/main/java/singleton) 91 | 92 | ## 6.Adapter Pattern 93 | The **adapter pattern** convert the interface of a class into another interface clients expect. Adapter lets 94 | classes work together that couldn’t otherwise because of incompatible interfaces. The client sees only the target 95 | interface and not the adapter. The adapter implements the target interface. Adapter delegates all requests to Adaptee. 96 | For example consider a USB to Ethernet adapter. We need this when we have an Ethernet interface on one end and 97 | USB on the other. Since they are incompatible with each other. we use an adapter that converts one to other. 98 | ![UML](https://media.geeksforgeeks.org/wp-content/uploads/classDiagram.jpg) 99 | 100 | ![Adapter pattern class Diagram](https://github.com/ankitech/design-pattern/blob/master/src/main/java/adapter/adapter-class-diagram.png) 101 | UML diagram for the classes in code example: [src.main.java.adapter](https://github.com/ankitech/design-pattern/tree/master/src/main/java/adapter) 102 | 103 | ## 7.Bridge Pattern 104 | The Bridge design pattern allows you to separate the abstraction from the implementation.It is a structural design pattern. 105 | There are 2 parts in Bridge design pattern : 106 | - Abstraction 107 | - Implementation 108 | 109 | This is a design mechanism that encapsulates an implementation class inside of an interface class. 110 | 111 | - The bridge pattern allows the Abstraction and the Implementation to be developed independently and the client code 112 | can access only the Abstraction part without being concerned about the Implementation part. 113 | - The abstraction is an interface or abstract class and the implementor is also an interface or abstract class. 114 | - The abstraction contains a reference to the implementor. Children of the abstraction are referred to as refined 115 | bstractions, and children of the implementor are concrete implementors. Since we can change the reference to the implementor 116 | in the abstraction, we are able to change the abstraction’s implementor at run-time. Changes to the implementor do not affect client code. 117 | - It increases the loose coupling between class abstraction and it’s implementation. 118 | ![UML](https://media.geeksforgeeks.org/wp-content/uploads/Bridge_Design.png) 119 | 120 | ![Bridge pattern class Diagram](https://github.com/ankitech/design-pattern/blob/master/src/main/java/bridge/bridge-class-diagram.png) 121 | UML diagram for the classes in code example: [src.main.java.bridge](https://github.com/ankitech/design-pattern/tree/master/src/main/java/bridge) 122 | 123 | ## 8.Composite Pattern 124 | _Composite pattern_ is a partitioning design pattern and describes a group of objects that is treated the same way as 125 | a single instance of the same type of object. The intent of a composite is to “compose” objects into tree structures 126 | to represent part-whole hierarchies. It allows you to have a tree structure and ask each node in the tree structure to perform a task. 127 | 128 | As described by Gof, **_“Compose objects into tree structure to represent part-whole hierarchies. Composite lets 129 | client treat individual objects and compositions of objects uniformly”_**. 130 | When dealing with Tree-structured data, programmers often have to discriminate between a leaf-node and a branch. 131 | This makes code more complex, and therefore, error prone. The solution is an interface that allows treating complex 132 | nd primitive objects uniformly. 133 | In object-oriented programming, a composite is an object designed as a composition of one-or-more similar objects, 134 | all exhibiting similar functionality. This is known as a “has-a” relationship between objects. 135 | The key concept is that you can manipulate a single instance of the object just as you would manipulate a group of them. 136 | The operations you can perform on all the composite objects often have a least common denominator relationship. 137 | 138 | ![Composite pattern class Diagram](https://github.com/ankitech/design-pattern/blob/master/src/main/java/composite/composite-class-diagram.png) 139 | UML diagram for the classes in code example: [src.main.java.composite](https://github.com/ankitech/design-pattern/tree/master/src/main/java/composite) 140 | 141 | ## 9.Decorator Pattern 142 | _Decorator pattern_ allows a user to add new functionality to an existing object without altering its structure. 143 | This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class. 144 | This pattern creates a decorator class which wraps the original class and provides additional functionality keeping 145 | class methods signature intact. 146 | We are demonstrating the use of decorator pattern via following example in which we will decorate a shape with some 147 | color without alter shape class. 148 | 149 | ![Decorator pattern class Diagram](https://github.com/ankitech/design-pattern/blob/master/src/main/java/decorator/decorator-class-diagram.png) 150 | UML diagram for the classes in code example: [src.main.java.decorator](https://github.com/ankitech/design-pattern/tree/master/src/main/java/decorator) 151 | 152 | ## 10.Facade Pattern 153 | 154 | ## 11.Flyweight Pattern 155 | 156 | ## 12.Proxy Pattern 157 | 158 | ## 13.Chain Of Responsibility Pattern 159 | 160 | ## 14.Command Pattern 161 | 162 | ## 15.Interpreter Pattern 163 | 164 | ## 16.Iterator Pattern 165 | 166 | ## 17.Mediator Pattern 167 | 168 | ## 18.Memento Pattern 169 | 170 | ## 19.Observer Pattern 171 | 172 | ## 20.State Pattern 173 | 174 | ## 21.Strategy Pattern 175 | 176 | ## 22.Template Pattern 177 | 178 | ## 23.Visitor Pattern 179 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.ankitech 8 | design-pattern 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | org.apache.maven.plugins 14 | maven-compiler-plugin 15 | 16 | 8 17 | 8 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | org.junit.jupiter 26 | junit-jupiter-api 27 | 5.5.2 28 | test 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/main/java/abstractFactory/AbstractFactory.java: -------------------------------------------------------------------------------- 1 | package abstractFactory; 2 | 3 | import abstractFactory.iceCream.IceCream; 4 | 5 | public interface AbstractFactory { 6 | IceCream createIceCream(String flavor); 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/abstractFactory/AmulIceCreamFactory.java: -------------------------------------------------------------------------------- 1 | package abstractFactory; 2 | 3 | import abstractFactory.iceCream.AmulChocolateIceCream; 4 | import abstractFactory.iceCream.AmulStrawberryIceCream; 5 | import abstractFactory.iceCream.IceCream; 6 | 7 | public class AmulIceCreamFactory implements AbstractFactory{ 8 | 9 | public IceCream createIceCream(String choice){ 10 | if (choice.equals("chocolate")){ 11 | return new AmulChocolateIceCream(); 12 | }else { 13 | return new AmulStrawberryIceCream(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/abstractFactory/Client.java: -------------------------------------------------------------------------------- 1 | package abstractFactory; 2 | 3 | import abstractFactory.iceCream.IceCream; 4 | 5 | public class Client { 6 | public static void main(String[] args) { 7 | 8 | FactoryCreator factoryCreator = new FactoryCreator(); 9 | 10 | AbstractFactory amul = factoryCreator.getFactory("amul"); 11 | IceCream choco = amul.createIceCream("chocolate"); 12 | 13 | AbstractFactory dairyQueen = factoryCreator.getFactory("dairyQueen"); 14 | IceCream strawberry = dairyQueen.createIceCream("strawberry"); 15 | 16 | System.out.printf("Chocolate icecream :\n name - %s\n calories - %d\n cost - %d\n",choco.getIceCreamName(),choco.getCalories(),choco.getCost()); 17 | System.out.printf("Strawberry icecream :\n name - %s\n calories - %d\n cost - %d\n",strawberry.getIceCreamName(),strawberry.getCalories(),strawberry.getCost()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/abstractFactory/DairyQueenCreamFactory.java: -------------------------------------------------------------------------------- 1 | package abstractFactory; 2 | 3 | import abstractFactory.iceCream.DairyQueenChocolateIceCream; 4 | import abstractFactory.iceCream.DairyQueenStrawberryIceCream; 5 | import abstractFactory.iceCream.IceCream; 6 | 7 | public class DairyQueenCreamFactory implements AbstractFactory{ 8 | 9 | public IceCream createIceCream(String choice){ 10 | if (choice.equals("chocolate")){ 11 | return new DairyQueenChocolateIceCream(); 12 | }else { 13 | return new DairyQueenStrawberryIceCream(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/abstractFactory/FactoryCreator.java: -------------------------------------------------------------------------------- 1 | package abstractFactory; 2 | 3 | public class FactoryCreator { 4 | 5 | public AbstractFactory getFactory(String brand){ 6 | if(brand.equals("amul")){ 7 | return new AmulIceCreamFactory(); 8 | }else { 9 | return new DairyQueenCreamFactory(); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/abstractFactory/abstract-factory-class-diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitech/design-pattern/1b0589cfb2b201c780ee40b9028874d4f27e9836/src/main/java/abstractFactory/abstract-factory-class-diagram.png -------------------------------------------------------------------------------- /src/main/java/abstractFactory/iceCream/AmulChocolateIceCream.java: -------------------------------------------------------------------------------- 1 | package abstractFactory.iceCream; 2 | 3 | public class AmulChocolateIceCream implements IceCream { 4 | 5 | @Override 6 | public String getIceCreamName() { 7 | return "Amul Chocolate"; 8 | } 9 | 10 | @Override 11 | public Integer getCalories() { 12 | return 200; 13 | } 14 | 15 | @Override 16 | public Integer getCost() { 17 | return 30; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/abstractFactory/iceCream/AmulStrawberryIceCream.java: -------------------------------------------------------------------------------- 1 | package abstractFactory.iceCream; 2 | 3 | public class AmulStrawberryIceCream implements IceCream { 4 | @Override 5 | public String getIceCreamName() { 6 | return "Amul Strawberry"; 7 | } 8 | 9 | @Override 10 | public Integer getCalories() { 11 | return 150; 12 | } 13 | 14 | @Override 15 | public Integer getCost() { 16 | return 20; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/abstractFactory/iceCream/DairyQueenChocolateIceCream.java: -------------------------------------------------------------------------------- 1 | package abstractFactory.iceCream; 2 | 3 | public class DairyQueenChocolateIceCream implements IceCream { 4 | 5 | @Override 6 | public String getIceCreamName() { 7 | return "Dairy Queen Chocolate"; 8 | } 9 | 10 | @Override 11 | public Integer getCalories() { 12 | return 200; 13 | } 14 | 15 | @Override 16 | public Integer getCost() { 17 | return 30; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/abstractFactory/iceCream/DairyQueenStrawberryIceCream.java: -------------------------------------------------------------------------------- 1 | package abstractFactory.iceCream; 2 | 3 | public class DairyQueenStrawberryIceCream implements IceCream { 4 | @Override 5 | public String getIceCreamName() { 6 | return "Dairy Queen Strawberry"; 7 | } 8 | 9 | @Override 10 | public Integer getCalories() { 11 | return 150; 12 | } 13 | 14 | @Override 15 | public Integer getCost() { 16 | return 20; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/abstractFactory/iceCream/IceCream.java: -------------------------------------------------------------------------------- 1 | package abstractFactory.iceCream; 2 | 3 | public interface IceCream { 4 | 5 | String getIceCreamName(); 6 | Integer getCalories(); 7 | Integer getCost(); 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/adapter/Bird.java: -------------------------------------------------------------------------------- 1 | package adapter; 2 | 3 | interface Bird 4 | { 5 | // birds implement Bird interface that allows 6 | // them to fly and make sounds adaptee interface 7 | public String fly(); 8 | public String makeSound(); 9 | } -------------------------------------------------------------------------------- /src/main/java/adapter/BirdAdapter.java: -------------------------------------------------------------------------------- 1 | package adapter; 2 | 3 | class BirdAdapter implements ToyDuck 4 | { 5 | // You need to implement the interface your 6 | // client expects to use. 7 | Bird bird; 8 | public BirdAdapter(Bird bird) 9 | { 10 | // we need reference to the object we 11 | // are adapting 12 | this.bird = bird; 13 | } 14 | 15 | public String squeak() 16 | { 17 | // translate the methods appropriately 18 | return bird.makeSound(); 19 | } 20 | } -------------------------------------------------------------------------------- /src/main/java/adapter/Client.java: -------------------------------------------------------------------------------- 1 | package adapter; 2 | 3 | class Client 4 | { 5 | public static void main(String args[]) 6 | { 7 | Sparrow sparrow = new Sparrow(); 8 | ToyDuck toyDuck = new PlasticToyDuck(); 9 | 10 | // Wrap a bird in a birdAdapter so that it 11 | // behaves like toy duck 12 | ToyDuck birdAdapter = new BirdAdapter(sparrow); 13 | 14 | System.out.println("Sparrow..."); 15 | System.out.println(sparrow.fly()); 16 | System.out.println(sparrow.makeSound()); 17 | 18 | System.out.println("ToyDuck..."); 19 | System.out.println(toyDuck.squeak()); 20 | 21 | // toy duck behaving like a bird 22 | System.out.println("BirdAdapter..."); 23 | System.out.println(birdAdapter.squeak()); 24 | } 25 | } -------------------------------------------------------------------------------- /src/main/java/adapter/PlasticToyDuck.java: -------------------------------------------------------------------------------- 1 | package adapter; 2 | 3 | class PlasticToyDuck implements ToyDuck 4 | { 5 | public String squeak() 6 | { 7 | return "Squeak"; 8 | } 9 | } -------------------------------------------------------------------------------- /src/main/java/adapter/Sparrow.java: -------------------------------------------------------------------------------- 1 | package adapter; 2 | 3 | class Sparrow implements Bird 4 | { 5 | // a concrete implementation of bird 6 | public String fly() 7 | { 8 | return "Flying"; 9 | } 10 | public String makeSound() 11 | { 12 | return "Chirp Chirp"; 13 | } 14 | } -------------------------------------------------------------------------------- /src/main/java/adapter/ToyDuck.java: -------------------------------------------------------------------------------- 1 | package adapter; 2 | 3 | interface ToyDuck 4 | { 5 | // target interface 6 | // toyducks dont fly they just make 7 | // squeaking sound 8 | public String squeak(); 9 | } -------------------------------------------------------------------------------- /src/main/java/adapter/adapter-class-diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitech/design-pattern/1b0589cfb2b201c780ee40b9028874d4f27e9836/src/main/java/adapter/adapter-class-diagram.png -------------------------------------------------------------------------------- /src/main/java/bridge/Client.java: -------------------------------------------------------------------------------- 1 | package bridge; 2 | 3 | import bridge.renderer.RasterRenderer; 4 | import bridge.renderer.Renderer; 5 | import bridge.renderer.VectorRenderer; 6 | import bridge.shape.Shape; 7 | import bridge.shape.Square; 8 | import bridge.shape.Triangle; 9 | 10 | public class Client { 11 | 12 | public static void main(String[] args) { 13 | 14 | Renderer vector = new VectorRenderer(); 15 | Renderer raster = new RasterRenderer(); 16 | 17 | Shape vectorTriangle = new Triangle(vector); 18 | Shape rasterSquare = new Square(raster); 19 | 20 | System.out.println(vectorTriangle); 21 | System.out.println(rasterSquare); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/bridge/bridge-class-diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitech/design-pattern/1b0589cfb2b201c780ee40b9028874d4f27e9836/src/main/java/bridge/bridge-class-diagram.png -------------------------------------------------------------------------------- /src/main/java/bridge/renderer/RasterRenderer.java: -------------------------------------------------------------------------------- 1 | package bridge.renderer; 2 | 3 | public class RasterRenderer implements Renderer { 4 | @Override 5 | public String whatToRenderAs() { 6 | return "pixels"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/bridge/renderer/Renderer.java: -------------------------------------------------------------------------------- 1 | package bridge.renderer; 2 | 3 | public interface Renderer { 4 | public String whatToRenderAs(); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/bridge/renderer/VectorRenderer.java: -------------------------------------------------------------------------------- 1 | package bridge.renderer; 2 | 3 | public class VectorRenderer implements Renderer { 4 | @Override 5 | public String whatToRenderAs() { 6 | return "lines"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/bridge/shape/Circle.java: -------------------------------------------------------------------------------- 1 | package bridge.shape; 2 | 3 | import bridge.renderer.Renderer; 4 | 5 | public class Circle extends Shape { 6 | public Circle(Renderer renderer) { 7 | super(renderer); 8 | } 9 | 10 | @Override 11 | public String getName() { 12 | return "Circle"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/bridge/shape/Rectangle.java: -------------------------------------------------------------------------------- 1 | package bridge.shape; 2 | 3 | import bridge.renderer.Renderer; 4 | 5 | public class Rectangle extends Shape { 6 | public Rectangle(Renderer renderer) { 7 | super(renderer); 8 | } 9 | 10 | @Override 11 | public String getName() { 12 | return "Rectangle"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/bridge/shape/Shape.java: -------------------------------------------------------------------------------- 1 | package bridge.shape; 2 | 3 | import bridge.renderer.Renderer; 4 | 5 | public abstract class Shape { 6 | 7 | Renderer renderer; 8 | 9 | public Shape(Renderer renderer) { 10 | this.renderer = renderer; 11 | } 12 | 13 | public abstract String getName(); 14 | 15 | @Override 16 | public String toString() { 17 | return String.format("Drawing %s as %s",getName(),renderer.whatToRenderAs()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/bridge/shape/Square.java: -------------------------------------------------------------------------------- 1 | package bridge.shape; 2 | 3 | import bridge.renderer.Renderer; 4 | 5 | public class Square extends Shape { 6 | public Square(Renderer renderer) { 7 | super(renderer); 8 | } 9 | 10 | @Override 11 | public String getName() { 12 | return "Square"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/bridge/shape/Triangle.java: -------------------------------------------------------------------------------- 1 | package bridge.shape; 2 | 3 | import bridge.renderer.Renderer; 4 | 5 | public class Triangle extends Shape { 6 | 7 | public Triangle(Renderer renderer) { 8 | super(renderer); 9 | } 10 | 11 | @Override 12 | public String getName() { 13 | return "Triangle"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/builder/CodeBuilder.java: -------------------------------------------------------------------------------- 1 | package builder; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class CodeBuilder { 7 | 8 | String className; 9 | Map instamceVeriables = new HashMap<>(); 10 | public CodeBuilder(String className) { 11 | this.className = className; 12 | } 13 | 14 | public CodeBuilder addField(String name, String value){ 15 | this.instamceVeriables.put(name, value); 16 | return this; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | StringBuilder cb = new StringBuilder(); 22 | cb.append("public class "+this.className+"\n{\n"); 23 | this.instamceVeriables.keySet().forEach( key -> cb.append(" public "+instamceVeriables.get(key)+" "+key+";\n")); 24 | cb.append("}"); 25 | return cb.toString(); 26 | } 27 | 28 | public static void main(String[] args) { 29 | CodeBuilder cb = new CodeBuilder("Person").addField("name","String").addField("age","int"); 30 | System.out.println(cb); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/builder/Person.java: -------------------------------------------------------------------------------- 1 | package builder; 2 | 3 | public class Person extends PersonDemo { 4 | public String name,city,pincode; 5 | public String company,position; 6 | public int salary; 7 | 8 | @Override 9 | public String toString() { 10 | return "Person{" + 11 | "name='" + name + '\'' + 12 | ", city='" + city + '\'' + 13 | ", pincode='" + pincode + '\'' + 14 | ", company='" + company + '\'' + 15 | ", position='" + position + '\'' + 16 | ", salary=" + salary + 17 | '}'; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/builder/PersonAddressBuilder.java: -------------------------------------------------------------------------------- 1 | package builder; 2 | 3 | public class PersonAddressBuilder extends PersonBuilder{ 4 | 5 | public PersonAddressBuilder(Person person) { 6 | this.person = person; 7 | } 8 | 9 | public PersonAddressBuilder name(String name){ 10 | person.name = name; 11 | return this; 12 | } 13 | 14 | public PersonAddressBuilder inCity(String city){ 15 | person.city = city; 16 | return this; 17 | } 18 | 19 | public PersonAddressBuilder atPincode(String pincode){ 20 | person.pincode = pincode; 21 | return this; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/builder/PersonBuilder.java: -------------------------------------------------------------------------------- 1 | package builder; 2 | 3 | public class PersonBuilder { 4 | 5 | protected Person person = new Person(); 6 | 7 | public Person build(){ 8 | return person; 9 | } 10 | 11 | public PersonAddressBuilder lives(){ 12 | return new PersonAddressBuilder(person); 13 | } 14 | 15 | public PersonOccupancyBuilder works(){ 16 | return new PersonOccupancyBuilder(person); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/builder/PersonDemo.java: -------------------------------------------------------------------------------- 1 | package builder; 2 | 3 | public class PersonDemo { 4 | 5 | public static void main(String[] args) { 6 | PersonBuilder pb = new PersonBuilder(); 7 | 8 | Person person = pb 9 | .lives() 10 | .name("Ankit") 11 | .inCity("Pune") 12 | .atPincode("427001") 13 | .works() 14 | .worksAt("Ideas") 15 | .worksAs("Developer") 16 | .paid(5000) 17 | .build(); 18 | 19 | System.out.println(person); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/builder/PersonOccupancyBuilder.java: -------------------------------------------------------------------------------- 1 | package builder; 2 | 3 | public class PersonOccupancyBuilder extends PersonBuilder{ 4 | 5 | public PersonOccupancyBuilder(Person person){ 6 | this.person = person; 7 | } 8 | 9 | public PersonOccupancyBuilder worksAt(String company){ 10 | person.company = company; 11 | return this; 12 | } 13 | 14 | public PersonOccupancyBuilder worksAs(String position){ 15 | person.position = position; 16 | return this; 17 | } 18 | 19 | public PersonOccupancyBuilder paid(int salary){ 20 | person.salary = salary; 21 | return this; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/builder/builder-class-diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitech/design-pattern/1b0589cfb2b201c780ee40b9028874d4f27e9836/src/main/java/builder/builder-class-diagram.png -------------------------------------------------------------------------------- /src/main/java/builder/builder-example.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitech/design-pattern/1b0589cfb2b201c780ee40b9028874d4f27e9836/src/main/java/builder/builder-example.jpg -------------------------------------------------------------------------------- /src/main/java/composite/Circle.java: -------------------------------------------------------------------------------- 1 | package composite; 2 | 3 | public class Circle extends GraphicObject { 4 | 5 | public Circle(String color) { 6 | name = "Circle"; 7 | this.color = color; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/composite/Client.java: -------------------------------------------------------------------------------- 1 | package composite; 2 | 3 | public class Client { 4 | 5 | public static void main(String[] args) { 6 | 7 | GraphicObject drawing = new GraphicObject(); 8 | drawing.setName("My Drawing"); 9 | drawing.children.add(new Square("Red")); 10 | drawing.children.add(new Circle("Green")); 11 | 12 | GraphicObject group = new GraphicObject(); 13 | group.children.add(new Square("Blue")); 14 | group.children.add(new Circle("Blue")); 15 | 16 | drawing.children.add(group); 17 | 18 | System.out.println(drawing); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/composite/GraphicObject.java: -------------------------------------------------------------------------------- 1 | package composite; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.List; 6 | 7 | public class GraphicObject { 8 | 9 | protected String name = "Group"; 10 | public String color; 11 | public List children = new ArrayList<>(); 12 | 13 | public String getName() { 14 | return name; 15 | } 16 | 17 | public void setName(String name) { 18 | this.name = name; 19 | } 20 | 21 | private void print(StringBuilder stringBuilder, int depth){ 22 | 23 | stringBuilder.append(String.join("", Collections.nCopies(depth, " *"))) 24 | .append(depth > 0 ? " ": "") 25 | .append((color == null || color.isEmpty()) ? " ": color + " ") 26 | .append(getName()) 27 | .append(System.lineSeparator()); 28 | 29 | for (GraphicObject child: children) { 30 | child.print(stringBuilder, depth+1); 31 | } 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | 37 | StringBuilder sb = new StringBuilder(); 38 | print(sb, 0); 39 | return sb.toString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/composite/Square.java: -------------------------------------------------------------------------------- 1 | package composite; 2 | 3 | public class Square extends GraphicObject { 4 | 5 | public Square(String color) { 6 | 7 | name = "Square"; 8 | this.color = color; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/composite/composite-class-diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitech/design-pattern/1b0589cfb2b201c780ee40b9028874d4f27e9836/src/main/java/composite/composite-class-diagram.png -------------------------------------------------------------------------------- /src/main/java/decorator/Circle.java: -------------------------------------------------------------------------------- 1 | package decorator; 2 | 3 | public class Circle implements Shape { 4 | 5 | private String name = "Circle"; 6 | private int radius; 7 | 8 | public Circle(int radius) { 9 | this.radius = radius; 10 | } 11 | 12 | @Override 13 | public String info() { 14 | return "This is a "+this.name +" of radius "+this.radius; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/decorator/Client.java: -------------------------------------------------------------------------------- 1 | package decorator; 2 | 3 | public class Client { 4 | 5 | public static void main(String[] args) { 6 | 7 | Circle circle = new Circle(10); 8 | System.out.println(circle.info()); 9 | 10 | ColoredShape blueSquare = new ColoredShape( 11 | new Square(12),"blue" 12 | ); 13 | System.out.println(blueSquare.info()); 14 | 15 | TransparentShape halfTransparentCircle = new TransparentShape( 16 | new Circle(40),50 17 | ); 18 | System.out.println(halfTransparentCircle.info()); 19 | 20 | 21 | TransparentShape red90TransparentCircle = new TransparentShape( 22 | new ColoredShape( 23 | new Circle(36), "Red" 24 | ),90 25 | ); 26 | 27 | System.out.println(red90TransparentCircle.info()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/decorator/ColoredShape.java: -------------------------------------------------------------------------------- 1 | package decorator; 2 | 3 | public class ColoredShape implements Shape { 4 | 5 | private Shape shape; 6 | private String color; 7 | 8 | public ColoredShape(Shape shape, String color) { 9 | this.shape = shape; 10 | this.color = color; 11 | } 12 | 13 | @Override 14 | public String info() { 15 | return shape.info()+ " and color "+this.color; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/decorator/Shape.java: -------------------------------------------------------------------------------- 1 | package decorator; 2 | 3 | public interface Shape { 4 | 5 | String info(); 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/decorator/Square.java: -------------------------------------------------------------------------------- 1 | package decorator; 2 | 3 | public class Square implements Shape { 4 | 5 | private String name = "Square"; 6 | private int side; 7 | 8 | public Square(int side) { 9 | this.side = side; 10 | } 11 | 12 | @Override 13 | public String info() { 14 | return "This is a "+this.name +" of side "+this.side; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/decorator/TransparentShape.java: -------------------------------------------------------------------------------- 1 | package decorator; 2 | 3 | public class TransparentShape implements Shape { 4 | 5 | private Shape shape; 6 | private int transperancy; 7 | 8 | public TransparentShape(Shape shape, int transperancy) { 9 | this.shape = shape; 10 | this.transperancy = transperancy; 11 | } 12 | 13 | @Override 14 | public String info() { 15 | return shape.info()+ " and tranceperancy "+this.transperancy; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/decorator/decorator-class-diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitech/design-pattern/1b0589cfb2b201c780ee40b9028874d4f27e9836/src/main/java/decorator/decorator-class-diagram.png -------------------------------------------------------------------------------- /src/main/java/factory/ChocolateIceCream.java: -------------------------------------------------------------------------------- 1 | package factory; 2 | 3 | public class ChocolateIceCream implements IceCream { 4 | 5 | @Override 6 | public String getIceCreamName() { 7 | return "Chocolate"; 8 | } 9 | 10 | @Override 11 | public Integer getCalories() { 12 | return 200; 13 | } 14 | 15 | @Override 16 | public Integer getCost() { 17 | return 30; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/factory/Client.java: -------------------------------------------------------------------------------- 1 | package factory; 2 | 3 | public class Client { 4 | 5 | public static void main(String[] args) { 6 | 7 | IceCreamFactory iceCreamFactory = new IceCreamFactory(); 8 | 9 | IceCream choco = iceCreamFactory.createIceCream("chocolate"); 10 | IceCream strawberry = iceCreamFactory.createIceCream("strawberry"); 11 | 12 | System.out.printf("Chocolate icecream :\n name - %s\n calories - %d\n cost - %d\n",choco.getIceCreamName(),choco.getCalories(),choco.getCost()); 13 | System.out.printf("Strawberry icecream :\n name - %s\n calories - %d\n cost - %d\n",strawberry.getIceCreamName(),strawberry.getCalories(),strawberry.getCost()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/factory/IceCream.java: -------------------------------------------------------------------------------- 1 | package factory; 2 | 3 | public interface IceCream { 4 | 5 | String getIceCreamName(); 6 | Integer getCalories(); 7 | Integer getCost(); 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/factory/IceCreamFactory.java: -------------------------------------------------------------------------------- 1 | package factory; 2 | 3 | public class IceCreamFactory { 4 | public IceCream createIceCream(String choice){ 5 | if (choice.equals("chocolate")){ 6 | return new ChocolateIceCream(); 7 | }else { 8 | return new StrawberryIceCream(); 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/factory/Point.java: -------------------------------------------------------------------------------- 1 | package factory; 2 | 3 | public class Point { 4 | 5 | private double x,y; 6 | 7 | private Point(double x,double y){ 8 | this.x = x; 9 | this.y = y; 10 | } 11 | 12 | public static class Factory{ 13 | 14 | public static Point newCartesianPoint(double x, double y){ 15 | return new Point(x,y); 16 | } 17 | 18 | public static Point newPolarPoint(double rho, double theta){ 19 | return new Point(rho*Math.cos(theta), rho*Math.sin(theta)); 20 | } 21 | } 22 | 23 | public double getX() { 24 | return x; 25 | } 26 | 27 | public double getY() { 28 | return y; 29 | } 30 | 31 | public static void main(String[] args) { 32 | Point cartesianPoint = Point.Factory.newCartesianPoint(5,6); 33 | Point polarPoint = Point.Factory.newPolarPoint(5,0.5); 34 | System.out.println("Cartesian Points : X:"+cartesianPoint.getX()+" Y:"+cartesianPoint.getY()); 35 | System.out.println("Polar Points : X:"+polarPoint.getX()+" Y:"+polarPoint.getY()); 36 | } 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/factory/StrawberryIceCream.java: -------------------------------------------------------------------------------- 1 | package factory; 2 | 3 | public class StrawberryIceCream implements IceCream { 4 | @Override 5 | public String getIceCreamName() { 6 | return "Strawberry"; 7 | } 8 | 9 | @Override 10 | public Integer getCalories() { 11 | return 150; 12 | } 13 | 14 | @Override 15 | public Integer getCost() { 16 | return 20; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/factory/factory-class-diagram.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitech/design-pattern/1b0589cfb2b201c780ee40b9028874d4f27e9836/src/main/java/factory/factory-class-diagram.jpg -------------------------------------------------------------------------------- /src/main/java/prototype/BlackColor.java: -------------------------------------------------------------------------------- 1 | package prototype; 2 | 3 | class BlackColor extends Color{ 4 | 5 | public BlackColor() 6 | { 7 | this.colorName = "black"; 8 | } 9 | 10 | @Override 11 | void addColor() 12 | { 13 | System.out.println("Black color added"); 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/prototype/BlueColor.java: -------------------------------------------------------------------------------- 1 | package prototype; 2 | 3 | class BlueColor extends Color 4 | { 5 | public BlueColor() 6 | { 7 | this.colorName = "blue"; 8 | } 9 | 10 | @Override 11 | void addColor() 12 | { 13 | System.out.println("Blue color added"); 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /src/main/java/prototype/Client.java: -------------------------------------------------------------------------------- 1 | package prototype; 2 | 3 | class Client 4 | { 5 | public static void main (String[] args) 6 | { 7 | ColorStore.getColor("blue").addColor(); 8 | ColorStore.getColor("black").addColor(); 9 | ColorStore.getColor("black").addColor(); 10 | ColorStore.getColor("blue").addColor(); 11 | } 12 | } -------------------------------------------------------------------------------- /src/main/java/prototype/Color.java: -------------------------------------------------------------------------------- 1 | package prototype; 2 | 3 | abstract class Color implements Cloneable 4 | { 5 | 6 | protected String colorName; 7 | 8 | abstract void addColor(); 9 | 10 | public Object clone() 11 | { 12 | Object clone = null; 13 | try 14 | { 15 | clone = super.clone(); 16 | } 17 | catch (CloneNotSupportedException e) 18 | { 19 | e.printStackTrace(); 20 | } 21 | return clone; 22 | } 23 | } -------------------------------------------------------------------------------- /src/main/java/prototype/ColorStore.java: -------------------------------------------------------------------------------- 1 | package prototype; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | class ColorStore { 7 | 8 | private static Map colorMap = new HashMap<>(); 9 | 10 | static 11 | { 12 | colorMap.put("blue", new BlueColor()); 13 | colorMap.put("black", new BlackColor()); 14 | } 15 | 16 | public static Color getColor(String colorName) 17 | { 18 | return (Color) colorMap.get(colorName).clone(); 19 | } 20 | } -------------------------------------------------------------------------------- /src/main/java/prototype/prototype-class-diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitech/design-pattern/1b0589cfb2b201c780ee40b9028874d4f27e9836/src/main/java/prototype/prototype-class-diagram.png -------------------------------------------------------------------------------- /src/main/java/singleton/Client.java: -------------------------------------------------------------------------------- 1 | package singleton; 2 | 3 | public class Client { 4 | 5 | public static void main(String[] args) { 6 | Singleton instance1 = Singleton.getInstance(); 7 | Singleton instance2 = Singleton.getInstance(); 8 | Singleton instance3 = Singleton.getInstance(); 9 | Singleton instance4 = Singleton.getInstance(); 10 | 11 | System.out.println("Connection count : "+instance1.getConnectionCount()); 12 | System.out.println("Connection count : "+instance2.getConnectionCount()); 13 | System.out.println("Connection count : "+instance3.getConnectionCount()); 14 | System.out.println("Connection count : "+instance4.getConnectionCount()); 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/singleton/Singleton.java: -------------------------------------------------------------------------------- 1 | package singleton; 2 | 3 | class Singleton 4 | { 5 | private static Singleton obj; 6 | private int count; 7 | 8 | private Singleton() { 9 | count = 0; 10 | } 11 | 12 | // Only one thread can execute this at a time 13 | public static synchronized Singleton getInstance() 14 | { 15 | if (obj==null) 16 | obj = new Singleton(); 17 | return obj; 18 | } 19 | 20 | public int getConnectionCount(){ 21 | count++; 22 | return count; 23 | } 24 | } -------------------------------------------------------------------------------- /src/main/java/singleton/singleton-class-diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitech/design-pattern/1b0589cfb2b201c780ee40b9028874d4f27e9836/src/main/java/singleton/singleton-class-diagram.png -------------------------------------------------------------------------------- /src/test/java/abstractFactory/FactoryCreatorTest.java: -------------------------------------------------------------------------------- 1 | package abstractFactory; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue; 6 | 7 | class FactoryCreatorTest { 8 | 9 | @Test 10 | void getFactory() { 11 | 12 | FactoryCreator factoryCreator = new FactoryCreator(); 13 | 14 | assertTrue(factoryCreator.getFactory("amul") instanceof AmulIceCreamFactory); 15 | assertTrue(factoryCreator.getFactory("dairyQueen") instanceof DairyQueenCreamFactory); 16 | } 17 | } -------------------------------------------------------------------------------- /src/test/java/adapter/BirdAdapterTest.java: -------------------------------------------------------------------------------- 1 | package adapter; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertNotEquals; 6 | 7 | class BirdAdapterTest { 8 | 9 | @Test 10 | void squeak() { 11 | 12 | Sparrow sparrow = new Sparrow(); 13 | ToyDuck toyDuck = new PlasticToyDuck(); 14 | ToyDuck birdAdapter = new BirdAdapter(sparrow); 15 | 16 | assertNotEquals(toyDuck.squeak(), birdAdapter.squeak()); 17 | } 18 | } -------------------------------------------------------------------------------- /src/test/java/builder/CodeBuilderTest.java: -------------------------------------------------------------------------------- 1 | package builder; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class CodeBuilderTest { 8 | 9 | @Test 10 | void addField() { 11 | assertEquals(CodeBuilder.class, new CodeBuilder("Person").addField("name","String").getClass()); 12 | } 13 | 14 | @Test 15 | void testToString() { 16 | String expected = "public class Person\n" + 17 | "{\n" + 18 | " public String name;\n" + 19 | "}"; 20 | assertEquals(expected, new CodeBuilder("Person").addField("name","String").toString()); 21 | } 22 | } -------------------------------------------------------------------------------- /src/test/java/builder/PersonBuilderTest.java: -------------------------------------------------------------------------------- 1 | package builder; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class PersonBuilderTest { 8 | 9 | @Test 10 | void build() { 11 | PersonBuilder pb = new PersonBuilder(); 12 | assertEquals(Person.class,pb.build().getClass()); 13 | } 14 | 15 | @Test 16 | void getRealObject() { 17 | PersonBuilder pb = new PersonBuilder(); 18 | Person person = pb 19 | .lives() 20 | .name("Ankit") 21 | .inCity("Pune") 22 | .atPincode("427001") 23 | .works() 24 | .worksAt("Ideas") 25 | .worksAs("Developer") 26 | .paid(5000) 27 | .build(); 28 | 29 | String expected = "Person{name='Ankit', city='Pune', pincode='427001', company='Ideas', position='Developer', salary=5000}"; 30 | assertEquals(expected,person.toString()); 31 | } 32 | } -------------------------------------------------------------------------------- /src/test/java/composite/GraphicObjectTest.java: -------------------------------------------------------------------------------- 1 | package composite; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class GraphicObjectTest { 8 | 9 | @Test 10 | void testComposition() { 11 | 12 | GraphicObject drawing = new GraphicObject(); 13 | drawing.setName("My Drawing"); 14 | drawing.children.add(new Square("Red")); 15 | drawing.children.add(new Circle("Green")); 16 | 17 | GraphicObject group = new GraphicObject(); 18 | group.children.add(new Square("Blue")); 19 | group.children.add(new Circle("Blue")); 20 | 21 | drawing.children.add(group); 22 | 23 | assertEquals(3, drawing.children.toArray().length); 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /src/test/java/decorator/TransparentShapeTest.java: -------------------------------------------------------------------------------- 1 | package decorator; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class TransparentShapeTest { 8 | 9 | @Test 10 | void info() { 11 | TransparentShape red90TransparentCircle = new TransparentShape( 12 | new ColoredShape( 13 | new Circle(36), "Red" 14 | ),90 15 | ); 16 | 17 | assertEquals("This is a Circle of radius 36 and color Red and tranceperancy 90",red90TransparentCircle.info()); 18 | } 19 | } -------------------------------------------------------------------------------- /src/test/java/factory/IceCreamFactoryTest.java: -------------------------------------------------------------------------------- 1 | package factory; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue; 6 | 7 | class IceCreamFactoryTest { 8 | 9 | @Test 10 | void createIceCream() { 11 | IceCreamFactory iceCreamFactory = new IceCreamFactory(); 12 | assertTrue(iceCreamFactory.createIceCream("chocolate") instanceof ChocolateIceCream); 13 | assertTrue(iceCreamFactory.createIceCream("strawberry") instanceof StrawberryIceCream); 14 | } 15 | } -------------------------------------------------------------------------------- /src/test/java/factory/PointTest.java: -------------------------------------------------------------------------------- 1 | package factory; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class PointTest { 8 | 9 | @Test 10 | void getCartesianPoint() { 11 | assertEquals(Point.class, Point.Factory.newCartesianPoint(5,6).getClass()); 12 | } 13 | } -------------------------------------------------------------------------------- /src/test/java/prototype/ColorStoreTest.java: -------------------------------------------------------------------------------- 1 | package prototype; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue; 6 | 7 | class ColorStoreTest { 8 | 9 | @Test 10 | void getColor() { 11 | 12 | assertTrue(ColorStore.getColor("blue") instanceof BlueColor); 13 | assertTrue(ColorStore.getColor("black") instanceof BlackColor); 14 | } 15 | } -------------------------------------------------------------------------------- /src/test/java/singleton/SingletonTest.java: -------------------------------------------------------------------------------- 1 | package singleton; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | class SingletonTest { 9 | 10 | @Test 11 | void getInstance() { 12 | 13 | assertTrue(Singleton.getInstance() instanceof Singleton); 14 | } 15 | 16 | @Test 17 | void getConnectionCount() { 18 | 19 | Singleton instance1 = Singleton.getInstance(); 20 | Singleton instance2 = Singleton.getInstance(); 21 | 22 | instance1.getConnectionCount(); 23 | assertEquals(2,instance2.getConnectionCount()); 24 | } 25 | } --------------------------------------------------------------------------------