Object Oriented Design Principle: The Open-Closed Principle (OCP)

open closed principle

When there’s a single change in the program that results to unexpected changes to the other dependent modules that program have obviously undesirable traits that we have come to associate with “bad design”. The program can easily break, hard to maintain and worse it can be unreusable. The OCP principle guides to us to avoid this.

The OCP principle states that,

Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.

To paraphrase, it says that you should design modules that never change. Whenever there’s a change in requirement, you should extend the existing module by adding new code, not by modifying existing code that have already been tested.

Conformance to this principle comes in two aspect.

  1. Modules should be “Open for Extension”

This means that the module can be extended. Let say you have already perfect, tested and working code, you don’t want anybody messing your code. You can let them extend through abstraction. For example, you have a class designed for inheritance, of course it’s designed to be extended. You can override the methods or add methods to the the class declared in the superclass to add or extend functionality to your more specific classes . In that case, you’re extending the module without messing the code that you know is working.

Inheritance is the easiest way to achieve OCP principle. But there are other better ways to achieve this like composition, delegation and many more.

2. Modules should be “Closed for modification”

This means that code that are already working should not be modified unless you have to. You can add functionality to the existing system by extending them. You make sure that you design modules that can easily change in the way that you’re not modifying perfect code that can cause to break other program’s functionality.

By avoiding changes to the existing code, you can also avoid new bugs and errors that will come up.

Conclusion

Like with other design principles, if you followed those principles correctly, you’re making your code easy to extend and maintain.

For code example to this principle, you may refer to this page.

If I miss something on this blog post, just leave a comment below. And I’ll add it to this blog post.

Don’t forget to subscribe to this blog to get notified for the new posts!

Advertisements

3 thoughts on “Object Oriented Design Principle: The Open-Closed Principle (OCP)

  1. As much as i agree with you about OCP I often find that it’s usually makes sense to change existing code than to build a fix on top of it. If you blindly follow OCP everywhere, you’ll probably end up in a bigger mess than you’ve been in before.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s