Open–closed Principle
   HOME

TheInfoList



OR:

In
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
, the open–closed principle (OCP) states "''software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification''"; that is, such an entity can allow its behaviour to be extended without modifying its
source code In computing, source code, or simply code, is any collection of code, with or without comments, written using a human-readable programming language, usually as plain text. The source code of a program is specially designed to facilitate the w ...
. The name ''open–closed principle'' has been used in two ways. Both ways use generalizations (for instance,
inheritance Inheritance is the practice of receiving private property, titles, debts, entitlements, privileges, rights, and obligations upon the death of an individual. The rules of inheritance differ among societies and have changed over time. Officia ...
or delegate functions) to resolve the apparent dilemma, but the goals, techniques, and results are different. Open–closed principle is one of the five
SOLID Solid is one of the four fundamental states of matter (the others being liquid, gas, and plasma). The molecules in a solid are closely packed together and contain the least amount of kinetic energy. A solid is characterized by structural ...
principles of object-oriented design.


Meyer's open–closed principle

Bertrand Meyer Bertrand Meyer (; ; born 21 November 1950) is a French academic, author, and consultant in the field of computer languages. He created the Eiffel programming language and the idea of design by contract. Education and academic career Meyer rece ...
is generally credited for having originated the term ''open–closed principle'',
Robert C. Martin Robert Cecil Martin (born 5 December 1952), colloquially called "Uncle Bob", is an American software engineer, instructor, and best-selling author. He is most recognized for developing many software design principles and for being a founder of t ...
br>C++ Report, January 1996">"The Open-Closed Principle", C++ Report, January 1996
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 tis 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 dilemma relied on the notion of object-oriented
inheritance Inheritance is the practice of receiving private property, titles, debts, entitlements, privileges, rights, and obligations upon the death of an individual. The rules of inheritance differ among societies and have changed over time. Officia ...
(specifically
implementation inheritance In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object ( prototype-based inheritance) or class ( class-based inheritance), retaining similar implementation. Also defined as deriving new classe ...
):
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 Robert Cecil Martin (born 5 December 1952), colloquially called "Uncle Bob", is an American software engineer, instructor, and best-selling author. He is most recognized for developing many software design principles and for being a founder of t ...
's 1996 article "The Open-Closed Principle" was one of the seminal writings to take this approach. In 2001
Craig Larman Craig Larman (born 1958) is a Canadian computer scientist, author, and organizational development consultant. With Bas Vodde, he is best known for formulating LeSS (Large-Scale Scrum), and for several books on product and software development. E ...
related the open–closed principle to the pattern by
Alistair Cockburn Alistair Cockburn ( ) is an American computer scientist, known as one of the initiators of the agile movement in software development. He cosigned (with 17 others) the Manifesto for Agile Software Development. Life and career Cockburn starte ...
called ''Protected Variations'', and to the
David Parnas David Lorge Parnas (born February 10, 1941) is a Canadian early pioneer of software engineering, who developed the concept of information hiding in modular programming, which is an important element of object-oriented programming today. He is al ...
discussion of ''
information hiding In computer science, information hiding is the principle of segregation of the ''design decisions'' in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decisio ...
''.


See also

*
SOLID Solid is one of the four fundamental states of matter (the others being liquid, gas, and plasma). The molecules in a solid are closely packed together and contain the least amount of kinetic energy. A solid is characterized by structural ...
– the "O" in "SOLID" represents the open–closed principle * Robustness principle


References


External links


The Principles of OOD

The Open/Closed Principle: Concerns about Change in Software Design

The Open-Closed Principle -- and What Hides Behind It
{{DEFAULTSORT:Open closed principle Object-oriented programming Type theory Software design Programming principles Software development philosophies