课程: Programming Foundations: Design Patterns

今天就学习课程吧!

今天就开通帐号,24,100 门业界名师课程任您挑!

Understanding the open-closed principle

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…

内容