← Back to PatternPip

STATE / THE WALKTHROUGH

Follow the order.
Find a home for its rules.

Start with one payment check. Add lifecycle rules, then see what changes when each stage owns its behavior.

CHAPTER 1 OF 3

One order. Has it been paid?

A tiny checkout only records payment. A boolean prevents a second payment. For this requirement, a small check is clear and sufficient.

A checkout with one responsibility

This first version only records whether payment happened. It has no shipping or cancellation rules yet.

No payment recorded yet.

A boolean is enough here · TypeScript
export class CheckoutOrder {
  private paid = false;
  pay(): boolean {
    if (this.paid) return false;
    this.paid = true;
    return true;
  }
}

Chapter changes start a fresh example. Theme and language preserve the current step.