Updated: Aug 29, 2021
This blog is about Object oriented design(OOD), its principles and benefits of using object oriented design(OOD) principles.
What is Object Oriented Design(OOD)?
Object Oriented Design teaches us some basic design principles to write high quality Object Oriented programs. The principles are learned from the common mistakes done by developers. The principles are the guidelines for the developers to avoid the same common mistakes.
Object Oriented Design(OOD) Patterns vs Principles
People might have heard about Design patterns and think we are going to talk about design patterns here. Design patterns provide few well known best practices to be used for some well known circumstances/scenarios and tells about how different classes can be written and used together in each scenario.
Object Oriented Design is more about basic principles, building blocks which programmers should keep in mind when they write programs. The Object Oriented Design principles can be applied in each design pattern, and in turn design patterns obeys the OO Design principles. That means they both are complementary and work together to handle different scenarios.
Why use Object Oriented Design Principles?
Any developers who write programs using any object oriented programming language should keep in mind the design principles and examine the code after writing whether the code is aligned with the design principles. The reasons why we should do this is because the code base becomes:
Easy to maintain(maintainability)
Easy to extend(extendibility)
Easy to understand(understandability)
Easy to test(testability)
Few words on each of the 4 points above, before we start discussion on Object Oriented Design principles. I would like to talk about each design principle with some code examples.
Easy to maintain
Whenever a small code change is needed in the existing code base, it will be easy to do the change if the code is written using OO design principles.
Easy to extend
When new functionalities are added to an existing code base the code base can become unnecessary complex if the design of the classes and methods are not done in a right way from the beginning. a good design forces programmers to follow some rules and avoid this situation. As an example if more classes need to be added that work similar things but not exactly the same thing, a good parent child relationship among the classes are needed to be used to avoid code duplication and perform better type checking.
Easy to understand
When new features and functionalities are added the code base becomes complex over time and that may lead to too many problems if programmers do not follow OO design. It becomes difficult to understand what a part of code is doing. programmers tend to make mistakes and that leads to bugs.
Easy to test
Unit tests are very important to test each method of a class or each class. These tests make sure that each method and class behaves as expected. to maintain a high quality bug free code, unit tests are used to achieve a good code coverage and branch coverage. If you follow Test Driven Development (TDD) then it helps detect bugs early and prevents later rework. In test driven programming a programmer first writes tests which tests most possible(if not all) conditions that can arise and how a part of code will behave in those cases. A high code coverage and branch coverage thus ensures high quality of code in terms of that all possible behaviour of the code is tested and there will be very few issues in the future. It does not provide any idea on code quality on code styling and code optimization.
Now to understand OO design you should know at least one OO programming. Here I have chosen to use JAVA. There are several principles and several names of the same principles. I will explain each of the Object Oriented Design Principles in a separate blog. Keep an eye in this space for them.