The intention is to abstract steps of construction of objects when there are lot of parameters and to provide thread safety at the time of object creation. You will have Class and ClassBuilder(static inner class) which has same parameters as Class, setters for parameters and a build method. The setter methods in ClassBuilder helps to carry validation over parameters before the object for Class gets created in build method

Why builder Pattern?
If there are many attributes in the class and object is created over several calls it may be in an inconsistent state partway through its construction. To address this builder pattern is introduced. If Object Creation should be delayed until all the validation of class attributes are performed builder pattern addresses the need.

If there are 2 attributes to a Class then we can create 2^N Constructor based on the two attributes. This Results in Boiler plate code. What if the number of attributes keeps increasing over a period of time.

Report.java

import java.util.InvalidPropertiesFormatException;

public class Report {
    private String reportName;
    private String reportFormat;

    private Report(){}

    public static ReportBuilder getReportBuilder(){
        return new ReportBuilder();
    }


    @Override
    public String toString() {
        return "Report{" +
                "reportName='" + reportName + '\'' +
                ", reportFormat='" + reportFormat + '\'' +
                '}';
    }

    public static class ReportBuilder {
        private String reportName;
        private String reportFormat;

        private ReportBuilder(){}

        public ReportBuilder setReportName(String reportName) {
            this.reportName = reportName;
            return this;
        }

        public ReportBuilder setReportFormat(String reportFormat) {
            this.reportFormat = reportFormat;
            return this;
        }

        public Report build() throws InvalidPropertiesFormatException {
            if(this.reportFormat == "CSV"){
                throw new InvalidPropertiesFormatException("Format Not Supported");
            }

            if(this.reportName.length() < 10){
                throw new InvalidPropertiesFormatException("Name should be greater than 10");
            }

            //Object for Report should not be created before validation is completed as above
            Report objReport = new Report();
            objReport.reportName = this.reportName;
            objReport.reportFormat = this.reportFormat;
            return objReport;
        }
    }
}

BuilderPatternClient.java

import java.util.InvalidPropertiesFormatException;

public class BuilderPatternClient {
    public static void main(String[] args) throws InvalidPropertiesFormatException {
        Report objReport = Report.getReportBuilder()
                .setReportFormat("PDF")
                .setReportName("Monthly Transactions")
                .build();

        System.out.println(objReport);
    }
}

Output

Report{reportName='Monthly Transactions', reportFormat='PDF'}

What is prototype Pattern?

When object creation is costly and time-consuming, Pick an object that is configured for either the default or in the ballpark of some specific use case and then you clone this object and configure to your exact needs.

When to use prototype Pattern?

  1. Prototype patterns are required when object creation is a time-consuming, and costly operation, so we create objects with the existing object itself.
  2. The Prototype Design Pattern allows you to create new objects by cloning existing ones without being bound to the specific classes of the objects being cloned. This promotes decoupling between the client code and the concrete classes, making it easier to introduce new types of objects without modifying the client code.

Where to use prototype Pattern?

  1. If you want to create an object but do not want to go through the expensive object creation procedure where network or database calls are made, then use the prototype pattern. Just create a copy of the object and do your changes on it.

How prototype Pattern is acheived?

  1. Create a Interface with clone method
  2. Create a Concrete prototypes which implements the interface. In the below example it is PersonPrototype. This Concrete class would be DB classes which consume lot of resource and the one which you dont want to create again and again
  3. Classes use the above Concrete prototype to add their own attributes and create new classes EmployeePrototype, StudentPrototype, SoftwareEmployee(Concrete Class)
  4. Prototypes are added to Registry which is nothing but an HashMap to Store String and Prototype classes
  5. Concrete classes which want to extend the Prototype uses the above hashmap registry to fetch the protype and add their custom attributes.

There are two things to note in code

  • One is Clone method which makes the extending class mandatory to implement clone method. The same can be done by implementing Clone interface
  • Second thing is how the Concrete Classes implements the clone and creates copy of itself at the same time calling the parent prototype class
    .
    .
        public StudentPrototype(StudentPrototype studentPrototype) {
            super(studentPrototype);
            this.institution = studentPrototype.institution;
            this.rollNo = studentPrototype.rollNo;
        }
    
    .
    
  • PersonPrototype is Concrete Prototype which is costly to create
  • We add Custom attributes at each level and create prototype and concrete class from PersonPrototype

Prototype.java

public interface Prototype<T> {
    T clone();
}

ConcretePersonPrototype.java

public class ConcretePersonPrototype implements Prototype<ConcretePersonPrototype>{
    private String name = "Mugilvannan G";
    private String aadhar = "1452-5874-5124-847";

