Alex Rivera | Logout

What is the meaning and reasoning behind the Open/Closed Principle?

Asked 2008-09-12T13:48:42.403
68

The Open/Closed Principle states that software entities (classes, modules, etc.) should be open for extension, but closed for modification. What does this mean, and why is it an important principle of good object-oriented design?

Edit
Report

2 Answers

35

Specifically, it is about a "Holy Grail" of design in OOP of making an entity extensible enough (through its individual design or through its participation in the architecture) to support future unforseen changes without rewriting its code (and sometimes even without re-compiling **).

Some ways to do this include Polymorphism/Inheritance, Composition, Inversion of Control (a.k.a. DIP), Aspect-Oriented Programming, Patterns such as Strategy, Visitor, Template Method, and many other principles, patterns, and techniques of OOAD.

** See the 6 "package principles", REP, CCP, CRP, ADP, SDP, SAP

answered 2008-09-12T16:21:56.823
2

The principle means that it should easy to add new functionality without having to change existing, stable, and tested functionality, saving both time and money.

Often, polymorhism, for instance using interfaces, is a good tool for achieving this.

answered 2008-09-12T13:54:07.087

Your Answer