Creational Paterns

These patterns describe the best practices related to object creation - how they should be created, where they should be created in order to build a loosely couple well designed software.

Singleton -

This pattern ensures that only 1 object of a class gets created (per JVM) and hence it is about how to create only one object of a class. So below are how you can create singletons:

   public Object getInstance() {        
      if (_instance == null) {              
          synchronized (){               
              if (_instance == null) {                             
                    _instance = new SingletonInstance()                                               
                    }                                      
                 }                                
             }                     
             return _instance           
       }           
   

Factory -

This design pattern deals with how objects should be created to ensure encapsulation. It is very important to seperate the object creation from object usage so that both of them can vary independently.Below are the variants of the factory pattern (In fact Spring framework is one of the most popular implementations of the Factory design pattern):

Builder -

This pattern is used to avoid telescoping (multiple) constructors and help to create the big object in steps. Gof Defines it as “Seperate the construction process of objects from its representation so that the same construction process can create multiple representations.” Components are : 

One interesting observation is that if you are using Kotlin as the programming language and leverage the featurees of default values in functions and named parameters in function calls, these features probably make method overloading and builder pattern redundant!

Prototype -

This pattern is used to create an exact clone if creation of an object is an expensive operation. Following types are available: