Liskov Substitution Principle isn’t Complex. Just Give it a Try

<p>As we all know, software requirements always change, and we, as developers, need to make sure these changes don&rsquo;t break the existing code. For this reason, the SOLID principles were introduced in Object-Oriented design to ease this process.</p> <p>The SOLID principles are a set of principles created by&nbsp;<a href="https://en.wikipedia.org/wiki/Robert_C._Martin" rel="noopener ugc nofollow" target="_blank">Robert C. Martin</a>&nbsp;(Uncle Bob). These principles help us create more flexible, maintainable, and understandable software. These principles are:</p> <ul> <li><a href="https://mayallo.com/do-you-really-know-what-is-single-responsibility/" rel="noopener ugc nofollow" target="_blank">Single Responsibility Principle</a></li> <li><a href="https://mayallo.com/open-closed-principle-the-hard-parts/" rel="noopener ugc nofollow" target="_blank">Open-Closed Principle</a></li> <li>Liskov Substitution Principle</li> <li>Interface Segregation Principle</li> <li>Dependency Inversion</li> </ul> <p>After introducing the&nbsp;<a href="https://mayallo.com/open-closed-principle-the-hard-parts/" rel="noopener ugc nofollow" target="_blank">Open-Closed Principle</a>&nbsp;in the previous article, we will discuss the third principle, the Liskov Substitution Principle (LSP), which is the &ldquo;L&rdquo; in the SOLID acronym.</p> <h1>Definition</h1> <p>Let&rsquo;s introduce the mathematical definition of the LSP and then jump into the details. Barbara Liskov introduced the mathematical definition in 1988:</p> <blockquote> <p>&ldquo;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.&rdquo;</p> </blockquote> <p>The basic object-oriented design controls the relationship between objects using either&nbsp;<a href="https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)" rel="noopener ugc nofollow" target="_blank">Inheritance</a>&nbsp;or&nbsp;<a href="https://en.wikipedia.org/wiki/Composition_over_inheritance" rel="noopener ugc nofollow" target="_blank">Composition</a>. Inheritance, the IS-A relationship, occurs when something IS A kind of another thing. For example, a horse IS AN animal.</p> <p><a href="https://betterprogramming.pub/liskov-substitution-principle-isnt-complex-just-give-it-a-try-d4f84093ca5f">Visit Now</a></p>
Tags: Complex Liskov