    public ConcretePersonPrototype() {
    }

    @Override
    public ConcretePersonPrototype clone() {
        return new ConcretePersonPrototype(this);
    }

    public ConcretePersonPrototype(ConcretePersonPrototype concretePersonPrototype) {
        this.name = concretePersonPrototype.name;
        this.aadhar = concretePersonPrototype.aadhar;
    }

    public String getName() {
        return name;
    }

    public String getAadhar() {
        return aadhar;
    }
}

EmployeePrototype.java

public class EmployeePrototype extends ConcretePersonPrototype {
    private Integer pfAccountNo;
    private String startDate;
    private String endDate;

    public EmployeePrototype() {
    }

    public EmployeePrototype(EmployeePrototype employeePrototype) {
        super(employeePrototype);
        this.pfAccountNo = employeePrototype.pfAccountNo;
    }

    public EmployeePrototype clone() {
        return new EmployeePrototype(this);
    }

    public Integer getPfAccountNo() {
        return pfAccountNo;
    }

    public void setPfAccountNo(Integer pfAccountNo) {
        this.pfAccountNo = pfAccountNo;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    @Override
    public String toString() {
        return "Career Details : {" + '\'' +
                " Employee Name ='" + super.getName() + '\'' +
                " Employee Aadhar ='" + super.getAadhar() + '\'' +
                " Employee pfAccountNo='" + this.getPfAccountNo() + '\'' +
                '}';
    }
}

StudentPrototype.java

public class StudentPrototypeConcrete extends ConcretePersonPrototype {
    private String institution;
    private  Integer rollNo;

    public StudentPrototypeConcrete() {
    }

    public StudentPrototypeConcrete(StudentPrototypeConcrete studentPrototype) {
        super(studentPrototype);
        this.institution = studentPrototype.institution;
        this.rollNo = studentPrototype.rollNo;
    }

    public Integer getRollNo() {
        return rollNo;
    }

    public void setRollNo(Integer rollNo) {
        this.rollNo = rollNo;
    }

    public String getInstitution() {
        return institution;
    }

    public void setInstitution(String institution) {
        this.institution = institution;
    }

    public StudentPrototypeConcrete clone(){
        return new StudentPrototypeConcrete(this);
    }

    @Override
    public String toString() {
        return "Education Details : {" +
                " Student Name='" + super.getName() + '\'' +
                " Student Aadhar='" + this.getAadhar() + '\'' +
                " Student Institution='" + this.getInstitution() + '\'' +
                " Student Rollno='" + this.getRollNo() + '\'' +
                '}';
    }
}

EmployeePrototype.java

public class EmployeePrototype extends PersonPrototype {
    private Integer pfAccountNo;
    private String startDate;
    private String endDate;

    public EmployeePrototype() {
    }

    public EmployeePrototype(EmployeePrototype employeePrototype) {
        super(employeePrototype);
        this.pfAccountNo = employeePrototype.pfAccountNo;
    }

    public EmployeePrototype clone() {
        return new EmployeePrototype(this);
    }

    public Integer getPfAccountNo() {
        return pfAccountNo;
    }

    public void setPfAccountNo(Integer pfAccountNo) {
        this.pfAccountNo = pfAccountNo;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    @Override
    public String toString() {
        return "Career Details : {" + '\'' +
                " Employee Name ='" + super.getName() + '\'' +
                " Employee Aadhar ='" + super.getAadhar() + '\'' +
                " Employee pfAccountNo='" + this.getPfAccountNo() + '\'' +
                '}';
    }
}

SoftwareEmployee.java

public class SoftwareEmployee extends EmployeePrototype {
    public Integer empId;
    public String designation;
    public String practice;
    public String organisation;

    public SoftwareEmployee() {
    }

    public SoftwareEmployee(SoftwareEmployee softwareEmployee) {
        super(softwareEmployee);
        this.empId = softwareEmployee.empId;
        this.designation = softwareEmployee.designation;
        this.practice = softwareEmployee.practice;
        this.organisation = softwareEmployee.organisation;
    }

    public SoftwareEmployee clone(){
        return new SoftwareEmployee(this);
    }

    public Integer getEmpId() {
        return empId;
    }

    public void setEmpId(Integer empId) {
        this.empId = empId;
    }

    public String getDesignation() {
        return designation;
    }

    public void setDesignation(String designation) {
        this.designation = designation;
    }

    public String getPractice() {
        return practice;
    }

    public void setPractice(String practice) {
        this.practice = practice;
    }

    public String getOrganisation() {
        return organisation;
    }

    public void setOrganisation(String organisation) {
        this.organisation = organisation;
    }

