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();
}

Factory pattern deals with creating objects of a single type. Define an interface or abstract class for creating an object but let the Subclass decide which class to instantiate.

Why Factory Pattern?
Factory pattern was introduced to hide the Complexity of Object creation and to delegate much of Object creation details to Separate Class(Factory Class – LoggerFactory.java) to avoid unnecessary boiler plate code.

When to use factory Pattern?

  1. Instead of using the new operator or a constructor directly, you use a factory method or a factory class that returns an object of the desired type. The factory method or class can handle the logic of choosing the right subclass, setting the initial state, or applying any dependencies or configurations to the object. This way, you can abstract the creation process and make your code more flexible and modular.
  2. Use factory pattern when We don’t know the exact class of object that will be created at runtime.
  3. To promote loose coupling between classes.
  4. It encapsulates the object creation process which makes the creation process without affecting the rest of the code

How it is implemented?

  1. You define an interface or an abstract class that represents the type of objects you want to create.
  2. Implement one or more subclasses that inherit from it. Each subclass represents a specific implementation or variation of the object.
  3. Create a factory method or a factory class that takes some parameters or input and returns an object of the interface or abstract class type.
  4. The factory method or class decides which subclass to instantiate based on the parameters or input, and returns it as the output.
  5. FactoryClass (LoggerFactory.java) with static factory method decides the objects without exposing the instantiation logic to the Client
  6. Class with main() method starts the whole process

Logger.java

public interface Logger {
    public void log();
}

ConsoleLogger.java

public class ConsoleLogger implements Logger {
    @Override
    public void log() {
        System.out.println("Logging to Console System.. . .");
    }
}

DatabaseLogger.java

public class DatabaseLogger implements Logger {
    @Override
    public void log() {
        System.out.println("Logging to Console System.. . .");
    }
}

FileLogger.java

public class FileLogger implements Logger {
    @Override
    public void log() {
        System.out.println("Logging to txt File... .");
    }
}

LoggerFactory.java

public class LoggerFactory {
    public enum LoggerType {
        DATABASE, FILE, CONSOLE;
    }

    private LoggerFactory() {
    }

    public static Logger getLogger(LoggerType loggerType) {
        Logger logger;

        switch (loggerType) {
            case DATABASE:
                logger = new DatabaseLogger();
                break;
            case CONSOLE:
                logger = new ConsoleLogger();
                break;
            case FILE:
                logger = new ConsoleLogger();
                break;
            default:
                logger = new FileLogger();
        }
        return logger;
    }
}

Client.java

public class Client {
    public static void main(String[] args) {
        Logger objLogger = LoggerFactory.getLogger(LoggerFactory.LoggerType.CONSOLE);
        objLogger.log();
    }
}

What is Telescoping Constructor Pattern?
In Java, there is no support for default values for constructor parameters. As a workaround, a technique called “Telescoping constructor” is often used. A class has multiple constructors, where each constructor calls a more specific constructor in the hierarchy, which has more parameters than itself, providing default values for the extra parameters.

We’ve all at some point encountered a class with a list of constructors where each addition adds a new option parameter

Pizza(int size) { ... }        
Pizza(int size, boolean cheese) { ... }    
Pizza(int size, boolean cheese, boolean pepperoni) { ... }    
Pizza(int size, boolean cheese, boolean pepperoni, boolean bacon) { ... }

Disadvantage
This is called the Telescoping Constructor Pattern. The problem with this pattern is that once constructors are 4 or 5 parameters long it becomes difficult to remember the required order of the parameters as well as what particular constructor you might want in a given situation.

One alternative you have to the Telescoping Constructor Pattern is the JavaBean Pattern where you call a constructor with the mandatory parameters and then call any optional setters after:

Pizza pizza = new Pizza(12);
pizza.setCheese(true);
pizza.setPepperoni(true);
pizza.setBacon(true);

The problem here is that because the object is created over several calls it may be in an inconsistent state partway through its construction. This also requires a lot of extra effort to ensure thread safety.

The better alternative is to use the Builder Pattern.

public class Pizza {
  private int size;
  private boolean cheese;
  private boolean pepperoni;
  private boolean bacon;

  public static class Builder {
    //required
    private final int size;

    //optional
    private boolean cheese = false;
    private boolean pepperoni = false;
    private boolean bacon = false;

    public Builder(int size) {
      this.size = size;
    }

    public Builder cheese(boolean value) {
      cheese = value;
      return this;
    }

    public Builder pepperoni(boolean value) {
      pepperoni = value;
      return this;
    }

    public Builder bacon(boolean value) {
      bacon = value;
      return this;
    }

    public Pizza build() {
      return new Pizza(this);
    }
  }

