From the course: Programming Foundations: Design Patterns

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Understanding the open-closed principle

Understanding the open-closed principle

From the course: Programming Foundations: Design Patterns

Start my 1-month free trial

Understanding the open-closed principle

- [Instructor] So where do we go from here? Let's begin by getting some insight from our next design principle, the open-closed principle. This principle says that classes should be open for extension but closed for modification. What does that mean? Well, think about our current design for the coffee shop. We know in the future we'll need to support new beverage types and new condiments but that means we'll have to modify existing code, and that's exactly what we don't want. We want to leave our design open for new beverage types and condiments but closed in the sense that we don't want to touch existing code. Let's look at this principle in a little more detail. Again, the open-closed principle says that code should be open for extension but closed for modification. If we can do that, we have flexibility and maintainability because we can add new behavior but without the risk of introducing new bugs into the code we've already written. So our goal is to have designs that we can…

Contents