Single Responsibility Principle Examples
We see some of the real-time software products following the SRP rule.
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.
👨🏻💻 Gopi Gorantala Newsletter
Join the newsletter to receive the latest updates in your inbox.