Which of the following best describes the Liskov Substitution Principle in system design?
Think about substituting objects in a program without breaking it.
LSP states that objects of a superclass should be replaceable with objects of a subclass without affecting the program's correctness.
Consider a class hierarchy where a Rectangle class has a method setWidth and setHeight. A subclass Square overrides these methods to keep width and height equal. Which problem does this violate according to LSP?
Think about whether the subclass changes the expected behavior of the base class methods.
The Square class changes the behavior of setting width and height independently, breaking the expectation from Rectangle, thus violating LSP.
When designing a large-scale system with multiple service components, how does adhering to LSP help in scaling and maintaining the system?
Consider how substitutability affects system upgrades and extensions.
Adhering to LSP allows components to be replaced or extended without breaking existing contracts, which supports easier scaling and maintenance.
What is a common tradeoff when strictly enforcing LSP in a complex system design?
Think about balancing design purity and practical optimizations.
Strict LSP enforcement can restrict subclasses from changing behavior to optimize for special cases, potentially limiting performance improvements.
Given a payment processing system where a PaymentProcessor interface defines a method processPayment(amount). A subclass CryptoPaymentProcessor overrides this method but throws an exception if the amount is below a minimum threshold, unlike the base implementation which accepts all amounts. What is the impact of this on LSP?
Consider whether the subclass can be used anywhere the base class is expected without causing errors.
The subclass throws exceptions for inputs that the base class accepts, breaking substitutability and violating LSP.