Prototype Pattern

  • if objects are available,copy and use them instead of creating new ones
    • clone the available object,modify and use
    • modify clone method to create new objects,such that cloning will provide the necessary object
  • used if we need many instances which are having little differences
  • can use shallow or deep copy
  • the object which we copy must provide the copying feature
  • cloning isles expensive that creating objects using new operator
  • Use
    • when the classes to instantiate are specified at runtime
    • When instances of a class can have one of only a few different combinations of state
  • JDK
  • Example: will update !

Reference :HowToDoInJava , Javapapers, A good read,

Builder Pattern

  • creational DP
  • used when the objects to be created has many attributes some of which are mandatory while some optional
    • in normal case this leads to having overloaded constructors
    • we can also create object and then fill up things to it,but this leads to inconsistent state of object
  • helps in creating immutable objects
  • In JDK
    • java.lang.StringBuffer append
    • java.lang.StringBuilder append etc
  • Implementation
    • a static inner class will be set up in the class which has multiple params to check while creating an object
    • has naming convention of classname+”Builder”
    • must have a public constructor with all req attributes as params
    • must have setter methods to set optional params
    • build() method will return the created object

Continue reading

Factory Pattern

Factory Pattern

  • is a creational design pattern
  • creates objects without exposing the instantiation logic to the client.
  • instead of having the creation of object in client code,its moved to a factory method.
  • creates different objects from factory by encapsulating the creation code without using new operator
  • refers to the newly created object through a common interface
  • defines an interface for creating objects, but lets subclasses decide which classes to instantiate.
  • we may have many subclasses which we need to instantiate in our program.Instead of calling new and creating it overtime,we pass on this logic to a Factory class which returns our common superclass / interface  /subclass instances and our program need not know the logic behind it 🙂
  • defines a virtual constructor
  • used widely in JDK,Spring etc
    • java.util.Calendar, ResourceBundle and NumberFormat getInstance() methods uses Factory pattern.
    • valueOf() method in wrapper classes like Boolean, Integer etc.
  • Advantages
    • Factory pattern is most suitable where there is some complex object creation steps are involved. To ensure that these steps are centralized and not exposed to composing classes, factory pattern should be used
    • code to interface and not implementation
    • decoupling of classes which helps to make changes easily – polymorphic creation is possible
    • inheritance provided abstraction between implementation and client classes Continue reading

Singleton Pattern

  • under creational DP
  • single class is responsible in creation of the object,initialisation,access and enforcement
  • ensures that only one instance of class exists in JVM
  • same instance is returned if already created
  • single global access point for this single created instance
  • used when
    • ownership does not keep changing
    • lazy initialisation is desirable
    • no global access
  • used in
    • logging,caching,thread pool ,driver objects etc
    • java.lang.Runtime,java.awt.Desktop
    • Abstract Factory Builder Pattern,Prototype,Facade etc
      Continue reading