Give a real example of polymorphism in a production system, and explain why the design pays off as requirements change.
Here is a payment system, and it is a real shape that you find in most shops. The service takes a payment method, and it does not know what kind it is.
public class Main {
public static void main(String[] args) {
PaymentService service = new PaymentService();
service.pay(new CardPayment("Ada", 25.0));
service.pay(new WalletPayment("Ada", 10.0));
}
}
And here is the interface, the service and two of the implementations.
public interface PaymentMethod {
boolean pay(double amount);
}
public class CardPayment implements PaymentMethod {
private final String cardHolder;
private final double limit;
public CardPayment(String cardHolder, double limit) {
this.cardHolder = cardHolder;
this.limit = limit;
}
@Override
public boolean pay(double amount) {
if (amount > limit) {
System.out.println("card declined, over the limit");
return false;
}
System.out.println("card paid " + amount);
return true;
}
}
public class PaymentService {
public boolean pay(PaymentMethod method, double amount) {
return method.pay(amount);
}
}
So why does the design pay off as the requirements change? Here is what happens when the requirements change.
the new requirement the change to the code
---------------------------- --------------------------------------------
add a wallet payment one new class, nothing else
add an invoice payment one new class, nothing else
add a discount rule one new class that wraps the old ones
add a logging wrapper one new class that wraps the old ones
And the second one is worth looking at closely, because it is the case that hurts people. Adding a discount rule does not need a new kind of payment, it needs a new way of using the existing ones. And the way to do that without touching the service is a decorator, which is a class that implements the same interface and holds another one.
public class DiscountedPayment implements PaymentMethod {
private final PaymentMethod inner;
private final double rate;
public DiscountedPayment(PaymentMethod inner, double rate) {
this.inner = inner;
this.rate = rate;
}
@Override
public boolean pay(double amount) {
return inner.pay(amount * (1 - rate));
}
}
And the service still takes a PaymentMethod and it still works, and the discount is applied by wrapping whatever method was passed in. Nothing in the service changed.
PaymentService service = new PaymentService();
PaymentMethod method = new DiscountedPayment(new CardPayment("Ada", 100.0), 0.10);
service.pay(method, 50.0); // the card is charged 45.0
And there is a third change that is worth knowing about, and it is the one that hurts. When the requirements change so that the service needs to know the kind of the payment, then the polymorphism has been pushed too far and you need to know the concrete type. The rule of thumb is that the service should never need to ask what kind of payment it has.
So the rule to remember is this. A new kind is one new class, and a new rule is one new class that wraps the old ones.
The last question in this topic is about keeping the design from becoming a tangle.