    @Override
    public String toString() {
        return "Present Work Details: {" +
                "Software Engineer Name ='" + super.getName() + '\'' +
                " Aadhar No ='" + super.getAadhar() + '\'' +
                " EmpId ='" + this.empId + '\'' +
                " Designation='" + this.designation + '\'' +
                " Organisation='" + this.organisation + '\'' +
                " Practice='" + this.practice + '\'' +
                '}';
    }
}

Client.java

public class Client {
    public static void fillRegistry(PersonRegistry registry) {
        StudentPrototype objStudPrototype = new StudentPrototype();
        objStudPrototype.setRollNo(15462);
        objStudPrototype.setInstitution("Mowbarys");
        registry.register("StudentPerson", objStudPrototype);

        EmployeePrototype objEmpPrototype = new EmployeePrototype();
        objEmpPrototype.setPfAccountNo(4151542);
        objEmpPrototype.setStartDate("2012");
        objEmpPrototype.setEndDate("NA");
        registry.register("EmployeePerson", objEmpPrototype);

        SoftwareEmployee objSoftwareEmployee = new SoftwareEmployee();
        objSoftwareEmployee.setPfAccountNo(4151542);
        registry.register("SoftwareEmployee", objSoftwareEmployee);
    }

    public static void main(String[] args) {
        PersonRegistry registry = new PersonRegistry();
        fillRegistry(registry);

        EmployeePrototype objEmployee = (EmployeePrototype) registry.get("EmployeePerson").clone();

        StudentPrototype objStudent = (StudentPrototype)registry.get("StudentPerson").clone();
        objStudent.setRollNo(15425);
        objStudent.setInstitution("Mowbarys");

        SoftwareEmployee objSoftEmployee1 = (SoftwareEmployee) registry.get("SoftwareEmployee").clone();
        objSoftEmployee1.setStartDate("2011");
        objSoftEmployee1.setEndDate("2012");
        objSoftEmployee1.setEmpId(1001);
        objSoftEmployee1.setOrganisation("Fuchsia Software");
        objSoftEmployee1.designation = "Programmer";
        objSoftEmployee1.practice = "Classic ASP, HTML, CSS";

        SoftwareEmployee objSoftEmployee2 = (SoftwareEmployee) registry.get("SoftwareEmployee").clone();
        objSoftEmployee2.setStartDate("2012");
        objSoftEmployee2.setEndDate("2015");
        objSoftEmployee2.setEmpId(754185);
        objSoftEmployee2.setOrganisation("Infosys");
        objSoftEmployee2.designation = "Technology Analyst";
        objSoftEmployee2.practice = "Java, Spring Boot";

        SoftwareEmployee objSoftEmployee3 = (SoftwareEmployee) registry.get("SoftwareEmployee").clone();
        objSoftEmployee3.setStartDate("2018");
        objSoftEmployee3.setEndDate("NA");
        objSoftEmployee3.setEmpId(152);
        objSoftEmployee3.setOrganisation("Cognizant");
        objSoftEmployee3.designation = "Technology Lead";
        objSoftEmployee3.practice = "Java, Devops";


        System.out.println(objStudent);
        System.out.println(objEmployee);
        System.out.println(objSoftEmployee1);
        System.out.println(objSoftEmployee2);
        System.out.println(objSoftEmployee3);
    }
}

Output

Education Details : { Student Name='Mugilvannan G' Student Aadhar='1452-5874-5124-847' Student Institution='Mowbarys' Student Rollno='15425'}
Career Details : {' Employee Name ='Mugilvannan G' Employee Aadhar ='1452-5874-5124-847' Employee pfAccountNo='4151542'}
Present Work Details: {Software Engineer Name ='Mugilvannan G' Aadhar No ='1452-5874-5124-847' EmpId ='1001' Designation='Programmer' Organisation='Fuchsia Software' Practice='Classic ASP, HTML, CSS'}
Present Work Details: {Software Engineer Name ='Mugilvannan G' Aadhar No ='1452-5874-5124-847' EmpId ='754185' Designation='Technology Analyst' Organisation='Infosys' Practice='Java, Spring Boot'}
Present Work Details: {Software Engineer Name ='Mugilvannan G' Aadhar No ='1452-5874-5124-847' EmpId ='152' Designation='Technology Lead' Organisation='Cognizant' Practice='Java, Devops'}

Another example using prototype pattern

NiosPrototype.java

import java.util.Objects;

//1.Create a Interface or abstract class with clone method
abstract class NiosPrototype {
    public String engineCapacity;
    public String transmission;

    public NiosPrototype() {
    }

    public NiosPrototype(NiosPrototype niosPrototype) {
        if(niosPrototype != null) {
            this.engineCapacity = niosPrototype.engineCapacity;
            this.transmission = niosPrototype.transmission;
        }
    }

    public abstract NiosPrototype clone();

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        NiosPrototype that = (NiosPrototype) o;
        return Objects.equals(engineCapacity, that.engineCapacity) && Objects.equals(transmission, that.transmission);
    }
    
}

Asta.java

//2.Create Concrete prototype which  implements the interface.This class is resource intensive which you dont want
//to use new operator again and again rather call clone method to create it
public class Asta extends NiosPrototype{
    public String sideBags;

