SOLID Design Principles
Object-Oriented Programming has brought a successful solution to many issues that we were facing in the past. but, as known nothing continue like a star always!
By the time, we discovered some limitations and problems for the implementations, like Fragile Classes, doesn’t prevent Confusing and Diamond Problem, that pushed for searching for a better solution.
Michael Feathers, Invented SOLID Principles as a mnemonic acronym that stands for the advocated five programming concepts:
- Single Responsibility Principle
- Open Closed Principle
- Liskov Substitution Principle
- Interface Segregation Principle
- Dependency Inversion Principle
In the 2000s a paper published for Robert C. Martin that introduces the Design Principles and Design Patterns.
Throughout a series of articles, we are going to discover these principles with some sample implementation examples.
Single Responsibility Principle (SRP)
A class should have one and only one reason for the change.
Which means, 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.
So, How we can apply this principle?
- Each class and module should focus on a single task at a time.
- Everything in the class should be related to that single purpose.
- There can be many members in the class as long as they related to the single responsibility.
And, this will have an impact on the design architecture like, classes become smaller and cleaner and the code itself will be less fragile.
Suppose we have a user class with some functionality to do, so we will create a class like below and add the functionality to it as normal implementations.
class User {
fun register(email: String, userName: String, password: String){
System.out.println("User with email: $email and name: $userName and $password Registered in Successfully!")
}
fun login(userName: String, password: String) {
System.out.println("User with name: $userName and $password logged in Successfully!")
}
fun logError(issue: String) {
System.out.println(issue)
}
fun sendEmail(emailText: String) {
System.out.println("sending new Email with text: $emailText")
}
}
What is the problem with that?
After a while a new user type to be implemented with some or extra features to the old one as well as some permissions which reflects in touching the old implementation of the class and resulting in a Fragile code that violates the principle.
How we can solve it?
We look at the functionality that we wanted to provide to the instances of that class and try to divide it relatively by job.
open class User {
fun register(email: String, userName: String, password: String){
System.out.println("User with email: $email and name: $userName and $password Registered in Successfully!")
}
fun login(userName: String, password: String) {
System.out.println("User with name: $userName and $password logged in Successfully!")
}
}class LoggerUser : User() {
fun logError(issue: String) {
System.out.println(issue)
}
}class SendEmailUser : User() {
fun sendEmail(emailText: String) {
System.out.println("sending new Email with text: $emailText")
}
}
Now, We have solved the issue. but is that implementation doesn’t lack other principles? what if we need to add a new user type with both functionalities, sending emails and reporting issues? this is what we will discuss in the next article.
Summary
We have seen the fundamental design principles that must guide how you develop software. We took a close look at the implementation of SRP and how it helps to make the code more isolated based on relative functionality. If you are just starting out these might seem a bit abstract, but like all specialized knowledge, they are needed to communicate well with your colleagues. They are fundamental to organize your knowledge and practice of Object Oriented Programming so that you can create code that is easily understandable by other people.
Remember always you are a Programmer, not Plumper! Thanks for continuing the article. If you like it just clap and share!
see you at the second principle!