
Liskov Substitution Is About Behavior, Not Inheritance That Compiles
- كاتب موثّق
محتويات المقال
VoucherGateway inherits from PaymentGateway, overrides RefundAsync, compiles, and passes its own tests. It also throws for any amount other than the full captured amount. RefundService was written against PaymentGateway, whose contract allows partial refunds, so it never expected that. The first fix proposed in review is if (gateway is VoucherGateway).
The common misconception
What actually happens
Substitutability is about what callers can tell
Signatures can't tell a stack from a queue
put that adds an element and a get that removes one. By the signature rules, either could be a legal subtype of the other. But "a program written in the expectation that x is a stack is unlikely to work correctly if x actually denotes a queue, and vice versa." Same shape, different behavior, and the compiler can't see the difference.
The behavioral rules
: the subtype's method must work in every state the supertype's method accepts. It may accept more, never less. VoucherGatewaybreaks this by refusing partial amounts.: the subtype may guarantee more than the supertype, never less. Exceptions: a subtype's method may not signal exceptions the supertype's doesn't, "since a caller of a method on a supertype object should not expect to handle an unknown exception." Invariants and history: what's always true of the supertype stays true. The paper shows that a set you can delete from is not a subtype of a set that only grows, even with matching signatures.
It's not about inheritance at all
The fix: promise only what every implementation keeps
public class PaymentGateway{ // Contract: refunds any amount up to what was captured public virtual Task RefundAsync(Payment p, decimal amount) => Task.CompletedTask; // stand-in for the provider's refund call}public sealed class VoucherGateway : PaymentGateway{ // Compiles, but demands more than the base type promised public override Task RefundAsync(Payment p, decimal amount) => amount == p.Captured ? base.RefundAsync(p, amount) : throw new InvalidOperationException("Full refunds only");}// Refunding one item of three now fails only for voucher orders,// and RefundService starts growing: if (gateway is VoucherGateway)// Each contract promises only what all of its implementations keeppublic interface IRefunds{ Task RefundInFullAsync(Payment p);}public interface IPartialRefunds : IRefunds{ Task RefundAsync(Payment p, decimal amount); // any amount <= Captured}public sealed class VoucherGateway : IRefunds{ public Task RefundInFullAsync(Payment p) => Task.CompletedTask;}// Code that refunds single items asks for IPartialRefunds,// so passing a VoucherGateway is now a compile errorIPartialRefunds; the voucher gateway implements only IRefunds, and its contract never promised partial refunds. RefundService takes IPartialRefunds for item returns and needs no type checks. The bug moved from production to the compiler, which is the best place to catch a broken promise.
How to apply it in your project
Write the contract down In the base type's doc comments, state which inputs are valid, what each method guarantees, and which exceptions callers must handle. Search for the smells Look for overrides that throw NotSupportedExceptionor do nothing, and for callers that check concrete types withis,asor type switches.Share contract tests Run the same test suite against every implementation of an interface or base class, including fakes used in other tests. Split promises that not everyone keeps When an implementation can't honor part of a contract, give that part its own interface, or use composition instead of inheritance.
Key takeaways
LSP is about behavior: code that uses a type must not be surprised by any of its subtypes. Compatible signatures are necessary but not enough; a stack and a queue can share them. A subtype may accept more and promise more, never demand more, deliver less or throw new exceptions. It applies to interfaces and duck types, not only to class inheritance. Type checks against subtypes and overrides that refuse to work are signs the contract is wrong; split it.
التعليقات (0)