    @Override
    public NiosPrototype clone() {
        return new Asta(this);
    }

    public Asta() {
    }

    public Asta(Asta target) {
        super(target);

        if(target != null){
            this.sideBags = target.sideBags;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        Asta asta = (Asta) o;
        return Objects.equals(sideBags, asta.sideBags);
    }
}

CarModel.java

public class CarModel {
    public static void main(String[] args) {
        Asta objAsta1 = new Asta();
        objAsta1.engineCapacity = "1196";
        objAsta1.transmission = "Manual";
        objAsta1.sideBags ="Available";

        Asta objAsta2 = (Asta)objAsta1.clone();

        if(objAsta1 == objAsta2){
            System.out.println("Both objects are same");
        }


        if(objAsta1.equals(objAsta2)){
            System.out.println("Both objects are different but has same value");
        }
    }
}

Note:

  1. To create a copy of you the best person who can tell about you is none other than you, just like autobiography. So constructor takes instance of itself as parameter arguments
  2. There would be two constructor. Default constructor and cloning constructor
  3. Default constructor would be called first time object is created and cloning constructor would be called from clone method when the created object needs to be cloned
  4. There wont be any person who can tell your ancestral origin, likewise constructor would call super constructor at time of cloning
  5. The cloning constructor itself would be called from clone method

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

  1. In Factory Pattern we have Product(Abstract), ConcreteProduct and Creator(Abstract), ConcreteCreator
  2. ConcreteCreator would create ConcreteProduct by implementing abstract factory method of Creator which has Product return type
  3. Incase if there is any new Product to be added it fully supports Open Closed Principle(Open For Extension, Closed for Changes).
  4. Open for Extension – Adding new ConcreteProduct and ConcreateCreator class, Closed for Changes – No changes in anyother code unlike Simple factory or static factory method which requires change in Switchcase, enum (or) if case
  5. Closed for Changes – No changes in anyother code unlike Simple factory or static factory method which requires change in Switchcase, enum (or) if case

JobProfile.java

abstract class JobProfile {
    public abstract String mandatorySkills();

    public Integer defaultWorkHrs(){
        return 8;
    }
}

JavaProfile.java

public class JavaProfile extends JobProfile{
    @Override
    public String mandatorySkills() {
        return "Java, Springboot, Microservices";
    }
}

SQLProfile.java

public class SQLProfile extends JobProfile{
    @Override
    public String mandatorySkills() {
        return "Cosmos, MySQL, MSSQL";
    }
}

JobProfileCreator.java

abstract class JobProfileCreator {
    public JobProfile getJobProfile(){
        JobProfile objJobProfile = createJobProfileFactory();
        return objJobProfile;
    }

    public abstract JobProfile createJobProfileFactory();
}

JavaProfileCreator.java

public class JavaProfileCreator extends JobProfileCreator {
    @Override
    public JobProfile createJobProfileFactory() {
       return new JavaProfile();
    }
}

SQLProfileCreator.java

public class SQLProfileCreator extends JobProfileCreator {
    @Override
    public JobProfile createJobProfileFactory() {
        return new SQLProfile();
    }
}

Consultancy.java

public class Consultancy {
    public static void main(String[] args) {
        getProfileDetails(new JavaProfileCreator());
    }

    public static void getProfileDetails(JobProfileCreator jobProfileCreator){
        JobProfile objJobProfile = jobProfileCreator.getJobProfile();
        System.out.println(objJobProfile.mandatorySkills() + " with "+ objJobProfile.defaultWorkHrs() + "hrs of Work");
    }
}

Output

Java, Springboot, Microservices with 8hrs of Work

