Twitter Registration: Single Responsibility Principle
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.
Table of Contents
Introduction
The Single Responsibility Principle (SRP) states that a class should have only one reason to change. Let's implement the first design principle on Twitter registration flow.
🔐 Signup process on Twitter
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 their 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 for SRP
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.
Conclusion
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.
Gopi Gorantala Newsletter
Join the newsletter to receive the latest updates in your inbox.