Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Sunday, April 30, 2023

What is SRP or Single Responsibility Principle Example in Java? SOLID Design Pattern Example

Hello guys, if you want to learn about SRP or the Single Responsibility principle and wondering how to write code that follows SRP and what are pros and cons of following this design principle then you have come to the right place. Earlier, I have shared the best object-oriented programming and design courses and books and in this article, I am going to talk about SPR. The Single Responsibility Principles, popularly known as SRP is part of 5 Object-oriented design principles, introduced by Robert C. Martin, popularly known as Uncle Bob. The Single Responsibility principle says that "there should be one reason to change a class". This means a class should do only one thing, and do it perfectly.

For those, who are not very familiar with SOLID design principles, it's a collection of five best practices of Object-Oriented design, which was first introduced by Robert C. Martin in his book Agile Software Development, Principles, Patterns, and Practices

SOLID is an acronym, where S represents the Single Responsibility principle, O represents Open Closed design principle, L is for Liskov Substitution, I is for Interface segregation and D represents the Dependency Inversion design principle.

You can also check my earlier post about 10 Object Oriented and SOLID design principles for Java Programmer, for a quick review of those principles. Combined with core Object-oriented concepts e.g. Abstraction, Encapsulation, Inheritance, Polymorphism, and Composition, these principles help you write better, modular, flexible, and easy to manage code.

Uncle Bob has shared a lot of tips on creating better code in his book Clean Code, which is worth reading for any Programmer or developer. In this Object-Oriented design tutorial, we will take a closer look at the Single Responsibility design Principle. We will see intent, benefits, drawbacks, and a couple of examples to learn SRP SOLID principle.

Why use SRP or Single Responsibility Principle? Advantanges

Here are some key advantages and benefits of following SRP or Single Responsibility Principles in Java and Programing:

1. The main advantage of SRP or Single Responsibility Principle results in highly cohesive code, because classes just do one thing, and most of the methods tend to use all the instance variables declared in the class since they are closely related to each other.

2. Another benefit of using the Single Responsibility Principle is Separation of concern, Since the class can't do more than one thing, they naturally separate concerns, which otherwise would have to mingle in the class.

3. One more advantage of the SRP SOLID design principle is flexibility. It allows you to incorporate a change in software with minimal impact. Since, if every class does just one thing, then change would be limited to that class only.

4. Single Responsibility Principles also increase the readability of code. It promotes better names, and since class tends to do one thing, there are fewer chances of surprise, to see code doing things other than its name suggests.

If you want to learn more you can also check out these free and best object-oriented programming and design courses to start with. 

What is SRP of SOLID Design Principle? Single Responsibility Principle Example in Java

Drawbacks of the Single Responsibility Principle

1. One of the main disadvantages of the Single Responsibility or SRP principle of SOLID design is too many classes. It's easy to carry away with SRP and creates too many small classes. Even Uncle Bob has advised against this in his book Clean Code. Ideally, you need to use a bit of common sense while using the SRP design principle and make sure you don't carry away with it.

2. Another disadvantage of the SRP SOLID design principle is increased coupling. Since class just do one thing, they tend to depend upon others for complimentary things. Though Developers should try to minimize this coupling by not including unrelated classes together. Even if you tend to depend on other classes, make sure they are logically related.

SRP and Single Responsibility Principle in Java Code Example

There are a lot of places where you see a violation of the SRP design principle. One example is from the Clean Code book itself where a class Employee not only represents Employee data but also how to generate Employee reports. 

Every time there is a change on the report, the Employee class is modified which is a violation of the Single Responsibility Principle.  To fix this, you can move out report generation logic from the Employee object to a separate Report object. 

Here is another, similar example of a Student class that violates the SRP principle. This class is responsible for not only storing student data but also calculating fees, reporting attendance, and saving Student data to the database. Every time there is a change in fee calculation logic this class will be modified. 
public class Student {
    private String firstName;
    private String lastName;
    private int age;

    public long calculateFee();
    public void save();
    public String reportAttendance();


To fix this, you can move out different functionalities into different classes, for example, you can create a FeeCalculator class which can take an Employee object and calculate the fee for him or her. 

Similarly, you can create AttendanceCalculator class to calculate attendance and a Persister class to save student objects. 

public class Student {
 private String firstName;
 private String lastName;
 private int age;


public class FeeCalculator{
   public long calculateFee(Student s){
     // fee calculation logic

public class AttendanceCalculator{
   public String reportAttendance(Student s);{
     // attendance calculation logic

public class StudnetPersister{
   public void save(Student s){
     // save student object to DB/File

That's all on these SOLID and Object-Oriented design tutorials, we have learned about the Single Responsibility Principle or SRP. We have seen the benefits and drawbacks of SRP, and some examples of the Single Responsibility Principle. 

Though it's one of the most basic and useful OOD principles, you should not get carried away with this, don't take it to the level, where you end of with many small classes. Keep your number of classes and package small.

Other Object-Oriented Programming tutorials you may like
  • 5 Free Courses to learn Object Oriented Programming (courses)
  • How to design a Vending Machine in Java? (questions)
  • Difference between Factory and Abstract Factory Pattern? (example)
  • 7 Best Books to learn the Design Pattern in Java? (books)
  • When to use Command Design Pattern in Java (example)
  • Difference between State and Strategy Design Pattern in Java? (answer)
  • 20 System Design Interview Questions (list)
  • 7 Best Courses to learn Design Pattern in Java (courses)
  • How to create thread-safe Singleton in Java (example)
  • Difference between Factory and Dependency Injection Pattern? (answer)
  • 18 Java Design Pattern Interview Questions with Answers (list)
  • 10 OOP Design Principle Every Java developer should learn (solid principle)
  • 5 Free Courses to learn Data Structure and Algorithms (courses)
  • How to implement the Strategy Design Pattern in Java? (example)

Thank you for reading this tutorial so far. If you like this tutorial then please share with your friends and colleagues, if you have any feedback or suggestion then please drop a comment. If you like to learn more about other SOLID design principles like Liskov Substitution Principle then just read Clean Code.

1 comment :

Anonymous said...

Good explanation

Post a Comment