SOLID Principles (Series) [PART 1]
SOLID - make Object-Oriented designs more understandable, flexible and maintainable.
![SOLID Principles (Series) [PART 1]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1664732260802%2FiUb4w1qzK.jpg&w=3840&q=75)
Senior Android Engineer from Bangladesh. Love to contribute in Open-Source. Indie Music Producer.
Search for a command to run...
SOLID - make Object-Oriented designs more understandable, flexible and maintainable.
![SOLID Principles (Series) [PART 1]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1664732260802%2FiUb4w1qzK.jpg&w=3840&q=75)
Senior Android Engineer from Bangladesh. Love to contribute in Open-Source. Indie Music Producer.
No comments yet. Be the first to comment.
In this series SOLID Principles with real life example. Happy learning...
SOLID - make Object-Oriented designs more understandable, flexible and maintainable.
A practical guide to choosing the right coroutine primitive for your Android/Kotlin projects

This guide explains what Channels are, when to use them, how to use them correctly, and when NOT to use them — in simple, professional language. 1. What problem do Channels solve? In Kotlin coroutines, you often have multiple coroutines running at t...

Learn how to build preview-safe ViewModels in Jetpack Compose using Hilt.

HandlerInterceptor vs Filter in Spring Boot: A Practical Guide with JNDI Injection Prevention

Simple In-Memory Caching: A Tiny Trick with Massive Impact

If get to know something new by reading my articles, don't forget to endorse me on LinkedIn
SOLID Principles are an object-oriented approach that are applied to software structure design. SOLID principles were introduced by Robert C. Martin in his 2000 paper “Design Principles and Design Patterns.”
Why Design Principles matters?
Encourage us to create more maintainable, understandable, and flexible software.
As applications grow in size, we can reduce their complexity and save ourselves a lot of headaches.
Software is modular, easy to understand, debug, and refactor.
**S: **Single Responsibility Principle.
**O: **Open–Closed Principle.
**L: **Liskov Substitution Principle.
**I: **Interface Segregation Principle.
**D: **Dependency Inversion Principle.
In this series, we'll start by defining each principle and then some examples to help us understand how and why we should use these principles in our code.
The SRP states that every Class must perform a single functionality. Implementation of multiple functionalities in a single class mashup the code and if any modification is required may affect the whole class. It precise the code and the code can be easily maintained.
class User {
fun getFirstName(): String {...}
fun getLastName() : String {...}
fun sendEmail(content : EmailContent) : Boolean {...}
fun updatePrimayEmail(newEmail : String): Boolean {...}
fun sendSMS(content : SMSContent) : Boolean {...}
}
We have a class called User, which has some responsibilites getFirstName(), getLastName(), sendEmail(content : EmailContent), updatePrimayEmail(newEmail : String), sendSMS(content : SMSContent). Everything looks good and okay, right? Yes but only for this time only. Because it will lead to some challenges. We can not make the codes resuable. All responsiblites of this class is interconnected and that would be hard to fix any error if occurs. Also, as long as the code base grow, responsibilites added to the Object, makes it harder to refactor, maintain, or fixing any issue.
Let's talk about Email sending responsibilites. Currently, we have one API called sendEmail(content:EmailContent) which perform operation to send an email to a specific user. Lets say, User might have secondary email address. Also, we might need an API to re-send user verification email. If we add all of those APIs one by to the User object, object will get messy, un-maintainable, hard to refactor and if any issue occurs hard to fix.
So, how do we solve this problem? By following SRP.
We can move all of the email related APIs to another service called EmailService. Now, EmailService is responsible to provide APIs for email related business logic.
class EmailService {
fun sendEmail(user : User, content : EmailContent): Boolean {...}
fun resendVerificationEmail(user: User, content : ResendEmailContent) : Boolean {...}
}
For, mobile SMS related APIs can be moved to SMSService
class SMSService{
fun sendSMS(user : User, content : SMSContent): Boolean {...}
}
So, our final User class would be
class User {
fun getFirstName(): String {...}
fun getLastName() : String {...}
fun updatePrimayEmail(newEmail : String): Boolean {...}
}
Now, responsiblites of our User object has been moved to respective services. Thus we can acheive SRP. Keep in mind that, this is just an basic example.
In next part, we will talk about Open–Closed Principle.
That's it for today. Happy Coding...