Vikas Rajput Profile picture
Oct 20 18 tweets 5 min read Twitter logo Read on Twitter
SOLID principle is one of the most important design principles in OOP languages like Java, Python, C#, etc.

Sadly, most of the programmers find it super difficult to understand.

Here's the simplest guide to understand SOLID principles:
1. Full form

S = Single Responsibility Principle
O = Open/Closed Principle
L = Liskov Substitution Principle
I = Interface Segregation Principle
D = Dependency Inversion Principle

(we will use Java to understand them)
2. Single Responsibility

A class should always have one responsibility and there should be only a single reason to change it.
2.1. Bad Implementation

Below Employee class contains personal details, business logic to perform a few calculations, and DB logic to save/update.

Our class is tightly coupled, hard to maintain, multiple reasons to modify this class. Image
2.2. Good Implementation:

We can split a single Employee class into multiple classes as per their specific responsibility.

It made our class loosely coupled, easy to maintain, and only single reason to modify. Image
3. Open Close

Class should be Open for Extension but Closed for Modification.
3.1. Bad Implementation

Below EmployeeSalary class calculates salary based on employee type: Permanent and Contractual.

Issue: In the future, if a new type(Part-time Employee) comes then the code needs to be modified to calculate the salary based on employee type. Image
3.2. Good Implementation:

We can introduce a new interface EmployeeSalary and create two child classes for Permanent and Contractual Employees.

By doing this, when a new type comes then a new child class needs to be created and our core logic will also not change from this. Image
4. Liskov Substitution

Child Classes should be replaceable with Parent Classes without breaking the behavior of our code.
4.1. Bad Implementation

Below, TeslaToyCar extends Car but does not support fuel() method as its toy. That's why it's violating the LS principle.

In our code where ever we've used Car, we can't substitute it directly with TeslaToyCar because fuel() will throw Exception. Image
4.2. Good Implementation

Creating new subclass RealCar from parent Car class, so that RealCar can support fuel() and Car can support generic functions support by any type of car.

As shown below, TeslaToyCar and TeslaRealCar can be substituted with their respective Parent class. Image
5. Interface Segregation:

Interface should only have methods that are applicable to all child classes.

If an interface contains a method applicable to some child classes then we need to force the rest to provide dummy implementation.

Move such methods to a new interface.
5.1. Bad Implementation:

Vehicle interface contains the fly() method which is not supported by all vehicles i.e. Bus, Car, etc. Hence they've to forcefully provide a dummy implementation.

It violates the Interface Segregation principle as shown below: Image
5.2. Good Implementation:

Pulling out fly() method into new Flyable interface solves the issue.

Now, Vehicle interface contains methods supported by all Vehicles.

And, Aeroplane implements both Vehicle and Flyable interface as it can fly too. Image
6. Dependency Inversion

Class should depend on abstractions (interface and abstract class) instead of concrete implementations.

It makes our classes de-coupled with each other.

If implementation changes then the class referring to it via abstraction won't change.
6.1. Bad Implementation

We've got a Service class, in which we've directly referenced concrete class(SQLRepository).

Issue: Our class is now tightly coupled with SQLRepository, in future if we need to start supporting NoSQLRepository then we need to change Service class. Image
6.2. Good Implementation

Create a parent interface Repository and SQL and NoSQL Repository implements it.

Service class refers to Repository interface, in future if we need to support NoSQL then simply need to pass its instance in constructor without changing Service class. Image
Thanks for reading!

This Saturday, I'm sharing my life story to @thehumansoftech and will be sharing my entire journey as a Backend Engineer do join in.

Details below:

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Vikas Rajput

Vikas Rajput Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @vikasrajputin

Oct 9
SQL Commands in a Nutshell:
DDL (Data Definition Language): Concerned with the structure or schema of the database.

CREATE: Used to create objects like tables, views, indexes, etc.

ALTER: Used to modify existing database objects. For example, it can add, delete, or modify columns in an existing table.
DROP: Used to delete database objects. For example, DROP TABLE deletes an existing table.

TRUNCATE: Used to remove all records from a table, including all spaces allocated for the records.
Read 10 tweets
Oct 6
To master docker, you need to have a solid understanding of how docker volume works.

Here's a quick guide to help you understand everything about volumes:
1. Introduction to volumes

Docker volumes are the preferred mechanism for persisting data generated and used by docker containers.

Think of it as an external hard drive you plug into multiple computers (containers).
2. Why need volumes?

Containers are temporary while data is permanent.

Volumes protect data even if a container dies.

Without volumes:
• Data in containers is lost when the container is killed.
• Sharing data among different containers becomes difficult.
Read 7 tweets
Sep 18
SOLID principle is one of the most important design principles in OOP languages like Java, Python, C#, etc.

Sadly, most of the programmers find it super difficult to understand.

Here's the simplest guide to understand SOLID principles:
1. Full form

S = Single Responsibility Principle
O = Open/Closed Principle
L = Liskov Substitution Principle
I = Interface Segregation Principle
D = Dependency Inversion Principle

(we will use Java to understand them)
2. Single Responsibility

A class should always have one responsibility and there should be only a single reason to change it.
Read 18 tweets
Sep 15
Mostly, we can store our data using two popular ways:

1) SQL
2) NoSQL

Here's a quick guide to help you choose which one suits your needs best:
1. SQL

- Used to store relational data, where one set of data is related to another

- It is strictly structured in nature, so before using it we need to specify what exact structure we need to use
2. NoSQL

- Used to store unstructured data, where each set is not expected to be related to each other

- It's not strict in nature and its schema can be changed at runtime as well
Read 10 tweets
Sep 13
JWT is now become the de-facto standard for authenticating Modern APIs.

This guide will make it dead simple for you to understand JWT:
1. JWT stands for JSON Web Token
2. It's a token that is used to authenticate and authorize users in an application.

"authenticate" means who they're.
"authorize" means what they can access.

The token itself contains, all the necessary information about the user, like user ID and role, etc, in a JSON.
Read 9 tweets
Sep 11
No matter if you're learning SQL for Data Science or Software Development.

You should master SQL "Normalization".

Here's a simple guide to get you started:
1. Introduction

Normalization is a process to eliminate redundant data, prevent data anomalies, and ensure data integrity.

It makes your life super easy to manage, store and query data effectively in longer run!

Normalization happens in multiple stages:

1NF, 2NF, 3NF
2. Realworld Example

To understand normalization, let's consider a simple restaurant database example.

We have one table "Orders" with columns: OrderID, CustomerName, Dish, Chef, DishType, Price. Image
Read 11 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(