Grokking Design Patterns for Engineers and Managers
0% completed
State Pattern
The State Pattern lets an object change its behaviour when its internal state changes, instead of checking a status field in every method.
A subscription moves through trialing, active, past due, and cancelled. Every method starts the same way:
public void pay() { if (status == TRIALING) { /* take first payment, become active */ } else if (status == ACTIVE) { /* renew */ } else if (status == PAST_DUE) { /* catch up, become active */ } else if (status == CANCELLED) { /* reject */ } }
The same four branches appear again in `cancel()
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%