Posts

Design Patterns Introduction

Creational Patterns Creational patterns instantiate Group of similar objects or Single object.  There are five creational patterns: Abstract Factory:  It creates family of related or dependent object. Builder . It is used to create complicated objects with the parts of class that required to be created in the some fix order or with specific algorithm style. External class controls the creation of object. Factory Method . This patter abstracting the process of object construction. It is used to create a object of particular class at runtime by delegating the responsibility of object creation to subclass.  Prototype . It is used to create a new object with copying all of the properties of an existing object, creates an independent clone object.  Singleton . It ensures that only one and only one object of a class is ever getting created. All references to objects of the class refer to the same created instance. Structural Patterns Structural pattern...

Builder Pattern

/* ADVANTAGES :  * 1) Immutable Object  * 2) Readability  * 3) No need of Multiple argument method  * 4) Can put proper validation on values while assigning values  * 5) Can force to have mandatory values while building object  * 6) No need of multiple argument constructor  * 7) Increase/decrease some required values based on value assignments  * */ package pattern.builder; public class FoodDish {     private String rice;     private String daal;     private String salad;     private String sabji;     private String roti;         public static class DishBuilder {         String rice;         String daal;         String salad;         String sabji;         String roti;      ...

SOLID Principles

Single responsibility principle A class should have only a single responsibility (i.e. changes to only one part of the software's specification should be able to affect the specification of the class) Open/closed principle software entities should be open for extension, but closed for modification. Liskov substitution principle objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program. Interface segregation principle Many client-specific interfaces are better than one general-purpose interface. Subclasses must not be force to implements the methods which are useless for it. Dependency inversion principle one should “depend upon abstractions, [not] concretions

Design Principle

Identify the aspects of your application that vary and separate them from what stays the same. Program to an interface, not an implementation. Favor composition over inheritance. Strive for loosely coupled designs between objects that interact. Classes should be open for extension, but closed for modification. Depend upon abstractions. Do not depend upon concrete classes. Principle of Least Knowledge - talk only to your immediate friends. The Principle of Least Knowledge guides us to reduce the interactions between objects to just a few close friends. A class should have only one reason to change.

Iterator Pattern

The   Iterator Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. It also places the task of traversal on the iterator object, not on the aggregate, which simplifies the aggregate interface and implementation, and places the responsibility where it should be. Example  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 15...

Template Pattern

The Template Method defines the steps of an algorithm and allows subclasses to provide the implementation for one or more steps The Template Method Pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure. Example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 public abstract class Vehicle { final void haveRide (){ info (); pourFood (); start (); passengers (); gearIt (); run (); stop (); } abstract public void info (); public void pourFood (){ System . out . println ( "Pouring Food" ); } public void start (){ System . out . println ( "Starting" ); } public ...

Command Pattern

The  Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations. Example public interface Command { 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 public void execute (); } ========== public interface Light { public void on (); public void off (); } ========== public class LightOff implements Command { private Light light ; public LightOff ( Light light ) { this . light = light ; } public void execute (){ System . out . println ( "OFF command is Fired" ); light . off (); } } ========== public class LightOn implements Command { private Light light ; LightOn ( L...