人気の面接質問と回答・オンラインテスト
面接対策、オンラインテスト、チュートリアル、ライブ練習のための学習プラットフォーム

集中型学習パス、模擬テスト、面接向けコンテンツでスキルを伸ばしましょう。

WithoutBook は、分野別の面接質問、オンライン練習テスト、チュートリアル、比較ガイドをひとつのレスポンシブな学習空間にまとめています。

Chapter 3

Creational Patterns Part 2: Abstract Factory, Builder, and Prototype

Go deeper into object creation with families of related objects, readable object construction, and cloning-based creation strategies.

Inside this chapter

  1. Abstract Factory Pattern
  2. Builder Pattern
  3. Prototype Pattern
  4. How to Choose Between Them
  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 3

Abstract Factory Pattern

Abstract Factory creates families of related objects without exposing concrete implementations to the client. It is useful when a system must work with consistent groups of products, such as UI components, cloud vendor adapters, themeable widgets, or environment-specific services.

interface Button {
    void render();
}

interface Dialog {
    void show();
}

interface UiFactory {
    Button createButton();
    Dialog createDialog();
}

A web UI factory and desktop UI factory can then produce matching button and dialog implementations without the calling code knowing the concrete classes.

Chapter 3

Builder Pattern

Builder solves the problem of constructing objects with many optional fields or validation rules. It improves readability, avoids telescoping constructors, and keeps creation expressive.

public class User {
    private final String username;
    private final String email;
    private final boolean admin;

    private User(Builder builder) {
        this.username = builder.username;
        this.email = builder.email;
        this.admin = builder.admin;
    }

    public static class Builder {
        private final String username;
        private String email;
        private boolean admin;

        public Builder(String username) {
            this.username = username;
        }

        public Builder email(String email) {
            this.email = email;
            return this;
        }

        public Builder admin(boolean admin) {
            this.admin = admin;
            return this;
        }

        public User build() {
            return new User(this);
        }
    }
}
Chapter 3

Prototype Pattern

Prototype creates objects by copying existing ones. It becomes useful when object setup is expensive, configuration is complex, or many similar instances must be derived from a baseline. Java cloning APIs exist, but many teams prefer explicit copy constructors or factory methods because they are clearer and safer than raw clone().

Chapter 3

How to Choose Between Them

Pattern Best Fit
Abstract FactoryNeed related product families that must remain compatible.
BuilderNeed readable construction for complex objects.
PrototypeNeed to derive new objects from configured existing ones.
Chapter 3

Real-World Usage Snapshot

Builders are extremely common in modern Java libraries, DTO design, test data setup, and immutable domain models. Abstract Factory appears in platform abstraction layers. Prototype is less visible by name, but copy-based construction is common in configuration templates, message objects, and cached baseline objects.

著作権 © 2026、WithoutBook。