Vymení chovanie objektu podľa toho, v akom stave sa práve nachádza.
class State { next(order) { throw new Error('abstraktná metóda'); }} class NewState extends State { next(order) { pay(); order.setState(new PaidState()); }} class PaidState extends State { next(order) { ship(); order.setState(new ShippedState()); }} class ShippedState extends State { next(order) { // final state, nothing switches any further }} class Order { #state = new NewState(); setState(state) { this.#state = state; } // Not a single condition. The state decides what happens next. next() { this.#state.next(this); }}