Most asked top Interview Questions and Answers & Online Test
Education platform for interview prep, online tests, tutorials, and live practice

Build skills with focused learning paths, mock tests, and interview-ready content.

WithoutBook brings subject-wise interview questions, online practice tests, tutorials, and comparison guides into one responsive learning workspace.

Chapter 2

Creational Patterns Part 1: Singleton and Factory Method

Understand how object creation choices shape design through Singleton and Factory Method, including their correct use cases, misuse cases, and Java examples.

Inside this chapter

  1. Why Creational Patterns Matter
  2. Singleton Pattern
  3. Factory Method Pattern
  4. Tradeoffs and Misuse
  5. Real-World Usage Snapshot

Series navigation

Study the chapters in order for the clearest path from first design principles to advanced Java architecture, framework usage, and interview-level pattern mastery. Use the navigation at the bottom of the page to move through the full tutorial smoothly.

Tutorial Home

Chapter 2

Why Creational Patterns Matter

Object creation is often treated as trivial by beginners, but large systems reveal that construction decisions influence lifecycle, testing, dependency management, configuration, and extensibility. Creational patterns focus on how objects are created and how client code can remain decoupled from concrete classes.

Chapter 2

Singleton Pattern

The Singleton pattern ensures one instance of a class and provides controlled access to it. It is useful when a truly shared, process-wide concept exists, such as configuration, registry-like metadata, or certain caches. It is harmful when used as disguised global state.

public final class AppConfig {
    private static final AppConfig INSTANCE = new AppConfig();

    private AppConfig() {
    }

    public static AppConfig getInstance() {
        return INSTANCE;
    }
}

In modern Java, an enum-based singleton is often the safest implementation because it handles serialization and reflection concerns better.

Chapter 2

Factory Method Pattern

Factory Method moves object creation behind a method so subclasses or collaborators can decide which implementation to instantiate. This helps when creation depends on environment, data, configuration, or extension points.

public interface Notification {
    void send(String message);
}

public class EmailNotification implements Notification {
    public void send(String message) {
        System.out.println("Email: " + message);
    }
}

public abstract class NotificationCreator {
    public abstract Notification createNotification();

    public void deliver(String message) {
        Notification notification = createNotification();
        notification.send(message);
    }
}

public class EmailNotificationCreator extends NotificationCreator {
    @Override
    public Notification createNotification() {
        return new EmailNotification();
    }
}
Chapter 2

Tradeoffs and Misuse

  • Singleton can hurt testability and hide dependencies.
  • Factory Method can add unnecessary hierarchy if creation is simple.
  • Dependency injection frameworks often reduce the need for hand-written singleton and factory logic.
  • Use the pattern only when variability or lifecycle control justifies it.
Chapter 2

Real-World Usage Snapshot

Spring uses bean scopes and dependency injection to manage many responsibilities that older Java systems solved with manual singleton or factory code. Still, understanding these patterns helps developers reason about framework behavior and make better architectural choices outside the container as well.

Copyright © 2026, WithoutBook.