Menu
Home Explore People Places Arts History Plants & Animals Science Life & Culture Technology
On this page
Open–closed principle
A computer-programming principle that states that software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification

In object-oriented programming, the open–closed principle (OCP) states that software entities like classes and functions should be open for extension but closed for modification, allowing behavior to be extended without changing the original source code. The term has two interpretations, both involving generalizations such as inheritance or delegation, though their goals and techniques differ. This principle is one of the five SOLID principles of object-oriented design, which promote maintainable and scalable software architectures.

We don't have any images related to Open–closed principle yet.
We don't have any YouTube videos related to Open–closed principle yet.
We don't have any PDF documents related to Open–closed principle yet.
We don't have any Books related to Open–closed principle yet.
We don't have any archived web articles related to Open–closed principle yet.

Meyer's open–closed principle

Bertrand Meyer is generally credited for having originated the term open–closed principle,2 which appeared in his 1988 book Object-Oriented Software Construction.3: 23 

  • A module will be said to be open if it is still available for extension. For example, it should be possible to add fields to the data structures it contains, or new elements to the set of functions it performs.
  • A module will be said to be closed if [it] is available for use by other modules. This assumes that the module has been given a well-defined, stable description (the interface in the sense of information hiding).

At the time Meyer was writing, adding fields or functions to a library inevitably required changes to any programs depending on that library. Meyer's proposed solution to this problem relied on the notion of object-oriented inheritance (specifically implementation inheritance):4: 229 

A class is closed, since it may be compiled, stored in a library, baselined, and used by client classes. But it is also open, since any new class may use it as parent, adding new features. When a descendant class is defined, there is no need to change the original or to disturb its clients.

Polymorphic open–closed principle

During the 1990s, the open–closed principle became popularly redefined to refer to the use of abstracted interfaces, where the implementations can be changed and multiple implementations could be created and polymorphically substituted for each other.

In contrast to Meyer's usage, this definition advocates inheritance from abstract base classes. Interface specifications can be reused through inheritance but implementation need not be. The existing interface is closed to modifications and new implementations must, at a minimum, implement that interface.

Robert C. Martin's 1996 article "The Open-Closed Principle"5 was one of the seminal writings to take this approach. In 2001, Craig Larman related the open–closed principle to the pattern by Alistair Cockburn called Protected Variations, and to the David Parnas discussion of information hiding.6

See also

References

  1. Meyer, Bertrand (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. 0-13-629049-3

  2. Robert C. Martin "The Open-Closed Principle", C++ Report, January 1996 Archived August 22, 2006, at the Wayback Machine /wiki/Robert_C._Martin

  3. Meyer, Bertrand (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. 0-13-629049-3

  4. Meyer, Bertrand (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. 0-13-629049-3

  5. Robert C. Martin "The Open-Closed Principle", C++ Report, January 1996 Archived August 22, 2006, at the Wayback Machine /wiki/Robert_C._Martin

  6. Larman, Craig (May–June 2001). "Protected Variation: The Importance of Being Closed" (PDF). IEEE Software. 18 (2). IEEE: 89–91. doi:10.1109/52.922731. http://codecourse.sourceforge.net/materials/The-Importance-of-Being-Closed.pdf