These are two important principles for a good object-oriented design (OOD).
The Open-Closed principle states that well-designed code should be open for extension and closed for modification. In other words, in a well-designed program, new functionalities are added by adding new code rather than by modifying already working code. It is the foundation for building software that is maintainable and reusable. This principle was coined by Bertrand Meyer in his book titled Object Oriented Software Construction. The second edition of this book is introduced here in Eiffel Software.
An article obtained from Object Mentor give a very good introduction on the Open-Closed principle. Here is the direct link to this article: The Open-Closed Principle (PDF file, 69 KB). The following is an excerpt of this article that introduces the principle.
Modules that conform to the open-closed principle have two primary attributes.
- They are “Open For Extension”. This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.
- They are “Closed for Modification”. The source code of such a module is inviolate. No one is allowed to make source code changes to it.
It would seem that these two attributes are at odds with each other. The normal way to extend the behavior of a module is to make changes to that module. A module that cannot be changed is normally thought to have a fixed behavior. How can these two opposing attributes be resolved?
The Liskov Substitution Principle (LSP) is a particular definition of subtype that was first introduced by Barbara Liskov in a 1988 paper entitled “Data Abstraction and Hierarchy”. This principle was then elaborated by Barbara Liskov and Jeannette Wing in a 1993 paper entitled “Family Values: A Behavioral Notion of Subtyping”. Essentially, the Liskov Substitution Principle states that
Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.
The above is a paraphrase of the LSP by another article obtained from Object Mentor. It gives a good explanation with clear examples on the LSF. Here is the direct link to this article: The Liskov Substitution Principle (PDF file, 30 KB).
These two principles are related by noting that the LSP is an important feature of all programs that conform to the Open-Closed principle.
A related principle to LSP is Design by Contract principle. It states that a sub-type can only have weaker pre-conditions and stronger post-conditions than its base class. Read this entry in Wikipedia to learn more about Design by Contract.
This entry was posted on Monday, January 16th, 2006 at 12:23 AM and filed in Software Engineering. Bookmark this entry. Follow the comments here with the RSS 2.0 feed. Comments are closed, but you can leave a trackback.

