Open-Closed Principle

Definition
A class shouldn't use selection as a means to be open to extension.

The principle basically tells us to use polymorphism as it was intended.

Selection is a term from structured programming that involves if/then statements, switch/case statements and such.

Open Closed Principle

How does it work?

The principle is usually described as: a software artifact should be open for extension but closed for modification. Being open for extension means that the class can be used as part of new functionality. Being closed for modification is usually interpreted as an imperative to change a class as little as possible, but this seems unnecessarily strict. A valid point however is this: if the class needs to take a new form (in the sense of polymorphism), this form shouldn't be implemented as a branch of selection (an if-statement, switch/case statement, or something). It should be implemented as a separate class. This new class can use the original class or it can inherit from it. This way the original class doesn't need to be changed whenever a new form is added, and it doesn't take on ever more different responsibilities.

When should you use it?

It helps to build a maintainable system.

Links