Java Strategy and State Patterns: Swap Behavior at Runtime
Maya's bookstore sells online now, and the checkout takes three kinds of payment — card, wallet, and cash on delivery. A fourth is coming: gift cards, for the holidays. Every new method means the same edit: another branch in the checkout's if-chain, another round of re-testing what already works. And the orders themselves are tracked with a status flag — a string that says NEW or PAID or SHIPPED — checked by an if-chain in every method. The checkout knows every payment method by name, and the order's life is a string anyone can rewrite. That is the problem. Post 1 asked what varies? Post 2, Java Singleton Pattern: Enum, Lazy Holder & Double-Checked Locking Explained , asked how many? Post 3, Java Factory Pattern: Factory Method vs Abstract Factory With Examples , asked who decides which object gets created? Post 4, Java Builder Pattern: Build Complex Objects Without Telescoping Constructors , asked how do you assemble one object, piece by piece? Post 5, Java Adapt...