S.O.L.I.D Object Oriented Design
You can see many of the articles which are related to SOLID principle. But you may have bit of confusion that actually what does it mean. So , I'm going to help you to understand this topic so I hope you can easily understand.
In university level we may not to aware of this principle, but when you are going to industry it should be major concept for writing code. Because, industry level projects are being developed by many developers, testers and designers concurrently. So our code should not affect each other codes. If there any new features added into existing system it won't affect to entire project.
So let me move to main topic.
SOLID
SOLID stands for- S : Single Responsibility Principle (SRP).
- O : Open for extends and Close for modification.
- L : Liskov's substitution.
- I : Interface Segregation principle.
- D : Dependency Inversion Principle.
Now, let check all one by one with simple example.
01) S : Single Responsibility Principle
This principle state that "Class should have one and only one responsibility to change"
In real world ,if we consider eraser , it's length define how long it will use. Eraser used to remove pencil mark on the paper. We can't do anything else by this.
Likewise, when we create a class ,it should have only one responsibility or one job or one purpose.We should avoid to write multiple responsibility into a one class. It tend to increase the length of the code and it will affect in future modifications.
02) O : Open for extension and closed to modifications.
In real world , If we consider leadership training camp.It contains certain things such as accommodation , food arrangement , exercises and etc. We can't change these things. (Closed to modification) But respective leaders can give permission to take rest while we are in sick. They open their rules for students' health conditions (Open for extension).
Likewise ,open closed applies at all levels.Don't modify an interface or class by removing methods or changing methods, instead add new methods or overloads of existing methods.
03) L : Liskov's substitution.
In real world, if we consider Pizza hut , they have small (base type) , medium (descendant) ,large size (descendant) of pizza. We can choose one of these types based on how much of people we are going to serve. Though the pizza size may different,but it's taste not different from each other. It's all inherits and extends from base type of pizza (Small).
Likewise, Liskov's substitution state that you should be able to substitute an object for any descendant object without your implementation knowing any different.
Descendant object is an object which is inherit from another objects.
04) I : Interface Segregation principle.
It's states that "Don't force any client to implement interface which is irrelevant to them.
In real world, We assume that you are going to buy a frock for your wedding. Imagine what will happen if the seller is showing you children frocks? it's irrelevant correct? He should show the wedding frocks which exactly fit on you.
Here our main goal is to focus on avoiding fat interface and give preference to many small client- specific interfaces. You should prefer many client interfaces rather than one general interface and each interfaces should have a specific responsibility.
Here our main goal is to focus on avoiding fat interface and give preference to many small client- specific interfaces. You should prefer many client interfaces rather than one general interface and each interfaces should have a specific responsibility.
05) D : Dependency Inversion Principle.
In real life. Let's take a our TV remote control. We can use any brand of battery for our remote controller. What we need is remote should work properly .It is not dependent on the battery brands.
likewise , dependency inversion make our code more reusable.Dependency inversion is an object not creating other objects which it relies upon. Instead , they're passed in by outside source.
Comments