The Single Responsibility Principle

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

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.

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.




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.


Share Button

4 comments for “The Single Responsibility Principle

  1. abhi
    November 22, 2016 at 10:44 am

    First time in history a bike is made that starts and stop itself.

    • November 22, 2016 at 1:27 pm

      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.

    • Nyng
      April 4, 2017 at 9:52 am

      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.

  2. subhasish sahu
    September 19, 2017 at 3:16 pm

    good one

Leave a Reply

Your email address will not be published. Required fields are marked *