Singleton, Factory, Abstract Factory

Singleton, Factory, Abstract Factory

In the Java programming language, the Singleton pattern, Factory pattern, and Abstract Factory pattern are creational design patterns in an object-oriented programming environment. They help Java developers control and manage how new class objects are created.

Singleton, Factory, Abstract Factory

Singleton pattern concept in Java oops.

The Singleton pattern in Java class programming helps ensure that only one object (instance) of a class is created in an entire user-defined application and provides a global method for accessing them.

Instances for database connections, loggers, and configuration managers.

Example of the Singleton pattern concept in Java.

class Singleton {

private static Singleton instance;

// Here we create a private constructor method

private Singleton() {

}

// Here we use a public method to get the single instance

public static Singleton getInstance() {

if (instance == null) {

instance = new Singleton();

return instance;

}

}

public class Main {

public static void main(String[] args) {

Singleton sample1 = Singleton.getInstance();

Singleton sample2 = Singleton.getInstance();

System.out.println(sample1 == sample2);

}

}

}

Here’s the main idea of ​​the Singleton pattern in this example.

A Singleton pattern creates a class → a sample object.

Factory pattern concept in Java oops.

The factory pattern in a Java program creates a class object without exposing the class object-creation logic to the Java client user.

For example, in the Java user factory pattern, instead of directly creating different types of Companies, a factory method is used to create an Employee organization.

Example of the Factory pattern concept in Java.

interface Company {

void dept();

}

class Employee implements Company {

@Override

public void dept() {

System.out.println(“Department of Employee”);

}

}

class Manager implements Company {

@Override

public void dept() {

System.out.println(“Department of Manager”);

}

}

class CompanyFactory {

public static Company createCompany(String type) {

if (type.equalsIgnoreCase(“Employee”)) {

return new Employee();

}

else if (type.equalsIgnoreCase(“Manager”)) {

return new Manager();

}

return null;

}

}

public class Main {

public static void main(String[] args) {

Company sample1 = CompanyFactory.createCompany(“Employee”);

Company sample2 = CompanyFactory.createCompany(“Manager”);

sample1.dept();

sample2.dept();

}

}

Used in the Java Factory pattern.

Company c = CompanyFactory.createCompany(“employee”);

c.dept();

Here’s the Factory pattern’s main idea in this example.

A Java Factory pattern → creates different types of related class objects within a user-defined class

Abstract Factory pattern concept in Java oops.

The abstract factory class pattern in Java programs is a single factory method for multiple factories. The abstract factory class pattern concept in Java programs is used when a Java developer needs to create a family of related class objects.

For example, suppose a company or bank supports several different work culture methods in an employee application.

Example of Abstract Factory pattern concept in Java.

// Here we define an Abstract Product 1

interface Employee {

void work();

}

// Here we define an Abstract Product 2

interface Manager {

void manage();

}

// Here we define an Abstract Factory method

interface CompanyFactory {

Employee createEmployee();

Manager createManager();

}

//here we create an IT Employee

class ITEmployee implements Employee {

public void work() {

System.out.println(“IT Employee develops software”);

}

}

//here we create an IT Manager

class ITManager implements Manager {

public void manage() {

System.out.println(“IT Manager manages software projects”);

}

}

//here we create a Banking Employee

class BankEmployee implements Employee {

public void work() {

System.out.println(“Bank Employee handles customer accounts”);

}

}

//here we create a Banking Manager

class BankManager implements Manager {

public void manage() {

System.out.println(“Bank Manager manages banking daily operations”);

}

}

// here we create an IT Company Factory method

class ITCompanyFactory implements CompanyFactory {

public Employee createEmployee() {

return new ITEmployee();

}

public Manager createManager() {

return new ITManager();

}

}

// here we create a Banking Company Factory method

class BankCompanyFactory implements CompanyFactory {

public Employee createEmployee() {

return new BankEmployee();

}

public Manager createManager() {

return new BankManager();

}

}

//here we create a Main Class

public class Main {

public static void main(String[] args) {

// here we create an IT Company elements object

CompanyFactory factory = new ITCompanyFactory();

Employee emp = factory.createEmployee();

Manager mgr = factory.createManager();

emp.work();

mgr.manage();

System.out.println(“——–#########———-“);

// Here we create a Banking Company element object

factory = new BankCompanyFactory();

emp = factory.createEmployee();

mgr = factory.createManager();

emp.work();

mgr.manage();

}

}

Here’s the main idea of ​​the Abstract Factory pattern in this example.

A class Factory pattern → Creates a family of related class objects.

Main Difference Between Singleton, Factory, And Abstract Factory Method

#Pattern MethodPurpose of Each Pattern MethodExample
1.Singleton patternIt helps user to create only one instance in single classLogger events
2.Factory patternIt helps to create one type of class object, hiding class creation logic expression when user needEmployee/Department factory
3.Abstract Factory patternIt helps java user to create families of related class objectsCompany/Works elements

Conclusion of Singleton, Factory, and Abstract Factory.

  • Singleton pattern – Creates a single object in a Java class.
  • Factory pattern – It helps to create a factory → individual products in a Java class.
  • Abstract factory pattern – It helps to create a factory → family of products in a Java class with multiple individual class object methods.

Leave a Reply