Java Design Pattern

Singleton Design Pattern Using Java

Singleton Design Pattern Using Java

Ashish Lahoti

This problem is frequently asked in the interview to check your understanding on singleton design pattern. This program answers following artefacts of singleton design pattern:-

  1. How to make singleton class thread safe?
    • using double checked locking
  2. How to prevent deserialization to create new object of singleton class?
    • using readResolve method to return same instance
  3. How to prevent cloning to create a new object of singleton class?
    • override clone method to return same instance
  4. How to prevent reflexion to create a new object of singleton class?
    • check instance exist in private constructor and throw exception if already exist You can find the singleton pattern implementation here.
Immutable Class in Java

Immutable Class in Java

Ashish Lahoti

In this tutorial, we’ll learn about Immutable Class and its benefits in thread-safety, caching and collections. We will also look at rules to create immutable classes and eventually we’ll write an Immutable Class from scratch in Java.