A very simple explanation of the principles of SOLID

Disclaimer: Everyone can, well, what do I do worse ?!

SOLID is a set of code organization principles. In fact, they declare certain rules that will help you save your and other people's nerves and time. And they may not help.

Let's try to understand these principles on the fingers, without code examples and SMS.

S - Single Responsibility Principle or SRP

There must be one and only one reason for changing the class (“A class should have only one reason to change.” Robert C. Martin.)
Imagine that you have on your computer there are five favorite songs, a couple of movies and pictures of cats. You bring it all into My Documents and, in general, rejoice in life.

Then you download more movies, a new album of your favorite band and a dozen new cats. In My Documents, it somehow becomes uncomfortable and you put everything in daddies.

  Music
 Films
 Cats 

Then you connect the high-speed unlimited, break off the coils and download all the Simpsons series, full discography of your favorite groups, and photos from the fishing trip with friends are added to the seal photos. Daddies begin to branch.

  Music
   The doors
     Waiting for the Sun
       Hello, I Love You.mp3
       ...
     ...
   Rhcp
   Agutin
 Video
   TV series
   Films
   Studio Private
 A photo
   Cats
   Fishing
   Preference in the country
   courtesans

And what do we have here?


What could be the reason for the change of “The Doors”? Only one - (qualitative or quantitative) change in the songs of The Doors. But the removal of the annoying series from "/ Video / Series /" cannot be such a reason, just like the renaming of "Music" into "Music".

What conclusions can be drawn?

  1. SRP is about decomposition (decomposed into subfolders) and connectivity (“Hello, I Love You.mp3” is associated only with “Waiting for the Sun” and she doesn't care about changes in “../Serials”. On the other hand, all songs “The Doors” are inside it and should not be in the Kitties folder).
  2. The level of abstraction of the reason for the change must be no higher than the level of abstraction of the variable entity. Adding to the "Music" subfolders "Alla Pugacheva" can in no way be the cause of the change "Waiting for the Sun".
  3. No need to bring to the point of absurdity. If you have three songs, one vidos and five photos of cats, they will look great in one heap - scattering them into folders will only confuse everything. Like the compilation "The best of the doors" you should not break it into subfolders by year, each of which will have one song.

O - Open / closed Principle or OCP

Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification ("Software entities (classes, modules, functions, etc.) should be open for extension, but not for modification." Bertrand Meyer)
Let's return to that step of our history when you connected the high-speed unlimited. Let's say that first of all you pumped all sorts of films about plumbers of the film company Private and, since they were all about plumbers, created the folder "/ Video / About Plumbers /".

After a while you downloaded more films from this studio, but already about pizza delivery. Moreover, your new girlfriend wrote you an SMS message “I downloaded the movie“ Afonya ”and saved it to the folder / Video / About plumbers / ok?”. And everything seems to be right - about plumbing, but there is a nuance.
And here it becomes clear to you that changing the functionality is impossible without modifying the already existing folder (class) structure, and this is a clear violation of the OCP principle.

How in this case should have been done? It is very simple - initially to design the system in such a way that the new functionality does not require changing the old code. Those. Do not hardcod the folder "../Pro Plumbers /", hoping that in the future there will be only about them, but to raise the level of abstraction to "../Studio Private /" and calmly feed her and plumbers, peddlers and other pizza - other ...

And for Afoni, create a new class, for example, "../ Mosfilm /", expanding the class "/ Video /"

Findings:

  1. Think ahead of what you will do if there are “other” plumber films. Maybe you should do it right away in your mind so as not to redo it later?
  2. This principle is mainly about abstract classes ("../Studio Private /").

L - Barbara Liskov Substitution Principle (LSP)

Objects in the program must be replaceable by instances of their subtypes without altering the correctness of the program execution.
Well, it's quite simple.

For an explanation, let's turn to the cute and nyashnosti, and specifically to the folder "/ Photos / Seals /".
We love cats and photos we have collected a lot.

  A photo
   Cats
     Redhead
     Striped
     Wet
     Black
     Manula 


It even happens that we directly run a slideshow across the entire root folder and admire. And so, one fine moment, when you have practically reached nirvana, the screen displays:
Unable to display the file "/ Photo / Seal / Books / Puss in Boots.fb2"
As it turned out, your girlfriend decided to raise the degree of grace and inherited "Kitties" with a new subfolder of "Books", grossly breaking the LSP, since the subclass of "Books" could not be used instead of the base class "Photo".

Findings:

  1. The name is frightening, the definition is complex, but the principle itself is simple and intuitive.
  2. If your method expects to enter "Photo", then he should not care what heir from "Photo" you will slip to him: "Manula", "Redhead" or "Wet", but if he receives "Books" at the entrance, then he is expected to choke on tea.

I - Interface Segregation Principle or ISP

Clients should not depend on methods that they do not use.
Here it will be difficult to explain with daddies and files, but I will try - do not judge strictly for some tension.

You are tired of the standard music player and you decide to download a new, trendy and hyip. He even knows how to display the cover of a music album and display the subtitles of the song being played. But the trouble is that if there are no “cover.jpg” and “subtitles.txt” files in the album folder, the player crashes. And you, cursing everything, begin to create these files in all subfolders with albums.

That is, by making incorrect analogies, we obliged the Music class and all its heirs to implement the AudioCoverSubtitles interface. At the same time, this interface is fully implemented only by the album “Waiting for the Sun”, the album “The best of The Doors” implements only part of “Audio + Cover”, and all the others are only “Audio”.

This brings us to the idea that it makes sense to divide the thick “AudioCoverSubtitles” interface into three small “Audio”, “Cover” and “Subtitles” and apply them only where they are really needed.

Findings:

  1. ISP is, suddenly, about the separation of interfaces.
  2. If your interface forces you to create stub methods, then this is a bad interface and you should walk with scissors.

D - dependency inversion principle (DIP)


The modules of the upper levels should not depend on the modules of the lower levels. Both types of modules must depend on abstractions.

Abstractions should not depend on the details. Details must depend on abstractions.
The Doors module should not depend on what kind of audio files are folded in it, .mp3, .flac or .wav.

The “The Doors” module, its “Waiting for the Sun” sub-module (and all the others), depends on the “Music” high-level abstraction, which defines their implementation (that they have music inside).

Suppose we decided to divide the storage of music on the principle of compression - “with losses” and “without losses”. These are the details that are united by the dependence on the abstraction "Music" - in them, ultimately, there must still be music. At the same time, the abstraction “Music” does not depend on these details. She doesn’t care, with or without loss of music - she was like music, so she remains.

Findings:

  1. DIP - this is about the particular should depend on the general, and not vice versa.
  2. DIP is “More Abstractions to God of Abstractions!”
  3. DIP is also about cause and effect, about the correct answer to the question “The branches swing from the fact that the wind blows or the wind blows from the fact that the branches swing”

Thank you for your attention and have a great weekend!

Source: https://habr.com/ru/post/413707/


All Articles