The open-closed principle definition by Bertrand Meyer
“Software
entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.”
Closed for modification: Once the entities are defined those should not be touched later on.
Once you implemented the base class you should not modify the functionality, if you change it, it will end up changing the behavior of the derived classes. It leads to code changes in all derived classes as well, you will mess up whole derived classes code every time you change the base class. Code maintenance will become difficult, and the code won't be flexible for the further changes.
Open Closed Principle helps in avoid touching the base class and allows the class to extends the functionality of it's by implementing the new class.
Real Life Example:
- An adapter is closed for modification it provides flexibility to use the ports.- So you can use the extension board and use it for different purpose.
- You can charge the mobile, laptop, power banks with the extension cables.
Lets take an example of salary processing in a company which sets the max salary based on the designation.
Single Responsibility Principle (SRP)
https://dotnetcookie.blogspot.com/2019/12/solid-design-principles-in-c-single.html
https://dotnetcookie.blogspot.com/2020/07/solid-design-principles-in-c-liskov.html
Interface Segregation Principle (ISP)
https://dotnetcookie.blogspot.com/2020/07/solid-design-principles-in-c-interface.html
https://dotnetcookie.blogspot.com/2020/07/solid-design-principles-in-c-dependency.html
No comments:
Post a Comment