Q1.What is the difference between Singleton vs Factory pattern?
A singleton pattern ensures that you always get back the same instance of whatever type you are retrieving, whereas the factory pattern generally gives you a different instance of each type.
The purpose of the singleton is where you want all calls to go through the same instance. An example of this might be a class that manages a disk cache, or gets data from a static dictionary; wherever it is important only one known instance interacts with the resource. This does make it less scalable.The purpose of the factory is to create and return new instances. Often, these won’t actually be the same type at all, but they will be implementations of the same base class. However, there may be many instances of each type
Q2.How to stop create instance via Reflection in Singleton
private Singleton() { if (singleton != null) { throw new IllegalStateException("Singleton already constructed"); } }
Q3.What are different ways of preventing object creation in singleton?
There are 5 ways of creating Objects. The below code explains how to prevent object creation by all 5 methods. Instead of
doing all these we can create singleton by using ENUM
Singleton.java
public class Singleton implements Serializable { private static final long serialVersionUID = 3119105548371608200 L; private static final Singleton singleton = new Singleton(); //Prevents Class creation by Constructor private Singleton() { //Prevents Object creation by reflection if( Singleton.singleton != null ) { throw new InstantiationError( "Creating of this object is not allowed." ); } } public static Singleton getInstance() { return singleton; } //Prevents Class creation during cloning @Override protected Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException("Cloning of this class is not allowed"); } //Prevents New Object Creation by returning same singleton object protected Object readResolve() { return singleton; } }
Main.java
try { Class<Singleton> singletonClass = (Class<Singleton>) Class.forName("test.singleton.Singleton"); Singleton singletonReflection = singletonClass.newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }