The Single Responsibility Principle
Latest posts by Prasad Kharkar (see all)
- PyCharm for Machine Learning - July 17, 2018
- Linear Discriminant Analysis using Python - April 30, 2018
- Principal Component Analysis using Python - April 30, 2018
Continuing our study about object oriented design principles, we will move on to the single responsibility principle and discuss in detail in this article. We will see how we can identify whether a class is having more than one responsibility and apply the single responsibility principle to split in multiple classes
Single Responsibility Principle:
It states that every object in your system should have a single responsibility and objects services should be focused on carrying out single task well.
Consider we have a Bike class and all the methods associated with bike class as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.thejavageek.dp; public class Bike { public void start() { } public void stop() { } public void shiftGear() { } public void applyBreaks() { } public void wash() { } public void ride() { } } |
At first sight, all the methods seem placed properly in the Bike class. After all it’s all related to bike right? Not so right, let us try to apply single responsibility principle to above class. I’m going to explain the same trick that I learned from Head First Object Oriented Design and Analysis book.
The <className> <methodName> itself.
let us apply it to bike class and check whether the method should really belong to Bike class. If Yes, then keep it there , if No, need to move to another class.
- The Bike starts itself : Yes
- The Bike stops itself: Yes
- The bike applies breaks itself : No
- The bike shifts gear itself : No
- The bike washes itself: No
- The bike rides itself: No
Now we’ve come to know that applyBreaks(), shiftGear(), wash() and ride() are not really the responsibilities of Bike itself. applyBreaks(), shiftGear() and ride() should belong to Rider class. wash() should belong to Washer class. So we can re-structure it as below after applying single responsibility principle.
Bike
1 2 3 4 5 6 7 8 9 10 11 |
package com.thejavageek.dp; public class Bike { public void start() { } public void stop() { } } |
Rider
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.thejavageek.dp; public class Rider { public void ride(Bike bike) { } public void shiftGear(Bike bike) { } public void applyBreaks(Bike bike) { } } |
Washer
1 2 3 4 5 6 7 8 9 |
package com.thejavageek.dp; public class Washer { public void wash(Bike bike) { } } |
Now all our classes have their a single well defined responsibility. I hope this article helped understand how single responsibility principle can be applied to achieve more flexible and maintainable code.
First time in history a bike is made that starts and stop itself.
My job is to provide very short examples which make things easier to understand. As I’ve clearly mentioned, we are all free to provide inputs. I would really appreciate inputs from you which provide more information to all people.
You can think like this: A bike has some mechanism to start, some laws of Physics, but the rider don’t need to know what happens after he/she step on the pedals. The bike just start.
You should think a little bit further before you reply.
good one