🧩State Pattern – Overview

🧠 Concept

The State Pattern allows an object to change its behavior when its internal state changes. It appears as if the object’s class changes at runtime.

πŸ‘‰ It removes complex if-else or switch statements for state transitions and encapsulates state-specific behavior into separate classes.


🧱 Key Roles

Role
Description

Context

The main object whose behavior changes depending on its state.

State (Interface)

Declares methods that represent behavior in a given state.

ConcreteState

Implements behavior specific to a particular state.


πŸ’» Java Example – Report Generator (States: Draft β†’ Review β†’ Published)

// Context
class ReportContext {
    private ReportState state;

    public ReportContext() {
        this.state = new DraftState(); // initial state
    }

    public void setState(ReportState state) {
        this.state = state;
    }

    public void request() {
        state.handle(this);
    }
}

// State interface
interface ReportState {
    void handle(ReportContext context);
}

// Concrete States
class DraftState implements ReportState {
    public void handle(ReportContext context) {
        System.out.println("Report in Draft β†’ Sending for Review...");
        context.setState(new ReviewState());
    }
}

class ReviewState implements ReportState {
    public void handle(ReportContext context) {
        System.out.println("Report under Review β†’ Publishing...");
        context.setState(new PublishedState());
    }
}

class PublishedState implements ReportState {
    public void handle(ReportContext context) {
        System.out.println("Report already Published. No further actions.");
    }
}

βœ… Usage Example:


🧠 Flow Summary (Step-by-Step)

Step
Current State
Action
Next State
Output

1️⃣

Draft

request()

Review

"Report in Draft β†’ Sending for Review..."

2️⃣

Review

request()

Published

"Report under Review β†’ Publishing..."

3️⃣

Published

request()

Published

"Report already Published. No further actions."


πŸͺœ Summary

  • Encapsulates state-specific behavior into separate classes.

  • Eliminates conditional logic for state transitions.

  • Makes adding new states easier without modifying the core logic.

  • Promotes Open/Closed and Single Responsibility principles.


πŸ“Š Real-world Analogy

πŸ“„ Document Workflow: A report changes its behavior as it moves through stages: Draft β†’ Review β†’ Published β€” each state has its own rules and actions.

Last updated