Category: Design Patterns

Singleton – Creational Design Pattern

Singleton Design Pattern is one of the creational design pattern which creates only one instance of the class. Only one instance of the class is created per Java Virtual Machine. Singleton pattern is used for logging, drivers object and thread pool. There are different ways to create singleton design pattern, but all of them follow three concepts

  1. private constructor to restrict access to other classes.
  2. private static variable of the same class that is only instance of the class.
  3. public static method to return the instance of the class.
class SingletonPattern {
   private static SingletonPattern defaultInstance;
   private SingletonPattern() { }
   public static SingletonPattern getInstance() {
      if(defaultInstance == null) {
         System.out.println("Singleton Pattern Default Instance Created");
         defaultInstance = new SingletonPattern();
      }
      return defaultInstance;
   }
}

The above singleton class is not thread-safe and may not be useful in multi-thread environment. Need to take necessary steps to make it thread safe. The below method of creating singleton pattern is the most widely used and doesn’t require any synchronization techniques.

class SingletonPattern {
    private SingletonPattern() { }	
    private static class SingletonHelper {
        private static final SingletonPattern INSTANCE = new SingletonPattern();
    }
    public static SingletonPattern getInstance() {
        return SingletonHelper.INSTANCE;
    }
}
Advertisements

Design Patterns – Introduction

Design Patterns are proven solutions provided to commonly occurred problems while designing software solution. They evolved over years to resolve most common problems, so we can follow them blindly without any hesitation.

Are Design Patterns dependent on any programming language? No, Design Pattern is an approach, it is not tied to any programming language. Once understood can be applied in any programming language.

Little History from wiki-

Patterns originated as an architectural concept by Christopher Alexander (1977/79). In 1987, Kent Beck and Ward Cunningham began experimenting with the idea of applying patterns to programming – specifically pattern languages – and presented their results at the OOPSLA conference that year. In the following years, Beck, Cunningham and others followed up on this work.

Design patterns gained popularity in computer science after the book Design Patterns: Elements of Reusable Object-Oriented Software was published in 1994 by the so-called “Gang of Four” (Gamma et al.), which is frequently abbreviated as “GoF”.

GoF Design Patterns are foundation for all the design patterns out there. They are 23 in number divided into three categories.

  1. Creational Patterns – Different ways of creating objects.
    • Singleton
    • Factory
    • Abstract Factory
    • Prototype
    • Builder
  2. Structural Patterns – Different ways of orgainising classes and objects, using associations and aggregations to form complex structures.
    • Adapter
    • Bridge
    • Composite
    • Decorator
    • Facade
    • Flyweight
    • Proxy
  3. Behavioural Patterns – Interaction/Communication b/w objects, they help to keep communication loosely coupled.
    • Chain of responsibility
    • Command
    • Interceptor
    • Iterator
    • Mediator
    • Memento
    • Observer
    • State
    • Strategy
    • Template method
    • Visitor

We will discuss each one of these patterns in details in the subsequent posts.