Single Responsibility Principle in Java

In this tutorial, we will learn about single responsibility principle in java.It is one of SOLID principles and simplest design principle as well.

Single responsibility principle dictates that there should be only one reason to change the class.If you have more than one reason to change the class then refactor the class into multiple classes according to functionality.

We will understand with the help of a simple example.Let’s say you have  Customer class as below.

Do you see the problem with the above class? Let’ see what can be the issue with above class.

  • If there is any change in the calculation of bill then we need to change Customer class.
  • If you want to add one more report type to generate, then we need to change Customer class.

If you notice, calculation of bill and report generation should not be the responsibility of Customer, we should create different classes for these functionalities.

Let’s see refactored class now.

Create a new class named BillCalculator and pass Customer object to it.This class will be responsible for calculation of the customer bill

Create a new class named ReportGenerator and pass Customer object to it.This class will be responsible for generation of the customer report

As you can see now if we need to change anything in bill calculation, we don’t need to modify customer class, we will make changes in BillCalculator class.
Similarly, If you want to add another reporting type, then you need to make changes in ReportGenerator class rather than Customer class.

That’s all about Single Responsibility Principle in java.

Was this post helpful?

Related Posts

  • 02 January

    Interface Segregation Principle in java

    In this tutorial, we will learn about Interface Segregation Principle.It is one of the SOLID principles. In simple term, Interface Segregation Principle dictates that client should not be forced to implement the methods which it won’t be able to use.You can always throw UnsupportedOperationException from the method which you don’t want to use but it […]

  • 02 January

    Open Closed Principle in Java

    In this tutorial, we will learn about Open Closed Design Principle in Java.Open closed principle is one of the SOLID principles. Open Closed Design Principle dictates that “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”. This definition was provided by Bertrand Meyer. Once we have written the class and […]

  • Solid principles in java
    09 November

    SOLID Principles in Java

    Table of ContentsWhat is the meaning of S.O.L.I.D?Single Responsibility Principle (SRP)Open Closed Principle Liskov Substitution Principle Interface Segregation PrincipleDependency Inversion PrincipleWrap Up In this post, we will see 5 SOLID Principles in Java. Robert C. Martin gave five objected oriented design principles, and the acronym S.O.L.I.D is used for it. Each letter of this acronym […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.