![]() Each one of them has a lawyer who represents them. For example, imagine 2 people who are getting divorced. Representatives do not share the relationships of the things they represent. It’s a piece of code! It’s a representation of a Rectangle. One idea might be to make direct instances of Square impossible by changing Square to a module.Īnother option might be to reconsider the notion of Square inheriting from Rectangle in the first place! To use Bob Martin’s explanation from his codecast: What can done to avoid this violation, which is also called a “Refused Bequest” by the SOLID gurus? height ) #fails for the Square! LSP Violation! "Refused Bequest" end end Shapes = def setRectangleWidthHeight ( shapes ) shapes. Rectangle and Square both have width and height attributes, but when setting the width of the Rectangle and the width of the Square, different behaviors are applied. Idiomatically, Rectangle is a superclass of Square. To illustrate the principle, think about 2 classes, Rectangle and Square. From then onwards, it should be treated exactly like the superclass with no added methods or special treatment. This means that LSP winds up applying more to which messages an object responds to, and less its type. Liskov Substitution Principle means that subclasses should be done with the understanding that you should only really care what subclass an instance is when it gets instantiated. In dynamic languages like Ruby, the Liskov principle (LSP) works slightly differently because Ruby less rigidly enforces how types work (so-called “Duck Typing”) as opposed to a language like Java, where type safety is enforced by the compiler. ![]() “if S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program.”Īll this really means is that objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program. The Liskov Substitution Principle (the “L” in SOLID design principles), is a simple, yet powerful concept that can be used to improve your design.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |