In this article, you will learn about the following:
The Single Responsibility Principle (SRP) is one of the five SOLID design principles that guide software development.
Definition : A class or module should have only one reason to change.
The principle states that a class should have only one reason to change and one responsibility. This principle is intended to promote modularity and help developers create easier code to understand, modify, and maintain.
In essence, the SRP states that each class should have a single, well-defined responsibility and that responsibility should be encapsulated within that class.
This means that a class should not have multiple responsibilities, as this can make it harder to understand and modify. By following the SRP, developers can create more maintainable and flexible code that is easier to work with over time.
The SRP is a fundamental principle in object-oriented programming, and it can significantly impact the quality and effectiveness of software development. We will explore the SRP in more detail, including how it works, why it is important, and how to apply it effectively in Java programming.
There are several reasons why SRP is important in software development:
Overall, adhering to the Single Responsibility Principle improves the quality and maintainability of the codebase, making it easier to manage, test, and deploy.
The advantages of following the Single Responsibility Principle (SRP) include the following:
Some of the disadvantages of the Single Responsibility Principle (SRP) include the following:
When programmers try to add features or new behavior to a software product, they frequently integrate everything within the current class. When something needs to be changed later, due to the complexity of the code, the code refactor process is extremely time-consuming and tedious.
The Single Responsibility Principle helps us create simple classes that perform just one task. This helps make modifications or adding extensions to the existing code much more effortless.
We design Twitter registration software with the help of the single responsibility principle that contains a notification service, database repository, account service, and execution class.
Let's implement the first design principle on Twitter registration flow.
Consider a use case where a user wants to register with Twitter. The steps Twitter takes to register are user are:
Twitter asks users to register with signup forms.
Twitter stores the user object in its database, which contains User
details - email
, name
, password
, and other metadata, etc.
Twitter sends a welcome message to the user.
Let us declare a class that does the above steps.
public class TwitterRegistration {
public void register() {
// step 1
System.out.println("Fill signup form");
// step 2
System.out.println("Store account details in database");
// step 3
System.out.println("Send a welcome message");
}
}
We are creating an account on Twitter, and the three steps should be handled separately. But the above class declared them all in a single TwitterRegistration
class. Isn't this a violation of SRP?
Also, step 2 of storing an object in the database requires additional work to open a connection with the database, authenticate the handshake, and store the user object. This is insertion logic and should be handled separately.
The third step is sending out a welcome message to the user, which should be handled by NotificationService
, separately.
Using the SRP principle, we divide the above TwitterRegistration
class into three different classes, each having a single and only one responsibility.
Refactoring TwitterRegistration the class gives:
// Notification Service
class NotificationService {
public void sendNotification() {
// step 3
System.out.println("Send out welcome message");
}
}
// Database handshakes
class AccountRepository {
public void createUser() {
// step 2
System.out.println("🔐 Auth Success!");
System.out.println("Store user data into database");
}
}
// Account Registration
class TwitterAccountRegister {
public void registerUser() {
// step 1
System.out.println("fill account internal details");
}
}
Finally, after refactoring the above classes. We first allow the TwitterAccountService
to create a couple of objects for AccountRepository
and NotificationService
to register users with Twitter.
// Execution Class or Main class
public class TwitterAccountRegister {
public static void main(String[] args) {
TwitterAccountService service = new TwitterAccountService();
service.registerUser();
}
}
// Account Registration Service
class TwitterAccountService {
AccountRepository repository = new AccountRepository();
NotificationService notificationService = new NotificationService();
public void registerUser() {
// step 1
System.out.println("fill account internal details");
repository.createUser();
notificationService.sendNotification();
}
}
// Notification Service
class NotificationService {
public void sendNotification() {
// step 3
System.out.println("Send out welcome message");
}
}
// Database handshakes
class AccountRepository {
public void createUser() {
// step 2
System.out.println("🔐Signup Success!! Registered");
System.out.println("Store user data into database");
}
}
/*
Outputs:
fill account internal details
🔐Signup Success!! Registered
Store user data into database
Send out welcome message
*/
In above, TwitterAccountService
is doing all three tasks. The primary responsibility is to fill in account details in account details and delegate the other responsibilities to other classes.
Finally, we know many teams work on the same software product.
By following the SRP principle, if we see file changes in GitHub for a file named TweetAnalytics
, we can be sure that the changes incorporated are related to analytics. This helps version control with ease.
In conclusion, the Single Responsibility Principle (SRP) is a software design principle that states that every class should have only one reason to change.
Following SRP makes code easier to understand, maintain, and extend. It reduces the risk of introducing bugs and makes it easier to test individual components in isolation.
SRP encourages the separation of concerns, making the code more modular and scalable. This principle is one of the five SOLID principles of object-oriented design and is an important aspect of creating clean, maintainable, and scalable code.
You will get the complete written notes @
<https://www.ggorantala.dev/ ](https://www.ggorantala.dev/)
Also published here