|
| 1 | +# Design Patterns in Java |
| 2 | + |
| 3 | +### 1. Creational Patterns:- |
| 4 | + - used to solve common problems when creating objects. |
| 5 | + - simplify the creation of complex objects. |
| 6 | + - Improve overall readability of code. |
| 7 | + |
| 8 | + Types of Creational Patterns:- |
| 9 | + |
| 10 | + i) Builder Pattern:- |
| 11 | + When to use:- - when dealing a complex object that requires alot of parameters. |
| 12 | + - when trying to prevent mistakes from creating a complex object. |
| 13 | + - when trying to avoid unreadable, big constructors that are sometimes required for complex creation. |
| 14 | + |
| 15 | + ii) Factory Pattern:- |
| 16 | + Advantages:- - Hides the internal logic of creating objects. |
| 17 | + - Enables the programmer to add new different objects of the same type. |
| 18 | + Disadvantages:- - The complexity of this pattern tends to be high. |
| 19 | + - Cannot be refractored into. |
| 20 | + |
| 21 | + iii) Abstract Factory Pattern:- It contains a super factory which creates object factories. |
| 22 | + |
| 23 | + iv) Singleton Pattern:- - Prevents the instantiation of a class more than once. |
| 24 | + - provides single access to an object. |
| 25 | + use cases:- logger, thread pool, cache, Report. |
| 26 | + |
| 27 | + v) Prototype Pattern:- It is used whenever object is very expensive to create. |
| 28 | + - creating new objects with clone(), not new() |
| 29 | + - Makes use of an interface. |
| 30 | + - shallow cloning, deep cloning (Shallow cloning copies only instance variables of an object whereas Deep cloning copies the other object references as well) |
| 31 | + - objects are still unique even if they are copied. |
| 32 | + Disadvantages:- - usually comes with other design patterns. |
| 33 | + - Increases complexity for creating a deep copy. |
| 34 | + vi) Object Pool Pattern:- The pool will allocate a predefined number of objects and the client will use these objects. |
| 35 | + Advantages:- - Objects are cached. |
| 36 | + - Enforces objects reusability. |
| 37 | + Disadvantages:- - Complexity is high |
| 38 | + - Tends to rely on other patterns. |
| 39 | + - Low popularity. |
0 commit comments