Skip to content

Single Responsibility Principle Examples

We see some of the real-time software products following the SRP rule.

Gopi Gorantala
Gopi Gorantala
1 min read

Table of Contents

SRP examples

Here are some examples of Java code that follow the Single Responsibility Principle (SRP):

Product

A "Product" class that has only one responsibility of storing and exposing product data:

public class Product {
   private String name;
   private double price;
   private String description;

   public String getName() {
      return name;
   }

   public double getPrice() {
      return price;
   }

   public String getDescription() {
      return description;
   }

   // Other methods for setting product data
}

The Product class has only one responsibility of storing and exposing product data. The class has a clear and well-defined purpose, and any changes related to managing or exposing product data will only affect this class.

This makes the code more maintainable and reduces the likelihood of unintended side effects.

Calculator

A Calculator class that performs mathematical calculations:

public class Calculator {
   public static double add(double num1, double num2) {
      return num1 + num2;
   }

   public static double subtract(double num1, double num2) {
      return num1 - num2;
   }

   public static double multiply(double num1, double num2) {
      return num1 * num2;
   }

   public static double divide(double num1, double num2) {
      return num1 / num2;
   }

   // Other mathematical functions
}

The Calculator class has only one responsibility performing mathematical calculations. The class is not responsible for managing input/output or other functionality outside mathematical operations.

Limiting the class's responsibility makes it easier to maintain and test the code.

Database

A "Database" class that is responsible for managing database connections and executing queries:

public class Database {
   private Connection conn;

   public void connect() {
      // Code to establish database connection
   }

   public void disconnect() {
      // Code to close database connection
   }

   public ResultSet executeQuery(String query) {
      // Code to execute database query
   }

   // Other methods for interacting with the database
}

The Database class has only one responsibility managing database connections and executing queries. The class is not responsible for any other functionality outside of database management. By limiting the class's responsibility, it becomes easier to modify and test the code.

In each of these examples, the class has a single responsibility that is encapsulated within the class. This makes the code easier to understand, modify, and maintain.

All three examples demonstrate SRP by adhering to the principle that each class should have only one reason to change. This results in easier-to-read, understand, and maintain code, reducing the likelihood of bugs and errors.

SOLID PrinciplesCore Java

Gopi Gorantala Twitter

Gopi has been a Full Stack(Java+React) since 2016. He worked in Europe, and India, from startups to tech giants. He has a strong engineering professional with a Bachelor's in Satellite Communications.

Comments


Related Posts

Members Public

Array Implementation

You will learn how to implement a custom array-like data structure.

Array Implementation
Members Public

A Guide to Common Errors and Exceptions in Java and How to Fix Them

You will be introduced to the most common errors and exceptions in Java. With examples and steps to rectify them. Some tips and suggestions to developers are provided in detail.

Members Public

Introduction to the Single Responsibility Principle

SRP states that each class should have a single, well-defined responsibility and that responsibility should be encapsulated within that class.