![]() (This example is based on Tom Dalling’s blog post about LSP and Mark Seemann’s Pluralsight course about SOLID principles.) Easier Definition LSP is often violated by attempts to remove features. ![]() But then we realize that something that we inherited doesn’t fit anymore and we throw NotSupportedException. We inherit it (like Penguin above) and code something more. This was a really simple example but think if we have a good class and we want to extend its behavior. This is a quite typical violation of LSP. Throw new NotSupportedException("Penguins can't fly") Example: Violating LSPĬonsider we have an IBird interface with a Fly method:īut when we try to code a Penguin class, we can’t implement the Fly method: ![]() I will start by violating LSP because that is easier than to start by explaining what it is. Martin)īut this doesn’t either make any sense of what LSP really is. Martin has modified it to a more user-friendly version:ĭerived classes must be substitutable for their base classes. That is something I can’t figure out, especially that it has something to do with object-oriented programming. Then φ(y) should be true for objects y of type S where S is a subtype of T. Subtype Requirement: Let φ(x) be a property provable about objects x of type T. Especially if we look at the original definition which is practically mathematics: Liskov substitution principle (LSP) has the most difficult definition of five SOLID principles. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |