Design Patterns in Java

Design pattern are basic building block to efficiently design application as per requirement.
Broadly we divide it into 3 categories.
  • Creational
  • Structural
  • Behavioral.

  • Singleton
  • Prototype
  • Factory
  • Factory method
  • abstract factory
  • Builder


Creational pattern are based upon strategy we apply to control creation of object.
If we want to create object which can't be created again we call it singleton. Its like resource which we don't want be duplicated like connection.we make constructor private to achieve our objective.

Below patterns comes under Creational design pattern.

Structural (A,B,C,D,F,F,P)
  • Adapter,
  • bridge,
  • Composite, 
  • Decorator, 
  • Facade,
  • Flyweight, 
  • Proxy

Behavioural (MM,II,SS,CC,TV,O)

  • Command, 
  • Chain of responsibility,
  • Mediator, 
  • Memento,
  • Iterator, 
  • Interpreter,
  • Strategy,
  • State, 
  • Template, 
  • Visitor, 
  • Observer

J2EE Design Patern (Apart from GORM)

  • MVC ,
  • Front controller , 
  • DAO , 
  • Service locator , 
  • Business delegate

Sample Code:


public class DemoSingleton {
    private static DemoSingleton instanceOfSingleton;
     * Create private constructor
    private DemoSingleton(){
     * Create a static method to get instance.
    public static DemoSingleton getInstance(){
        if(instanceOfSingleton == null){
            instanceOfSingleton = new DemoSingleton();
        return instanceOfSingleton;
    public void getSomeResource(){
        // do something here
        System.out.println(" get me ...");
    public static void main(String a[]){
        DemoSingleton ds = DemoSingleton.getInstance();

Factory Pattern

Factory of what? Of classes. In simple words, if we have a super class and n sub-classes, and based on data provided, we have to return the object of one of the sub-classes, we use a factory pattern.


Popular posts from this blog

Conversion from Clob To String in java and Vice versa

Portable Java 8/ JDK8 setup without admin rights

Multi Threading , Producer consumer problem in Java