OCP

The OCP states that software entities should be open for extension but closed for modification.

The ambiguity of this statement can be resolved through two variations of the principle:

  • Existing classes should be modified only in order to correct bugs. This restriction delivers the closed aspect of the principle. The open aspect is delivered through implementation inheritance or, in other words, inheritance with the goal of reusing rather than subtyping.
  • Openness through polymorphism by definition also provides for closure, as extensibility is supported through substitution rather than modification. Unfortunately, substitution often leads to accidental complexity, which must be addressed by yet another principle--the LSP.

The primary utility of the OCP is the confinement of cascading changes while providing extensibility. This is achieved by designing for extensibility and prohibiting changes to existing entities. Extensibility is attained by fancy tricks with abstract classes and virtual functions. Closure is achieved by encapsulation, or rather by the hiding of moving parts.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset