가장 많이 묻는 면접 질문과 답변 & 온라인 테스트
면접 준비, 온라인 테스트, 튜토리얼, 라이브 연습을 위한 학습 플랫폼

집중 학습 경로, 모의고사, 면접 준비 콘텐츠로 실력을 키우세요.

WithoutBook은 주제별 면접 질문, 온라인 연습 테스트, 튜토리얼, 비교 가이드를 하나의 반응형 학습 공간으로 제공합니다.

Chapter 6

Behavioral Patterns Part 1: Strategy, State, and Template Method

Learn how to vary algorithms, model changing object behavior, and define reusable processing skeletons in Java applications.

Inside this chapter

  1. Strategy Pattern
  2. State Pattern
  3. Template Method
  4. Choosing Among 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 6

Strategy Pattern

Strategy encapsulates interchangeable algorithms behind a common interface. It is ideal when business rules vary by country, customer tier, payment mode, pricing policy, or sorting logic.

interface DiscountStrategy {
    double apply(double amount);
}

class PremiumDiscountStrategy implements DiscountStrategy {
    public double apply(double amount) {
        return amount * 0.8;
    }
}

class BillingService {
    private final DiscountStrategy strategy;

    BillingService(DiscountStrategy strategy) {
        this.strategy = strategy;
    }

    double finalAmount(double amount) {
        return strategy.apply(amount);
    }
}
Chapter 6

State Pattern

State models an object whose behavior changes based on internal state. Instead of large if or switch blocks, state-specific classes hold the behavior. Order processing, workflow engines, media playback, and user onboarding state machines are good candidates.

Chapter 6

Template Method

Template Method defines the structure of an algorithm in a base class while allowing subclasses to customize some steps. It is useful when workflows share common sequencing but vary in one or two steps.

abstract class DataExporter {
    public final void export() {
        readData();
        transform();
        writeOutput();
    }

    protected abstract void readData();
    protected abstract void transform();
    protected abstract void writeOutput();
}
Chapter 6

Choosing Among Them

Pattern Primary Force
StrategySwap algorithms or business rules.
StateChange behavior as object state evolves.
Template MethodReuse a stable process skeleton.
Chapter 6

Real-World Usage Snapshot

Pricing engines use strategy, workflow systems use state, and many framework hooks use template method. These patterns are central to writing code that remains easy to extend while keeping business logic readable.

Copyright © 2026, WithoutBook.