fbpx
Wikipedia

Open–closed principle

In object-oriented programming, the open–closed principle (OCP) states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification";[1] that is, such an entity can allow its behaviour to be extended without modifying its source code.

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.

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

Meyer's open–closed principle Edit

Bertrand Meyer is generally credited for having originated the term open–closed principle,[2] which appeared in his 1988 book Object Oriented Software Construction.[1]: 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.[citation needed] Meyer's proposed solution to this dilemma relied on the notion of object-oriented inheritance (specifically implementation inheritance):[1]: 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 Edit

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"[2] 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.[3]

See also Edit

References Edit

  1. ^ a b c Meyer, Bertrand (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3.
  2. ^ a b Robert C. Martin "The Open-Closed Principle", C++ Report, January 1996 August 22, 2006, at the Wayback Machine
  3. ^ Larman, Craig (May–June 2001). "Protected Variation: The Importance of Being Closed" (PDF). IEEE Software. IEEE. 18 (2): 89–91. doi:10.1109/52.922731.

External links Edit

  • The Principles of OOD
  • The Open/Closed Principle: Concerns about Change in Software Design
  • The Open-Closed Principle -- and What Hides Behind It

open, closed, principle, object, oriented, programming, open, closed, principle, states, software, entities, classes, modules, functions, should, open, extension, closed, modification, that, such, entity, allow, behaviour, extended, without, modifying, source,. In object oriented programming the open closed principle OCP states software entities classes modules functions etc should be open for extension but closed for modification 1 that is such an entity can allow its behaviour to be extended without modifying its source code 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 Open closed principle is one of the five SOLID principles of object oriented design Contents 1 Meyer s open closed principle 2 Polymorphic open closed principle 3 See also 4 References 5 External linksMeyer s open closed principle EditBertrand Meyer is generally credited for having originated the term open closed principle 2 which appeared in his 1988 book Object Oriented Software Construction 1 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 citation needed Meyer s proposed solution to this dilemma relied on the notion of object oriented inheritance specifically implementation inheritance 1 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 EditDuring 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 2 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 3 See also EditSOLID the O in SOLID represents the open closed principle Robustness principleReferences Edit a b c Meyer Bertrand 1988 Object Oriented Software Construction Prentice Hall ISBN 0 13 629049 3 a b Robert C Martin The Open Closed Principle C Report January 1996 Archived August 22 2006 at the Wayback Machine Larman Craig May June 2001 Protected Variation The Importance of Being Closed PDF IEEE Software IEEE 18 2 89 91 doi 10 1109 52 922731 External links EditThe Principles of OOD The Open Closed Principle Concerns about Change in Software Design The Open Closed Principle and What Hides Behind It Retrieved from https en wikipedia org w index php title Open closed principle amp oldid 1175980259, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.