From the course: Advanced Design Patterns: Design Principles

Unlock this course with a free trial

Join today to access over 22,500 courses taught by industry experts.

Open-closed principle

Open-closed principle

- [Instructor] The Open/Closed Principle says that our designs should be open for extension but closed for modification. So how do we think about that? Well imagine a class saying this to you, "Come on in, we're open. "Feel free to extend our classes with "any new behavior you like. "If your needs or requirements change, "just go ahead and make your own extensions." And imagine the same class saying this to you, "Sorry, we're closed. "We spent a lot of time getting this code correct "and bug-free so we can't let you alter the existing code. "It must remain closed for modification." Well that sounds a bit contradictory, doesn't it? After all, the less modifiable something is, the harder it is to extend, right? So how can we extend the behavior of a system without having to modify that system? As it turns out, there are some clever, object-oriented techniques for allowing systems to be extended even if we can't change the…

Contents