• Make the default constructor private, to prevent other objects from using the new operator with the Singleton class.
  • Create a static creation method that acts as a constructor. Under the hood, this method calls the private constructor to create an object and saves it in a static field. All following calls to this method return the cached object.

Prevent Cloning to break a Singleton Class Pattern

@Override
protected Object clone()
throws CloneNotSupportedException {
   return getInstance();
}

🌱 Back to Garden