Java Design Patterns

Short Answer:
Design Patterns are proven solutions to common software design problems. They improve scalability, flexibility, and maintainability.


1️⃣ Types of Design Patterns

Category Purpose Examples
Creational Object creation Singleton, Factory, Builder
Structural Object structure Proxy, Decorator
Behavioral Object behavior Observer, Strategy

2️⃣ Creational Patterns

🔹 Singleton Pattern

Purpose: Only one instance allowed.

class Singleton {
    private static final Singleton instance = new Singleton();
    private Singleton() {}
    public static Singleton getInstance() {
        return instance;
    }
}

Real Use: Logger, Configuration.


🔹 Factory Pattern

Purpose: Hide object creation logic.

interface Shape { void draw(); }

class Circle implements Shape {
    public void draw() { System.out.println("Circle"); }
}

class ShapeFactory {
    public static Shape getShape(String type) {
        if("circle".equals(type)) return new Circle();
        return null;
    }
}

Real Use: Spring BeanFactory.


🔹 Builder Pattern

Purpose: Create complex object step-by-step.

class User {
    private String name;
    private int age;

    private User(Builder builder) {
        this.name = builder.name;
        this.age = builder.age;
    }

    static class Builder {
        private String name;
        private int age;

        Builder setName(String name) {
            this.name = name;
            return this;
        }

        Builder setAge(int age) {
            this.age = age;
            return this;
        }

        User build() {
            return new User(this);
        }
    }
}

Real Use: Lombok @Builder.


3️⃣ Structural Patterns

🔹 Proxy Pattern

Purpose: Control access to object.

interface Service { void request(); }

class RealService implements Service {
    public void request() { System.out.println("Real Service"); }
}

class ProxyService implements Service {
    private RealService service = new RealService();

    public void request() {
        System.out.println("Access Checked");
        service.request();
    }
}

Real Use: Spring AOP.


🔹 Decorator Pattern

Purpose: Add behavior dynamically.

interface Coffee { String make(); }

class SimpleCoffee implements Coffee {
    public String make() { return "Coffee"; }
}

class MilkDecorator implements Coffee {
    private Coffee coffee;
    MilkDecorator(Coffee coffee) { this.coffee = coffee; }
    public String make() {
        return coffee.make() + " + Milk";
    }
}

Real Use: Java I/O streams.


4️⃣ Behavioral Patterns

🔹 Observer Pattern

Purpose: Notify multiple subscribers.

interface Observer { void update(); }

class Subscriber implements Observer {
    public void update() {
        System.out.println("Notified");
    }
}

Real Use: Event-driven systems.


🔹 Strategy Pattern

Purpose: Interchangeable algorithms.

interface PaymentStrategy { void pay(); }

class CreditCardPayment implements PaymentStrategy {
    public void pay() {
        System.out.println("Paid by Card");
    }
}

Real Use: Payment gateway switching.


Frequently Asked Interview Questions (With Answers)

1️⃣ How to make Singleton thread-safe?

class Singleton {
    private static volatile Singleton instance;
    private Singleton() {}

    public static Singleton getInstance() {
        if(instance == null) {
            synchronized(Singleton.class) {
                if(instance == null)
                    instance = new Singleton();
            }
        }
        return instance;
    }
}

2️⃣ Difference between Factory and Builder?

Factory creates object in one step. Builder constructs object step-by-step.


3️⃣ When to use Strategy Pattern?

When multiple algorithms exist for same functionality.


4️⃣ What pattern is used in Spring?

  • Singleton
  • Factory
  • Proxy
  • Observer

5️⃣ Difference between Decorator and Proxy?

Decorator adds new behavior. Proxy controls access.


Final Interview Summary

  • Singleton → One instance.
  • Factory → Hide creation logic.
  • Builder → Complex object construction.
  • Observer → Event notification.
  • Strategy → Switchable behavior.
  • Proxy → Access control.
  • Decorator → Extend functionality dynamically.

0 comments

Leave a comment