This is the ‘O’ in SOLID
Simply put:
- Open to extension is to allow new behavior to be added in the future
- Closed to modification is not to change the source/binary code
Definition
“*The Open / Closed Principle states that software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.*” – Wikipedia
- Dr. Bertrand Meyer originated the term in his 1988 book, Object-Oriented Software Construction
How do we open things for extension?
The key is to rely on abstractions, so this is interfaces or an abstract base class. Its however very important not to simply apply OCP to every class unless you know based that the business rules are likely to change.
Its best to focus on “Refactoring to a better design”, this means you should write your code with the least amount of complexity first and when needed refactor it and apply a principal such as OCP. So if it changes once, accept it. If it changes again consider refactoring.
PROS
- Existing core classes are not changed so you are less likely to introduce regression bugs
- You are adding new classes each time, so you can test and have faith you will not upset things
CONS
- This add complexity and must be used with caution, dont use OCP to try impress your boss
Typical Approaches
- Parameters
- By passing parameters to your methods you allow their behavior to change.
- Inheritance
- By using the
override
keyword in a concrete class you can over-ride the behavior ofvirtual
orabstract
methods.
- By using the
- Composition / Dependency Injection
- By injecting an instance of a service defined by its
interface
the consumer is open to extension but closed for modification. - An Abstract Factory can ne used if there are different rules the consumer must follow based on its state. An example is shopping cart price rules.
- By injecting an instance of a service defined by its
Sample Code
Shopping cart examples based on work from Steve Smith
Great examples by Steve Smith aka Ardalis
- https://github.com/carlpaton/SolidSample
- https://www.pluralsight.com/courses/csharp-solid-principles
Other Examples from Brad Vincent
References
- https://hackernoon.com/why-the-open-closed-principle-is-the-one-you-need-to-know-but-dont-176f7e4416d
- https://blog.cleancoder.com/uncle-bob/2014/05/12/TheOpenClosedPrinciple.html
- https://codeblog.jonskeet.uk/2013/03/15/the-open-closed-principle-in-review/
- http://joelabrahamsson.com/a-simple-example-of-the-openclosed-principle/
- http://www.debugosaurus.com/solid/2017/03/18/Open-closed.html
- https://code-maze.com/open-closed-principle/