Algo-Sim › Design Patterns
Strategy: swappable discounts
Easy · Design Patterns · step through it in Java, Python and C++ on your own input
Put each interchangeable algorithm behind one interface, and let the context hold whichever one it needs. Swapping behaviour becomes swapping an object.
Problem
A checkout charges pay(price) using whichever discount policy is currently set: none, a percentage off, or a flat amount off (never below 0). The policy can be swapped at runtime. Design it so that adding a new policy never changes the checkout class. pay returns the amount charged.
Constraints
- Each discount policy is one class behind a shared interface
- The checkout holds a policy object and delegates to it; no if/else on the policy type
- Swapping a policy is O(1)
Edge cases to test
- No strategy set yet (pick a default or fail clearly)
- Swapping the strategy between two calls
- A strategy that needs extra data
Follow-up questions
- How is it different from State?
- Strategy is chosen from outside and the strategies don’t know each other; in State the states pick the next state themselves.
- Do you need classes in Java 8+ or Python?
- Often not: a function or lambda can be the strategy when it has no state of its own.
Solution
Java
interface Discount {
int apply(int price);
}
class NoDiscount implements Discount {
public int apply(int price) { return price; }
}
class PercentOff implements Discount {
private final int percent;
PercentOff(int percent) { this.percent = percent; }
public int apply(int price) { return price - price * percent / 100; }
}
class FlatOff implements Discount {
private final int amount;
FlatOff(int amount) { this.amount = amount; }
public int apply(int price) { return Math.max(0, price - amount); }
}
class Checkout {
private Discount discount = new NoDiscount();
public void setDiscount(Discount discount) {
this.discount = discount;
}
public int pay(int price) {
return discount.apply(price);
}
}Python
class NoDiscount:
def apply(self, price):
return price
class PercentOff:
def __init__(self, percent):
self.percent = percent
def apply(self, price):
return price - price * self.percent // 100
class FlatOff:
def __init__(self, amount):
self.amount = amount
def apply(self, price):
return max(0, price - self.amount)
class Checkout:
def __init__(self):
self.discount = NoDiscount()
def set_discount(self, discount):
self.discount = discount
def pay(self, price):
return self.discount.apply(price)C++
#include <bits/stdc++.h>
using namespace std;
struct Discount {
virtual int apply(int) const = 0;
virtual ~Discount() = default;
};
struct NoDiscount : Discount {
int apply(int price) const override {
return price;
} /* no discount */
};
struct PercentOff : Discount {
int percent;
explicit PercentOff(int p)
: percent(p) {}
int apply(int price) const override {
return price - price * percent / 100;
}
};
struct FlatOff : Discount {
int amount;
explicit FlatOff(int a)
: amount(a) {}
int apply(int price) const override {
return max(0, price - amount);
}
};
class Checkout {
unique_ptr<Discount> discount = make_unique<NoDiscount>();
public:
void setDiscount(unique_ptr<Discount> next) {
discount = move(next);
}
int pay(int price) {
return discount->apply(price);
}
};
More Design Patterns problems
- Observer: publish and subscribe (Easy)
- State: a document workflow (Easy)
- Command: undo and redo (Medium)
- Chain of Responsibility: an ATM (Easy)
- Factory: create without naming classes (Easy)
- Singleton: one shared instance (Easy)
- Builder: a pizza step by step (Easy)
- Decorator: stack toppings at runtime (Easy)
- Adapter: two legacy sensors, one interface (Easy)
- Proxy: a caching, lazy price service (Easy)
- Composite: folder sizes (Easy)