  private Pizza(Builder builder) {
    size = builder.size;
    cheese = builder.cheese;
    pepperoni = builder.pepperoni;
    bacon = builder.bacon;
  }
}

Note that Pizza is immutable and that parameter values are all in a single location. Because the Builder’s setter methods return the Builder object they are able to be chained.

Pizza pizza = new Pizza.Builder(12)
                       .cheese(true)
                       .pepperoni(true)
                       .bacon(true)
                       .build();

This results in code that is easy to write and very easy to read and understand. In this example, the build method could be modified to check parameters after they have been copied from the builder to the Pizza object and throw an IllegalStateException if an invalid parameter value has been supplied. This pattern is flexible and it is easy to add more parameters to it in the future. It is really only useful if you are going to have more than 4 or 5 parameters for a constructor. That said, it might be worthwhile in the first place if you suspect you may be adding more parameters in the future.

Factory Patterns vs Builder Pattern
Consider a restaurant. The creation of “today’s meal” is a factory pattern, because you tell the kitchen “get me today’s meal” and the kitchen (factory) decides what object to generate, based on hidden criteria.

The builder appears if you order a custom pizza. In this case, the waiter tells the chef (builder) “I need a pizza; add cheese, onions and bacon to it!” Thus, the builder exposes the attributes the generated object should have, but hides how to set them.

The Servlet which loads the Singleton class should be loaded during the server startup

ConfigServlet.java

public class ConfigServlet extends HttpServlet
{	
	@Override
	public void init() throws ServletException
	{
	  super.init();
	  SingletonDBConnection.getInstance();
	}
}

web.xml

<servlet>
   <servlet-name>StartUpServlet</servlet-name>
   <servlet-class>com.mugil.tutor.ConfigServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>

DBConnection.java

public class DBConnection
{
	public Connection getDBConnection()
	{
		Connection connection = null;

		try
		{
			connection = DriverManager.getConnection(
					"HOST_NAME", "USER_NAME", "PASSWORD");

		}
		catch (SQLException e)
		{
			e.getLocalizedMessage();	
			System.out.println("Connection Failed! Check output console");
			e.printStackTrace();
			return null;
		}
		return connection;
	}
}

SingletonDBConnection.java

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class SingletonDBConnection
{
	private static SingletonDBConnection singleInstance;
	private static DataSource dataSource;
	private static Connection dbConnect;
	
	private SingletonDBConnection()
	{
		try
		{
			Context initContext = new InitialContext();
			Context envContext  = (Context) initContext.lookup("java:/comp/env");
			dataSource		   = (DataSource) envContext.lookup("jdbc/testdb");
			
			try
			{
				dbConnect  = dataSource.getConnection();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			} 	
		}
		catch (NamingException e)
		{
			e.printStackTrace();
		}
	}
	
	public static SingletonDBConnection getInstance()
	{
		if(singleInstance == null)
		{
			synchronized (SingletonDBConnection.class)
			{
				if(singleInstance == null)
				{
					singleInstance = new SingletonDBConnection();
				}
			}
		}

		return singleInstance;
	}
	
	public static Connection getConnInst()
	{
		try
		{
			dbConnect = dataSource.getConnection();
		}
		catch (SQLException e1)
		{
			e1.printStackTrace();
		}
		
		if(dbConnect == null)
		{
			try
			{
				Context initContext = new InitialContext();
				Context envContext  = (Context) initContext.lookup("java:/comp/env");
				dataSource		    = (DataSource) envContext.lookup("jdbc/testdb");
				
				try
				{
					dbConnect  = dataSource.getConnection();
				}
				catch (SQLException e)
				{
					e.printStackTrace();
				} 	
			}
			catch (NamingException e)
			{
				e.printStackTrace();
			}
		}
		
		return dbConnect;		 
	}
}

ListUsers.java

public List<User> getUsersList()
	{
		Connection conn;
		Statement  stmt = null;
		ResultSet  rs;
		List<User> arrUsersList = new ArrayList<User>();
		conn = SingletonDBConnection.getInstance().getConnInst();
		
		String strSQL = "SELECT UserId, UserName, Gender, UserLocation " +
						"  FROM tblusers";
		
		try
		{
			stmt = conn.createStatement();			
			rs = stmt.executeQuery(strSQL);
			
			while(rs.next())
			{
				User objUser = new User();
				objUser.setUserId(rs.getString("UserId"));
				objUser.setUserName(rs.getString("UserName"));
				objUser.setUserGender(rs.getString("Gender"));
				objUser.setUserLocation(rs.getString("UserLocation"));
				arrUsersList.add(objUser);
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(conn != null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); }
		}
		
		
		return arrUsersList;
	}