This is the first article in our series of articles covering the SOLID Design Principles.
What are the SOLID Design Principles?
SOLID is a set of object-oriented design principles allowing you to write understandable and maintainable code. It makes sure that changing one part of your code doesn't impact other parts. SOLID is an acronym for 5 key design principles:
- S - Single Responsibility principle
- O - Open-Closed principle
- L - Liskov Substitution principle
- I - Interface Segregation principle
- D - Dependency Inversion principle
In this series, we will cover all these 5 principles. We will cover the first one in this article.
Single Responsibility principle
The Single Responsibility Principle states that a class should have only one responsibility means that it should have only one reason to change or in other words, it should have only one job.
If your class has multiple responsibilities, it's better to split it into multiple classes to avoid the violation of single responsibility.
Example
Consider we're building an application to manage the students and their courses.
Naive Solution
We might have a class like this:
class Student {
constructor(name) {
this.name = name;
this.courses = [];
}
enrollCourse(course) {
this.courses.push(course);
this.notify(course);
}
notify(course) {
console.log(`${this.name}, you have successfully enrolled ${course}.`);
}
}
const Shahmir = new Student("Shahmir");
Shahmir.enrollCourse("Physics");
// Output
// Shahmir, you have successfully enrolled Physics.
Here we have 2 properties, the name, and courses of the student. Then a function enrollCourse for adding courses and uses the notify function to notify the student about that course.
Now the solution looks good, isn't it? However, it's violating the single responsibility principle.
Why's it Violating the Single Responsibility?
If you carefully look at the above example, it has 2 responsibilities:
- Enrolling Courses
- Notifying Students about the new enrolled course
The class has 2 reasons to change:
- if we want to change how we're enrolling in the course
- if we want to change how we notify the user
Here are the problems with the notify function:
- What if we want to send an email or a push notification to notify the student instead of just printing the message in the console? We need to come back and make changes to the class.
- What if we have 10 different classes and we want the notify functionality in them? We need to write the function 10 times.
- And what if we want to change the function again? We need to make changes in 10 different places.
Better Solution
Why not just take that function out of the class? This will make the class use the single responsibility principle and we will be able to use that function wherever we want. And whenever we want to make changes, we will only change one part of the code without affecting the other parts.
function notify(message) {
console.log(message);
// we can also email or send a push notification
}
class Student {
constructor(name) {
this.name = name;
this.courses = [];
}
enrollCourse(course) {
this.courses.push(course);
notify(`${this.name}, you have successfully enrolled ${course}.`);
}
}
const Shahmir = new Student("Shahmir");
Shahmir.enrollCourse("Physics");
// Output
// Shahmir, you have successfully enrolled Physics.
Now the class has only one responsibility and that is to enroll the courses. The notify function is separate from the class.
So that's it. Will see you in the next article covering the open-closed principle.
Top comments (0)