The Open-Closed Principle

The Open-Closed Principle (OCP) states:

A component should be open to extension but closed to modification.

A component can be anything from a single class to an entire program.

Example: Inheritance

Instead of modifying a class that does not quite fit our needs, we can declare a new class that inherits the features of the original class and adds new features. The new class can even override (redefine) some of the inherited features:

Example: Polymorphism

Polymorphism means that a client object does not know the specific type of a provider object only its super type. This means that new subtypes of providers can be added to a system without modifying the client.

Example: Frameworks

Inheritance allows us to extend the functionality of a single class. Polymorphism allows us to extend the functionality of a component that may consist of several classes. Frameworks allow us to extend the functionality of an entire system.

A framework is the application-independent part of a program. A framework provides a generic user interface, error handling, persistence mechanism, and architecture.

A framework customization is the application-dependent part of a program. It provides details about the business logic and data:

Programmers can customize a framework without modifying it by extending strategic framework classes and implementing strategic framework interfaces:

A Vertical Framework (Prospector):

A Horizontal Framework (Microsoft Foundation Classes)