What is SOLID
Single responsibility principle
Open/closed principle
Liskov's substitution principle
S.O.L.I.D. is an abbreviation of the five basic principles of object-oriented programming and design. SOLID principles are used for the design and development of such software systems, which, with a high probability, will be able to develop, expand and maintain for a long time.
One class - one task, one class should be responsible for one thing. It is better to organize the code in such a way that when changing, the least number of modules are used.
Pros:
Class
getField
log
Class
getField
Logger
log
Printer
printToPdf
printToExcel
PrinterToPdf
printToPdf
PrinterToExcel
printToExcel
Entities must be open for extension but closed for modification.
Changing existing code is bad because it is already tested and working. Therefore, it is better to add new functionality by adding a new entity, and not by changing an existing entity.
Pros:
entity 1
entity 2
entity 3
entity 4
new functional
functional
functional
functional
new functional
Entities that use the parent type must work the same way with child classes.
Inherited class should complement, not replace, the behavior of the base class.
The base class must have methods that are available to all child classes.
Pros:
Class 1
first()
second()
Class 2
extends
first()
second()
third()
Class 3
first()
second()
third()
uniqualMethod3()
Class 4
first()
second()
third()
uniqualMethod4()
Class 5
first()
second()
third() { return: null }
uniqualMethod5()
extends
Program entities should not depend on methods they do not use.
It's related to the first and third principles.
The principle is to divide large interfaces into smaller, highly specialized that solve one task.
Pros:
Interface1
method1()
method2()
Class1 implements Interface 1
method1()
method2()
method3()
method3()
Class2 implements Interface 1
method1()
method2()
method3()
Class3 implements Interface 1
method1()
method2()
method3()
Interface1
method1()
Class 1
method1()
method2()
Class 2
method1()
method2()
Class 3
method1()
method3()
method3()
Interface2
method2()
Interface3
method3()
Class 1 implements
Interface1, Interface2, Interface3
Class 2 implements
Interface1, Interface2
Class 3 implements
Interface1, Interface3
High-level modules should not depend on low-level modules.
All of them must depend on abstractions.
Abstractions should not depend on details. Details must depend on abstractions.
Pros:
Pros:
module 2
module 1
module 3
details 1
details 2
module 2
module 1
module 3
details 1
details 2
abstract class
abstract class
class HttpService
request() {}
interface Connection
request() {}
class Http
constructor(httpService: HttpService) {}
get() { this.httpService.request() }
class Http
constructor(httpService: Connection) {}
get() { this.httpService.request() }
class HttpService implement Connection
request() {}
Low level
High level