TechTorch

Location:HOME > Technology > content

Technology

Exploring Factory Design Patterns in Java: From Simple to Singleton

March 22, 2025Technology2429
Introduction to Factory Design Patterns in Java In Java, factory desig

Introduction to Factory Design Patterns in Java

In Java, factory design patterns are creational design patterns that help manage and decouple object creation from the system's business logic. This article delves into four common factory design patterns: Simple Factory Pattern, Factory Method Pattern, Abstract Factory Pattern, and Singleton Factory Pattern, providing detailed explanations and practical examples for each.

1. Simple Factory Pattern

The Simple Factory Pattern is a common approach that allows a factory class to create objects based on input. It is not a formal design pattern but a widely used method to simplify object creation. The factory class contains a method that returns instances of different classes based on the input provided.

class Shape {
void draw{}
}
class Circle extends Shape {
void draw{}
}
class Square extends Shape {
void draw{}
}
class ShapeFactory {
public static Shape getShape(String shapeType) {
if (shapeType null) {
return null;
}
if (shapeType.equalsIgnoreCase("circle")) {
return new Circle();
} else if (shapeType.equalsIgnoreCase("square")) {
return new Square();
}
return null;
}

Usage Example:

Shape shape1  ("circle");
shape1.draw(); // Output: Drawing a Circle

2. Factory Method Pattern

The Factory Method Pattern defines an interface for creating objects but allows subclasses to alter the type of objects that will be created. This pattern is useful when there is a common interface for different product classes but different implementations of each product need to be created in subclasses.

abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {

}
class Square extends Shape {
void draw() {

}
abstract class ShapeFactory {
abstract Shape createShape();
}
class CircleFactory extends ShapeFactory {
Shape createShape() {
return new Circle();
}
}
class SquareFactory extends ShapeFactory {
Shape createShape() {
return new Square();
}
}

Usage Example:

ShapeFactory circleFactory  new CircleFactory();
Shape shape1 ();
shape1.draw(); // Output: Drawing a Circle

3. Abstract Factory Pattern

The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern is useful when a group of related objects (a product family) must be created in a uniform way.

interface Shape {
void draw();
}
class Circle implements Shape {
public void draw() {

}
class Square implements Shape {
public void draw() {

}
interface Color {
void fill();
}
class Red implements Color {
public void fill() {

}
class Blue implements Color {
public void fill() {

}
interface AbstractFactory {
Shape createShape();
Color createColor();
}
class ShapeFactory implements AbstractFactory {
public Shape createShape() {
return new Circle();
}
public Color createColor() {
return new Red();
}
}

Usage Example:

AbstractFactory factory  new ShapeFactory();
Shape shape ();
Color color ();
shape.draw(); // Output: Drawing a Circle
(); // Output: Filling with Red color

4. Singleton Factory Pattern

The Singleton Factory Pattern ensures that a class has only one instance and provides a global point of access to it. This pattern can be combined with a factory method to ensure controlled and unique object creation.

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

Usage Example:

Singletone singleton  ();

Summary

The Simple Factory Pattern, Factory Method Pattern, Abstract Factory Pattern, and Singleton Factory Pattern are critical tools in Java for managing object creation. Each pattern addresses different aspects of object creation and can provide significant benefits in terms of code organization, flexibility, and maintainability. By choosing the right pattern, developers can ensure that their code is clean, modular, and easily maintainable.