🔹 Creational Design Patterns in OOP
**Creational Design Patterns** focus on the **creation of objects** in a **controlled and efficient manner**. These patterns help manage object creation, improve **flexibility**, and reduce **tight coupling** between classes.
📌 Why Use Creational Patterns?
Creational patterns help in handling **complex object creation logic**.
- ✅ **Encapsulates Object Creation** – Hides object construction details.
- ✅ **Improves Code Maintainability** – Makes code more reusable.
- ✅ **Reduces Code Duplication** – Ensures consistent object creation.
- ✅ **Enhances Scalability** – Helps manage object dependencies efficiently.
📌 Common Creational Patterns
🔹 1. Singleton Pattern
Ensures a **class has only one instance** and provides a **global access point**.
class Singleton {
private static Singleton instance;
private Singleton() {} // Private constructor prevents instantiation
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
// Usage
public class Main {
public static void main(String[] args) {
Singleton obj1 = Singleton.getInstance();
Singleton obj2 = Singleton.getInstance();
System.out.println(obj1 == obj2); // ✅ Output: true (Same instance)
}
}
🔹 2. Factory Pattern
Provides an **interface for creating objects** but allows subclasses to decide which class to instantiate.
interface Shape {
void draw();
}
class Circle implements Shape {
public void draw() {
System.out.println("Drawing a Circle");
}
}
class Square implements Shape {
public void draw() {
System.out.println("Drawing a Square");
}
}
class ShapeFactory {
public static Shape getShape(String type) {
if (type.equalsIgnoreCase("Circle")) return new Circle();
if (type.equalsIgnoreCase("Square")) return new Square();
return null;
}
}
// Usage
public class Main {
public static void main(String[] args) {
Shape shape1 = ShapeFactory.getShape("Circle");
shape1.draw(); // ✅ Output: Drawing a Circle
}
}
🔹 3. Builder Pattern
Separates **object construction** from its representation, useful for **creating complex objects**.
class Car {
private String brand;
private int speed;
private Car(CarBuilder builder) {
this.brand = builder.brand;
this.speed = builder.speed;
}
public static class CarBuilder {
private String brand;
private int speed;
public CarBuilder setBrand(String brand) {
this.brand = brand;
return this;
}
public CarBuilder setSpeed(int speed) {
this.speed = speed;
return this;
}
public Car build() {
return new Car(this);
}
}
}
// Usage
public class Main {
public static void main(String[] args) {
Car myCar = new Car.CarBuilder()
.setBrand("Toyota")
.setSpeed(120)
.build();
}
}
🔹 4. Prototype Pattern
Creates **new objects by copying an existing object**, useful for **cloning objects**.
class Prototype implements Cloneable {
private String name;
Prototype(String name) {
this.name = name;
}
public Prototype clone() throws CloneNotSupportedException {
return (Prototype) super.clone();
}
}
// Usage
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Prototype obj1 = new Prototype("Original");
Prototype obj2 = obj1.clone(); // ✅ Cloning
System.out.println(obj1 != obj2); // ✅ Output: true (Different objects)
}
}
🎯 Summary
Creational patterns **improve object creation efficiency** and **reduce dependencies**. Common patterns include **Singleton, Factory, Builder, and Prototype**.