Single Responsibility Principle (SRP): This says that each class or block of code (method) should have a single responsibility to accomplish the task. Means, there should be only one reason in the future to change the existing class or block of the code and that should be the purpose of that code when you have created it. Before writing any new module or class, please check that it should not violate the SRP.
- Everything in that block of code should fulfill the single purpose. For example, if we have one class as Authorization which has one method as validating the account user then it should only validate the user, it should not send the email as well. It should perform only one task which belongs to the purpose of that class.
- SRP does not say that it should be only one method or property, it could be many, but all should consider the purpose of that class where these are declared. In Authorization class, you can create multiple methods, but these methods should purpose to user validating like check user authorization, check user role permission, check user to any resource permission etc.
- If something requires changing in that class, then it should not affect or altered to other classes.
As per the Wikipedia: The single responsibility principle is a computer programming principle that states that every module, class, or function should have responsibility for a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class.
- Help us to resolve most of the architecture issues at the time of designing the system.
- Help us to create the loosely coupled system.
- Encapsulate the complexity from the user.
- providing an extensible program
Module Level: SRP also applies in module level. Means, while creating the module and classes inside this, we should take care that classes should logically perform the similar tasks [Which perform the similar functionality].
Class Level: SRP also applies in class level, here it says that a class should contain the methods which have similar nature. If a class is a User specific then it’s methods should be performed only user-specific action, not any other.
Method Level: In the method level, SRP says that a method performs only one task at a time. If there should require performing multiple tasks, then it should be separated to create new methods.
Wrong Code: a class with two methods, one for validating the user and other for sending the email.
class Authentication {
constructor() {
}
userValidation(userName: string): boolean {
// Validate the user
if (userName != '' && userName == 'Admin') {
this.sendEmail();
return true;
} else {
return false;
}
}
private sendEmail(): void {
// Send email to user.
// Email send code goes here.
alert('Email Sent');
}
}
let authentication = new Authentication();
let userStatus: boolean = authentication.userValidation('Admin');
alert('User authentication status is : ' + userStatus);
Correct Code: two different classes with one method as validating the user and if something happens then sending the email respectively.
// Email Service
class EmailService{
sendEmail(): void {
// Send email to user.
// Email send code goes here.
alert('Email Sent');
}
}
class Authentication {
// Inject the dependency
constructor(private emailService: EmailService) {
}
userValidation(userName: string): boolean {
// Validate the user
if (userName != '' && userName == 'Admin') {
this.emailService.sendEmail();
return true;
} else {
return false;
}
}
}
let authentication = new Authentication(new EmailService());
let userStatus: boolean = authentication.userValidation('Admin');
alert('User authentication status is : ' + userStatus);
Conclusion
So, today we have learned about Single Responsibility Principal in Typescript.
I hope this post will help you. Please put your feedback using comment which helps me to improve myself for the next post. If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends. Thanks
You are here, it means, you have read something useful. So, If you like this article then please connect me on