And following one principle does not ensure you follow the others correctly.Īs we will see, the LSP extends the Open-Closed principle, and following the rules of the OCP isn’t enough to ensure that your code is open for extension and closed for modifications. Keep in mind that the five SOLID principles are somehow related. Having said that, how can we guarantee that replacing a supertype with a subtype has no side effects on our existing code? ![]() The LSP brings additional constraint to the Object-Oriented Design and states that these relationships aren’t sufficient and should be replaced with IS-SUBSTITUTABLE-FOR.īut what does that mean? Simply, it means a supertype should be replaceable with its subtype without breaking the existing code. In other words, the supertype should behave the same as its subtype. For example, an address HAS A city related to it. On the other hand, Composition, the HAS-A relationship with something else. Inheritance, the IS-A relationship, occurs when something IS A kind of another thing. The basic object-oriented design controls the relationship between objects using either Inheritance or Composition. “If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T.” Barbara Liskov introduced the mathematical definition in 1988: ![]() ![]() Let’s introduce the mathematical definition of the LSP and then jump into the details.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |