热门面试题与答案和在线测试
面向面试准备、在线测试、教程与实战练习的学习平台

通过聚焦学习路径、模拟测试和面试实战内容持续提升技能。

WithoutBook 将分主题面试题、在线练习测试、教程和对比指南整合到一个响应式学习空间中。

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.

版权所有 © 2026,WithoutBook。