9LSP: THE LISKOV SUBSTITUTION PRINCIPLE

Image

In 1988, Barbara Liskov wrote the following as a way of defining subtypes.

What is wanted here is something like the following substitution property: 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.1

To understand this idea, which is known as the Liskov Substitution Principle (LSP), let’s look at some examples.

GUIDING THE USE OF INHERITANCE

Imagine that we have a class named License, as shown in Figure 9.1. This class has a method named calcFee(), which ...

Get Clean Architecture: A Craftsman's Guide to Software Structure and Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.