  1. In Simple Factory we have a Factory Class(LoggerFactory.java) and We call the createLogger method which returns different implementation of logger
  2. Logger is a abstract class which has different implementations

Logger.java

public abstract class Logger {
  abstract void log(String logstring);
}

ConsoleLogger.java

public class ConsoleLogger extends Logger{
    @Override
    void log(String logstring) {
        System.out.println("Logging to Console - "+ logstring);
    }
}

DBLogger.java

public class DBLogger extends Logger{
    @Override
    void log(String logstring) {
        System.out.println("Logging to Database - "+ logstring);
    }
}

FileLogger.java

public class FileLogger extends Logger{
    @Override
    void log(String logstring) {
        System.out.println("Logging to File - "+ logstring);
    }
}

LoggerFactory.java

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

    //The same code could be written using if else block instead of switch case
    public Logger createLogger(LoggerType loggerType) {
        Logger logger;

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

        return logger;
    }
}

ClientApp.java

public class ClientApp {
    public static void main(String[] args) {
        LoggerFactory objLoggerFactory = new LoggerFactory();
        Logger logger = objLoggerFactory.createLogger(LoggerFactory.LoggerType.CONSOLE);
        logger.log("Hello there");
    }
}

Output

Logging to Console - Hello there

Factory allows the consumer to create new objects without having to know the details of how they’re created, or what their dependencies are – they only have to give the information they actually want.

Account.java

abstract class Account {
   abstract Integer calculateInterest();
}

CreditAccount.java

public class CreditAccount extends Account{
    @Override
    Integer calculateInterest() {
        return 11;
    }
}

SalaryAccount.java

public class SalaryAccount extends Account{
    @Override
    Integer calculateInterest() {
        return 5;
    }
}

SavingsAccount.java

public class SavingsAccount extends Account{
   @Override
    Integer calculateInterest() {
        return 7;
    }
}

AccountFactory.java

public class AccountFactory {
    static Map<String, Account>  hmAccountMap =  new HashMap<>();

    static {
        hmAccountMap.put("SavingsAcc", new SavingsAccount());
        hmAccountMap.put("CreditAcc", new CreditAccount());
        hmAccountMap.put("SalaryAcc", new SalaryAccount());
    }

    public static Account getAccount(String accountType){
        return hmAccountMap.get(accountType);
    }
}

CalcInterest.java

public class CalcInterest{
    public static void main(String[] args) {
        Account objAccountFactory = AccountFactory.getAccount("SavingsAcc");
        System.out.println("Interest rate is - " + Optional.of(objAccountFactory.calculateInterest()));
    }
}

Using Streams for AccountFactory Class
AccountFactory.java

public static Optional<Account> getAccount(String accountType) {
        return hmAccountMap.entrySet().stream()
                                      .filter(accParam -> accParam.getKey().equals(accountType))
                                      .findFirst()
                                      .map(Map.Entry::getValue);
}

CalcInterest.java

public class CalcInterest{
    public static void main(String[] args) {
        Account objAccountFactory = AccountFactory.getAccount("SavingsAcc");
        System.out.println("Interest rate is - " + Optional.of(objAccountFactory.calculateInterest()));
    }
}

Output

Interest rate is - 7

Static factory method is a static method that returns an instance of a class. The key idea is to gain control over object creation and delegate it from constructor to static method.

The key idea of static factory method is to gain control over object creation and delegate it from constructor to static method. The decision of object to be created is like in Factory made outside the method (in common case, but not always). While the key (!) idea of Factory Method is to delegate decision of what instance of class to create inside Factory Method. E.g. classic Singleton implementation is a special case of static factory method. Example of commonly used static factory methods:

  • valueOf
  • getInstance(used in singleton)
  • newInstance

When to use?

  1. Static factory methods can have meaningful names, hence explicitly conveying what they do
  2. Static factory methods can return the same type that implements the method(s), a subtype, and also primitives, so they offer a more flexible range of returning types
  3. Static factory methods can encapsulate all the logic required for pre-constructing fully initialized instances, so they can be used for moving this additional logic out of constructors. This prevents constructors from performing further tasks, others than just initializing fields

Another example of static factory is as follow

Optional<String> value1 = Optional.empty();
Optional<String> value2 = Optional.of("Baeldung");
Optional<String> value3 = Optional.ofNullable(null);

Logger.java

public class Logger {
    public String logType;
    public String fileLocation;

    public Logger(String logType) {
        this.logType = logType;
    }

    public Logger() {
    }

    public static Logger getDefaultLogger(){
        return new Logger("Console");
    }

    public static Logger getFileLogger(String fileLocation){
        Logger logger =  new Logger("File");
        logger.fileLocation = fileLocation;
        return logger;
    }

    @Override
    public String toString() {
        return "Logger{" +
                "logType=" + logType +
                ", fileLocation='" + fileLocation + '\'' +
                '}';
    }
}

ClientApp.java

public class ClientApp {
    public static void main(String[] args) {
        Logger objLogger = Logger.getDefaultLogger();
        System.out.println(objLogger);
    }
}

Output

Logger{logType=Console, fileLocation='null'}

Logger.java

public class Logger {
    public String logType;
    public String fileLocation;

    public Logger(String logType) {
        this.logType = logType;
    }

    public Logger() {
    }

    public static Logger getLoggerInstance(LoggerType loggerType){
        Logger logger;
        switch(loggerType) {
            case CONSOLE:
                logger = new Logger("Console Logger");
                logger.fileLocation = "JVM Memory";
                break;
            case DATABASE:
                logger = new Logger("Database Logger");
                logger.fileLocation = "DB Connection";
                break;
            case FILE:
                logger = new Logger("File Logger");
                logger.fileLocation = "C:/logs";
                break;
            case SPLUNK:
                logger = new Logger("Splunk Logger");
                logger.fileLocation = "Splunk URL";
                break;
            default:
                logger = new Logger();

        }

        return logger;
    }

    @Override
    public String toString() {
        return "Logger{" +
                "logType=" + logType +
                ", fileLocation='" + fileLocation + '\'' +
                '}';
    }
}

ClientApp.java

public static void main(String[] args) {
        Logger objLogger = Logger.getLoggerInstance(LoggerType.CONSOLE);
        System.out.println(objLogger);
    }

LoggerType.java

public enum LoggerType {
    CONSOLE, FILE, DATABASE, SPLUNK
}

Output

Logger{logType=Console Logger, fileLocation='JVM Memory'}

Simple Factory and Static Factory are not design patterns rather coding style. However factory pattern could be implemented by using any one of the coding style but they wont honor Open Closed Principle.

As per Open Closed Principle, Code should be open to extension and Closed to Changes. In both the above coding style we have to change either the if else or switch case if new implementation of abstract class is introduces.

Factory Pattern is a design pattern in object-oriented programming that lets subclasses decide which class to instantiate when creating an object

Now Factory pattern honors Open Closed Principle and does not need change to factory method

Factory Pattern vs Abstract Factory Pattern
One difference between the two is that with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation

Abstract Factory Pattern
The Abstract Factory Pattern consists of an AbstractFactory, ConcreteFactory, AbstractProduct, ConcreteProduct and Client. We code against AbstractFactory and AbstractProduct and let the implementation to others to define ConcreteFactory and ConcreteProduct

Why Abstract Factory Introduced?
Another layer of abstraction over factory pattern. Abstract Factory patterns work around a super-factory which creates other factories. Factory pattern deals with creating objects of a single type(AppleFactory manufactures Iphones), while the Abstract Factory pattern deals with creating objects of related types(MobileFactory manufactures Mobiles). Abstract Factory pattern is more robust and consistent and uses composition to delegate responsibility of object creation.

When to use Abstract Factory?
When higher level of Abstraction is required.Provides an interface for creating families of related objects

MobileManufactureFactory.java

public interface MobileManufactureFactory {
    public Mobiles getManufacturerDetails();
}

Mobiles.java

public interface Mobiles {
    public void getMobileModels();
}

AppleFactory.java

public class AppleFactory implements MobileManufactureFactory {
    @Override
    public Mobiles getManufacturerDetails() {
        return new Iphone();
    }
}

GoogleFactory.java

public class GoogleFactory implements MobileManufactureFactory {
    @Override
    public Mobiles getManufacturerDetails() {
        return new Android();
    }
}

MicrosoftFactory.java

public class MicrosoftFactory implements MobileManufactureFactory {
    @Override
    public Mobiles getManufacturerDetails() {
        return new Windows();
    }
}

IphoneMobiles.java

public class IphoneMobiles implements Mobiles {
    @Override
    public void getMobileModels() {
        System.out.println("Iphone 13,14 and 15");
    }
}

AndroidMobiles.java

public class AndroidMobiles implements Mobiles {
    @Override
    public void getMobileModels() {
        System.out.println("Oneplus, Realme, Samsung");
    }
}

WindowsMobile.java

public class WindowsMobile implements Mobiles {
    @Override
    public void getMobileModels() {
        System.out.println("Samsung Focus, Nokia Lumia, Pocket PC");
    }
}

MobileFactory.java

public class MobileFactory {
    private MobileFactory(){
    }

