![]() Hanselminutes Podcast #145 on SOLID principals with Scott Hanselman and Robert C Martin.Īn alternate view of the LSP (suggesting that context and usage are more important) by Alastair Cockburn. The Liskov Substitution Principle a paper by Robert C Martin. It doesn't matter if someMethod on ClassOne operates differently from someMethod on ClassTwo if that method is never invoked during a particular operation, but it may make sense for those methods to exist for use by other operations with different constraints elsewhere in the application.ĭesign Principles and Design Patterns by Robert C Martin. In the context of a specific application, enforcing LSP may be overkill. The circle really only needs a setRadius operation while the ellipse would need both a setRadius and a setAspectRatio operation. If you look at the behavior of a circle and an ellipse, it becomes clear why. This is a violation of the LSP because that constraint means that a circle does not behave the same way as an ellipse (an operation which attempted to set both height and width would experience a potentially undesirable difference in behavior). At first glance, a circle could be considered a subclass of ellipse (because it implements the same interface), with the added requirement that height and width must match. (The base class has a method that always returns a value in the range 1 - 100 but the child class needs to return a value of 101.)Ī subclass needs to provide an exception condition that was not present in the base class (unless the exception object is itself a subclass of a valid exception object raised by the base class).Ī common example showing a violation of the LSP is the classic "Is a circle an ellipse" question. The Liskov substitution principle, written by Barbara Liskov in 1988, states that functions that reference base classes must be able to use objects of derived (. (The base class has a method that accepts any SomeBaseClass as a parameter, but the child class requires a ClassInheritedFromSomeBaseClass.)Ī subclass needs to weaken postconditions. The LSP may not be completely appropriate if:Ī subclass needs to strengthen preconditions. ![]() #LISKOV SUBSTITUTION PRINCIPLE CODE#It is expected, or desired, that the code using on the base class will be reused elsewhere and may encounter alternate derived classes. (An example would be the Strategy pattern where the caller should not require any additional information about the specifics of the strategy.) There is no reason for the underlying processing to know about any more than the base class. Subclasses with more constraints than base classes Design By Contract simply specifies the part of the behavior which must remain unchanged in a given scenario.Įrrors could be raised by a subclass that are not expected/raised by the base class ![]() ![]() Note also that Design By Contract does not imply (or require) use of the LSP. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |