Role of Design Principles Including Single Responsibility Principle and Open Closed Principles
Role of Design Principles Including Single Responsibility Principle and Open Closed Principles
We are going to club methods AddInvoice() and DeleteInvoice() into single class
Invoice as they do a similar kind of functionality. Whereas we will be creating
separate classes Report and Email for methods GenerateReport() and
EmailReport() respectively as they are completely independent and have different
functionality.
role of design principles including open closed principle
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.
Well, in the land of software development, it’s possible for an item to be opened
for extension and be closed for modification. It means you or your team members
should be able to add new functionalities to an existing software system without
changing the existing code.
If you want to add new functionality to your existing code and you have to modify
it before you add the new functionality, then you are not following the open-closed
principle.
Why Should You Use the Open-Closed Principle?
Here are some reasons why you should be using the open-closed principle:
You don’t need to re-invent the wheel: as the principle states, the code you and
your team are working on is closed for extension. This means if you’re following
the open-closed principle, you don’t need to reinvent the wheel (and rebuild
everything) when you want to add new features.
You focus on what is necessary: as the OCP states, your code is closed for
modification. It means you can add new features without performing too much
editing on the existing code, or none at all. This can help you and your team
members focus on what is necessary when it’s time to implement new
functionalities.
You can avoid bugs: since you don’t have to edit the existing code before adding
new features, you can avoid introducting unnecessary bugs.
-Your code is more maintainable, testable, and flexible: following the OCP will
make your codebase loosely coupled. With this the code is more flexible and
maintainable. And if you want, you can unit test each class successfully.