    public static MobileManufactureFactory getMobilesBasedOnManufacturer(MobileCompany manufacturerName){
        if(manufacturerName.equals(MobileCompany.APPLE)){
            return new AppleFactory();
        }else if(manufacturerName.equals(MobileCompany.MICROSOFT)){
            return new MicrosoftFactory();
        }else if(manufacturerName.equals(MobileCompany.GOOGLE)){
            return new GoogleFactory();
        }
        return null;
    }
}

MobileCompany.java

public enum MobileCompany {
    APPLE, MICROSOFT, GOOGLE
}

Client.java

public class Client {
    public static void main(String[] args) {
        MobileManufactureFactory objMobileManufa = MobileFactory.getMobilesBasedOnManufacturer(MobileCompany.APPLE);
        Mobiles objMobile = objMobileManufa.getManufacturerDetails();
        objMobile.getMobileModels();
    }
}

Output

 Iphone 13,14 and 15

When Singleton should be used?
Singleton should be used incase creation of Object is costly or heavy I.E. DBConnection. It should not be used if the Object is mutable.

  1. Eager initialization
  2. Lazy initialization
  3. Static block initialization
  4. Using Synchronized method
  5. Using Synchronized block
  6. Double Checked Locking
  7. Enum Singleton
  8. Initialization on demand holder idiom

3 Things are common across all singleton implementation

private constructor
public static getInstance Method
private static instance field

Eager initialization
An instance of Singleton Class is created at the time of class loading

EagerInitialized.java

package com.mugil.singleton;

public class EagerInitialized {
	
	private static final EagerInitialized instance = new EagerInitialized();
	
	//Private Constructor
	private EagerInitialized()
	{
	}
	
	public static EagerInitialized getInstance()
	{
         return instance;
        }	
}

Lazy Initialization
With lazy initialization you crate instance only when its needed and not when the class is loaded

LazyInitialized.java

package com.mugil.singleton;

public class LazyInitialized 
{
	public static  LazyInitialized instance = null; 
	
	private LazyInitialized()
	{
		
	}
	
	public static LazyInitialized getInstance(){
        if(instance == null){
            instance = new LazyInitialized();
        }
        return instance;
    }
}

Static Block Initialization
Static block initialization is similar to eager initialization, except that an instance of class is created in the static block that provides an option for exception handling.

StaticBlockInitialized.java

package com.mugil.singleton;

public class StaticBlockInitialized {

	private static StaticBlockInitialized instance;

	private StaticBlockInitialized() {
	}

	// Static block initialization for exception handling
	static {
		try {
			instance = new StaticBlockInitialized();
		} catch (Exception e) {
			throw new RuntimeException("Exception occurred in creating singleton instance");
		}
	}

	public static StaticBlockInitialized getInstance() {
		return instance;
	}
}

Using Synchronized Method
The easier way to create a thread-safe singleton class is to make the access method synchronized, so that only one thread can execute this method at a time.

ThreadSafeSing.java

package com.mugil.singleton;

public class ThreadSafeSing {

    private static ThreadSafeSinginstance;
    
    private ThreadSafeSing(){}
    
    public static synchronized ThreadSafeSing getInstance(){
        if(instance == null){
            instance = new ThreadSafeSing();
        }
        return instance;
    }    
}

Using Synchronized Block
The easier way to create a thread-safe singleton class is to make the access method synchronized, so that only one thread can execute this method at a time. Refer the link for the same Link

ThreadSafeSing.java

package com.mugil.singleton;

public class ThreadSafeSing {
    private static ThreadSafeSinginstance;
    
    private ThreadSafeSing(){}
    
    public static ThreadSafeSing getInstance(){
        Synchronized(ThreadSafeSing.class){ 
          if(instance == null){
            instance = new ThreadSafeSing();
          }
        }
        return instance;
    }    
}

Double Checked Locking Singleton
Above Implementation of Singleton provides thread safety buy has a hit over performance. The performance hit happens when two threads try to get the instance of object in getInstance() method but only one is allowed within Synchronized block while the other needs to wait until the first is done checking irrespective of the instance is already created or not.

In the Below method of Implementing Singleton, we again use Synchronized Block but we do the check for the availability of the object instance even before taking lock.

DCCSingleton.java

package com.mugil.singleton;

public class DCCSingleton{

    private static DCCSingletoninstance;
    
    private DCCSingleton(){}
    
   public static DCCSingleton getInstanceUsingDoubleLocking()
   {
    if(instance == null){
        synchronized (DCCSingleton.class) {
            if(instance == null){
                instance = new DCCSingleton();
            }
        }
    }
    return instance;
  }    
}

Refer the Link for how Double Checked Locking works

ENUM Singleton
enum fields are compile time constants, but they are instances of their enum type. And, they’re constructed when the enum type is referenced for the first time.
ENUMSingleton.java
Simple Singleton using enum for DBConnection

public enum EnumSingleton
{
    INSTANCE;

