From the course: Learning SOLID Programming Principles

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

OCP via inheritance and composition

OCP via inheritance and composition - Python Tutorial

From the course: Learning SOLID Programming Principles

OCP via inheritance and composition

- [Instructor] Let's say there's some suspicion that the correlation class has an algorithm that's too slow. Rather than modify the class, it makes more sense to extend the class hierarchy with another peer class that uses a different algorithm for correlation or linear regression or both. And this follows the Liskov Substitution Principle as well as the Open/Closed Principle. What techniques are available to us? Python offers a number of ways of extending a class. In this section I'll talk about two of the common techniques. Inheritance is the primary mechanism for creating a subclass to add or replace features of a superclass. Composition combines objects. There are a number of design patterns for this. For example, if a class uses the strategy pattern, then changes can be made via extension to the available strategy plugin functions or classes. We'll look at other ways to extend a class in the next section. And yes…

Contents