Lably
Skip to content

Open–closed principle

Concept in object-oriented programming

  • 2 min read
  • 2 chapters

Related on Lably

Through Object-oriented programming, Inheritance (object-oriented programming) and SOLID

The name open–closed principle has been used in two ways. Both ways use generalizations (for instance, inheritance or delegate functions) to resolve the apparent dilemma, but the goals, techniques, and results are different.

The open–closed principle is one of the five SOLID principles of object-oriented design.

Meyer's open–closed principle

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

  • 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):

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.

Sign in to keep reading

One more chapter is free with a Lably account.

More in Computing and communication