Strategy and State look structurally almost identical. What's the actual difference between them?
Both patterns involve a context class holding a reference to an interchangeable object and delegating to it, so a class diagram alone often can't distinguish them. The real difference is intent and who controls the switch. With Strategy, the caller explicitly chooses and sets which implementation to use — a PaymentService doesn't decide on its own to switch from credit card to UPI, some external code calls setPaymentStrategy() to make that choice. With State, the object's own internal logic decides when to transition, often without any external caller ever calling a setter — a vending machine's own state object might trigger a transition to a different state as a direct consequence of handling a request, with the caller having no idea a switch even happened.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response