    // instance vars, constructor
    private final Connection connection;

    private Singleton()
    {
        //Initialize the connection
        connection = DB.getConnection();
    }

    public Connection getConnection(){
       return connection;
    }

    // Static getter
    public static Singleton getInstance()
    {
        return INSTANCE;
    }
}

The way it works we can either use getInstance() method or directly call EnumSingleton.INSTANCE to access connection Object.

The Connection can be created using the Below Code

  Connection Conn = Singleton.getInstance().getConnection();

Minimal Implementation using ENUM

public enum MySingleton {
  INSTANCE;   
}

In the above code we may get a doubt how the object is created as we have a empty private constructor which is never invoked?.

The above code gets converted as one below. Above code has an implicit empty constructor. Lets make it explicit instead,

public enum MySingleton {
    INSTANCE;
    private MySingleton() {
        System.out.println("Here");
    }
}

Now when we make a call as below the private constructor is invoked and object gets created

public static void main(String[] args) {
    System.out.println(MySingleton.INSTANCE);
}

Output

Here
INSTANCE

Initialization on demand holder idiom
How it works?

  1. The Below implementation is a lazy loading and thread safe method of creating Singleton
  2. On calling the getInstance() method the lazySingletonClass would be initialized which in turn makes the INSTANCE to be initialized.
  3. Class initialization is inherently thread-safe and if you can have an object initialized on class initialization the object creation too are thread-safe.
  4. Singleton instance variable will never be created and or initialized until getInstance() is invoked. And again since class initialization is thread-safe the instance variable of IntiailizationOnDemandClassholder will be loaded safely, once and is visible to all threads.
public class Singleton {
    private Singleton(){}

    public static class lazySingletonClass{
      private static final Singleton INSTANCE = new Singleton();
    }

    public Singleton getInstance(){
        return lazySingletonClass.INSTANCE;
    }
}
When Object is Created Thread Safe Performance Comments
Eager Initialization Object Gets Created once the Class is Loaded Yes Bad Object is created everytime when the class is accessed
Lazy Initialization Object is Created once required No In lazy initialization you give a public API to get the instance. In multi-threaded environment it poses challenges to avoid unnecessary object creation
Static Block Initialization Object is Created once Static Block is loaded Yes Bad Not a good idea to load resources from static block as it causes performance hit during app startup
Synchronized Method Object is Created once getInstance is called Yes Bad Thread safety is guaranteed.Slow performance because of whole method locking is done
Synchronized Block Object is Created once getInstance is called No Two singleton instances may be created when context switching happens after checking instance is null
Double Checked Locking Object is Created once getInstance is called Yes Good Instance check is done twice
Enum Implementation Object Created using enum is referenced Yes Good
Initialization on demand holder idiom Object Created when static class is initiazlied by calling getInstance method Yes Good Class initialization is inherently thread-safe and if you can have an object initialized on class initialization the object creation too are thread-safe

Note
Dont be confused between Class Loading and Initialization. In the Initialization on demand holder method of singleton you can easily get confused as the static inner class wont be loaded when the outer class loads. The implementation relies on the fact that during initialization phase of execution within the Java Virtual Machine (JVM) as specified by the Java Language Specification (JLS) When the class Singleton is loaded by the JVM, the class goes through initialization. Since the class does not have any static variables to initialize, the initialization completes trivially. The static class definition lazySingletonClasswithin it is not initialized until the JVM determines that lazySingletonClassmust be executed. The static class lazySingletonClassis only executed when the static method getInstance is invoked on the class

The static class lazySingletonClass is only executed when the static method getInstance is invoked on the class Singleton , and the first time this happens the JVM will load and initialize the lazySingletonClass class. The initialization of the lazySingletonClassclass results in static variable INSTANCE being initialized by executing the (private) constructor for the outer class Something. Since the class initialization phase is guaranteed by the JLS to be sequential, i.e., non-concurrent, no further synchronization is required in the static getInstance method during loading and initialization

Simple Singleton Using ENUM

MySingleton.java

public enum MySingleton {
  INSTANCE;   
}

Enum Classes has Private Constructor by Default

The Above code can be explicitly written as

MySingleton.java

public enum MySingleton {
    public final static MySingleton INSTANCE = new MySingleton();
    private MySingleton() {
    }
}

When your code first accesses INSTANCE, the class MySingleton will be loaded and initialized by the JVM. This process initializes the static field above once (lazily).

Why cant enum constructors be protected or public in Java?
Enums as a class with a finite number of instances. There can never be any different instances beside the ones you initially declare.Thus, you cannot have a public or protected constructor, because that would allow more instances to be created.