What is the SOLID?

Published on March 11, 2019

4 min read

Hi everyone. In this article I will describe SOLID principle. SOLID principle is group of most popular design principles in object oriented programming.

The main purpose of these principles is to ensure that the program we develop is open to future changes and innovations.

S — Single-Responsibility Principle

O — Open-Closed Principle

L — Liskov Substitution Principle

I — Interface Segregation Principle

D — Dependency Inversion Principle

Single Responsibility Principle

The main purpose of the Single Responsibility principle is that a class or method is based on a single task.

For example, if you are developing a method only sending an email you can’t get users inside of this method. Because that method only sending an email.

Wrong :

public void sentEmail(){
// get users
// sent email
}

True :

public void sentEmail(){
// call getUsers method
// sent email
}
getUsers(){
// returns getUsers
}

Open Closed Principle

The objects or entities we developed in the application should be closed to open to change.

If you want to expand the prewritten code, it is necessary to write more extensible codes by using interface or abstract classes and to write extensible codes when needed.

For example :

public interface Animal {
// walks
// eats
}
public class Dog : Animal {
// walks
// eats
}
public class Cat: Animal {
// walks
// eats
}

Liskov Substitution Principle

Liskov’s principle of substitution says that objects created from subclasses must demonstrate the same behavior when they are replaced by objects of the upper classes.

For example :

Wrong :

public class Animal {
// walks
// eats
}
public class Dog : Animal {
// walks
}
public class Cat: Animal {
// walks
// eats
}

Here Dog class is not using all methods from base class so this is wrong for this principle it should be :

public class Animal{
// walks
// eats
}
public class Dog : Animal {
// walks
// eats
}
public class Cat: Animal {
// walks
// eats
}

Interface Segregation Principle

This principle says don’t use unnessecary methods in interface. Because it may cause errors in your application.

For example :

Wrong :

public interface ICar {
// OpenAirCondition
// Start
}
public class BadCar: ICar {
// OpenAirCondition
// Start
}
public class GoodCar: ICar {
// OpenAirCondition
// Start
}

We added OpenAirConditionMethod inside of interface but BadCard doesn’t have air condition property. It should be:

public interface ICar {
// Start
}public interface IAirCondition: ICar {
// OpenAirCondition
// Start
}
public class BadCar: ICar{
// OpenAirCondition
// Start
}
public class GoodCar: IAirCondition{
// OpenAirCondition
// Start
}

Bad car only implements ICar but GoodCar implements IAirCondition and it has OpenAirCondition and Start methods.

Dependency Inversion Principle

High-level modules should not be connected to low-level modules. Both must depend on abstract concepts.( Ex. Interface…)

Wrong :

public class AlarmService {
// Create object to PhoneAlarm
// call functions inside phone alarm
// Set
// Close
}
public class PhoneAlarm {
// Set
// Close
}

Dependency inversion principle :

public class AlarmService {
// Define interface object
// construct interface object
// call functions from Interface
// Set
// Close
}public interface IAlarm {
// Set
// Close
}public class PhoneAlarm: IAlarm{
// Set
// Close
}

I tried to explain SOLID principles but I’m sorry if I made a mistake.

_Thank you for reading. Sharing and applause will increase motivation :).