notes/Media/articles/S.O.L.I.D. Principles - DEV Community.md

53 lines
5.6 KiB
Markdown
Raw Permalink Normal View History

2023-08-06 21:42:41 +02:00
---
author: twitter:Trekhleb
link: https://dev.to/trekhleb/s-o-l-i-d-principles-around-you-1o17
2023-08-20 19:08:16 +02:00
done: false
2023-08-06 21:42:41 +02:00
date: August 2, 2023
---
# S.O.L.I.D. Principles Around You - DEV Community
#programming #software-development #object-oriented-design
![Cover image for S.O.L.I.D. Principles Around You](https://res.cloudinary.com/practicaldev/image/fetch/s--twyS3ck3--/c_imagga_scale,f_auto,fl_progressive,h_420,q_auto,w_1000/https://dev-to-uploads.s3.amazonaws.com/i/0qazxkim2uf50lnwjkhx.png)
In this article, I want to briefly go through [SOLID](https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)) principles (the acronym that stands for five basic principles of object-oriented programming and design) supplying each of them with real-world visual examples to make those principles more understandable, readable and memorizable.
> If you want to see code examples instead you may take a look at [variety of tree data structure implementations](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/tree) in **JavaScript** like [Binary Search Tree](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/tree/binary-search-tree), [AVL Tree](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/tree/avl-tree), [Red-Black Tree](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/tree/red-black-tree), [Segment Tree](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/tree/segment-tree) or [Fenwick Tree](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/tree/fenwick-tree).
So lets move on!
## [](https://dev.to/trekhleb/s-o-l-i-d-principles-around-you-1o17#s-single-responsibility-principle)S — Single Responsibility Principle
\[a.k.a [SRP](https://en.wikipedia.org/wiki/Single_responsibility_principle)\] A class should have only a single responsibility. Only one potential change in the softwares specification should be able to affect the specification of the class.
[![Single Responsibility Principle](https://res.cloudinary.com/practicaldev/image/fetch/s--Qh1_I3hH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xabfs57cezxegih8uh2f.png)](https://res.cloudinary.com/practicaldev/image/fetch/s--Qh1_I3hH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xabfs57cezxegih8uh2f.png)
## [](https://dev.to/trekhleb/s-o-l-i-d-principles-around-you-1o17#o-openclosed-principle)O — Open/Closed Principle
\[a.k.a [OCP](https://en.wikipedia.org/wiki/Open/closed_principle)\] Software entities should be open for EXTENSION, but closed for MODIFICATION. Allow behavior to be extended without modifying the source code.
[![Open/Closed Principle](https://res.cloudinary.com/practicaldev/image/fetch/s--pagpCyfX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fv3xpd9kkfgntqby9eg6.png)](https://res.cloudinary.com/practicaldev/image/fetch/s--pagpCyfX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fv3xpd9kkfgntqby9eg6.png)
## [](https://dev.to/trekhleb/s-o-l-i-d-principles-around-you-1o17#l-liskov-substitution-principle)L — Liskov Substitution Principle
\[a.k.a. [LSP](https://en.wikipedia.org/wiki/Liskov_substitution_principle)\] Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.
[![Liskov Substitution Principle](https://res.cloudinary.com/practicaldev/image/fetch/s--ArU0mGdu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7wdzib8lqfq9bcstfqu3.png)](https://res.cloudinary.com/practicaldev/image/fetch/s--ArU0mGdu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7wdzib8lqfq9bcstfqu3.png)
## [](https://dev.to/trekhleb/s-o-l-i-d-principles-around-you-1o17#i-interface-segregation-principle)I — Interface Segregation Principle
\[a.k.a. [ISP](https://en.wikipedia.org/wiki/Interface_segregation_principle)\] Many client-specific interfaces are better than one general-purpose interface. No client should be forced to depend on methods it does not use.
[![Interface Segregation Principle](https://res.cloudinary.com/practicaldev/image/fetch/s--58sXrCsO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rnwds5cv5qcodlam1wc6.png)](https://res.cloudinary.com/practicaldev/image/fetch/s--58sXrCsO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rnwds5cv5qcodlam1wc6.png)
## [](https://dev.to/trekhleb/s-o-l-i-d-principles-around-you-1o17#d-dependency-inversion-principle)D — Dependency Inversion Principle
\[a.k.a. [DIP](https://en.wikipedia.org/wiki/Dependency_inversion_principle)\] One should depend upon abstractions, not concretions.
- High-level modules should not depend on low-level modules. Both should depend on abstractions.
- Abstractions should not depend on details. Details should depend on abstractions.
[![Dependency Inversion Principle](https://res.cloudinary.com/practicaldev/image/fetch/s--yw39zKqE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wugaxuqznqow3wzgp8hr.png)](https://res.cloudinary.com/practicaldev/image/fetch/s--yw39zKqE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wugaxuqznqow3wzgp8hr.png)
The plug doesnt care which type of wire it uses, it just needs wires that conduct electricity.
I hope these illustrations have been useful for you :)