User Tools

Site Tools


Open-Closed Principle

Variants and Alternative Names


Principle Statement

Modules should be open for enhancement but closed for modification.





Beware that wrong application of OCP may lead to the anti-pattern onion.

See also section contrary principles.


Bertrand Meyer: Object-Oriented Software Construction, p. 57pp.


Relations to Other Principles


  • Encapsulate the Concept that Varies (ECV): The OCP demands encapsulating abstract concepts in base classes (or interfaces) in order to be able to enhance the module by subclassing which is possible without changing the previously written code. In this case several variations of a concept may exist in the code at the same time. There is always the abstract base class plus one or usually more concrete subclasses. So the OCP is about encapsulating abstract concepts that vary “in space”.


Contrary Principles

  • more Is More Complex (MIMC): The OCP demands introducing abstract base classes or interfaces.
  • Keep It Simple Stupid (KISS): The OCP demands introducing abstract base classes or interfaces. This increases complexity.
  • Model Principle (MP): OCP sometimes results in the introduction of artificial classes that do not correspond to a real-world concept.

Complementary Principles

  • Dependency Inversion Principle (DIP): OCP results in the introduction of abstract classes or interfaces and descendant concrete classes. DIP now tells that other classes should only depend on the abstractions.
  • Liskov Substitution Principle (LSP): OCP may results in the introduction of abstract classes or interfaces. Here it is important to get the abstraction right. Otherwise LSP may be violated.

Principle Collections

Robert C. Martin created the SOLID principle collection where “SOLID” is an acronym for the following principles:

This is the subset of Martin's principles that deals with the design of classes. For the full list of principles he collected see Robert C. Martin's Principle Collection.


Further Reading


Description Status

Further Reading

principles/open-closed_principle.txt · Last modified: 2021-03-26 08:56 by