📜 ⬆️ ⬇️

How not to understand the principles of the development of architecture SOLID

There is a problem with the description and interpretation of the principles of the development of the SOLID architecture (authored by Robert Martin). Many sources give their definition and even examples of their use. Studying them and trying to try on using myself, I consistently caught myself thinking that there was not enough explanation for the magic of their use. And trying to see the internal gears, to understand - and for me it means to memorize - laid them out in their own "terms-shelves". Well if it will be useful to someone else.


image


Let's start "juggle with shelves" of the above design approach.


Single Responsibility Principle (SRP) sole responsibility principle


One piece of code should be changed only during the implementation of one goal. If a section of code implements two tasks and changes for different uses, then this section should be duplicated by instance for each purpose. This is very important because it requires a departure from the generally accepted principle of eliminating duplication.


The purpose of this principle is to eliminate implicitly inserted errors resulting from the fact that the following invariants exist in the development for a section of code, procedure, class, component (hereinafter referred to as combining these concepts):



Open-Closed Principle (OCP) principle of openness / closeness


Code development is optimally planned in such a way that a new programmer needs to add new code to implement a programmer, while the old code does not need to be changed. The code must be open (Open) to add and closed (Closed) to change.


The goal for this principle is to minimize labor costs and eliminate implicitly inserted errors resulting from the development of the following invariants:



Liskov Substitution Principle (LSP) Barbara Liskov substitution principle


This principle limits the use of the extension of the base interface [base] by the implementation, stating that each implementation of the base interface must have a behavior as the base interface. At the same time, the basic interface fixes the expected behavior at the places of its use. And the presence in the behavior of the implementation of the differences from the expected behavior, fixed by the basic interface, will lead to the possibility of violation of the invariant [2].


This principle is based and specifies the method of design, based on abstraction. In this approach, an abstraction is introduced — some basic properties and behavior characteristic of many situations are fixed. For example, [component-procedure] "Move to previous position" for situations: "Cursor in text", "Book on shelf", "Element in array", "Legs in dance", etc. And for this [component] are fixed ( often everyday experience and without formalization) some prerequisites and behavior, for example: "The presence of a moving object", "Repeat several times", "The presence of the order of elements", "The presence of fixed positions of elements". LSP requires that, when adding a new usage situation, all the prerequisites and limitations of the base are met for the [component]. And the situation “a grain in a sugar can” cannot be described by this abstraction, although the grain, of course, has a position, there are positions in which the grain has been previously, and it is possible to move it in them - there are no fixed positions of elements.


The goal for this principle is to eliminate implicitly inserted errors resulting from the development of the following invariants:



Interface Segregation Principle (ISP) interface separation principle; Dependency Inversion Principle (DIP) dependency inversion principle


These two principles are very similar in terms of their requirements. Both implicitly imply the utility of using the minimum possible basic interface as a tool for the interaction of two [components]: "client" and "server" - these names are chosen simply for identification. In this case, the general information used by [components] is concentrated in the basic interface. One [component] ("server") implements a base interface implementation, the other [component] ("client") refers to this implementation.


The goal for these principles is to minimize component dependencies, allowing independent changes to their code if it does not change the underlying interface. The independence of component changes reduces complexity and effort if the components fulfill the requirements of the SRP principle. A similar approach is possible because the following invariants exist in the development:



Returning to the title, I’ll explain why I’ve chosen not to understand. The denial is added in order to emphasize the errors suffered and very IMHO useful rule. It is better not to understand and therefore not to use technology than to misunderstand, to take for granted, to spend on the use of technology its resources and as a result not to receive any useful exhaust except complacency and the possibility of bragging about involvement in fashionable technology.


Thanks for attention.


Links



')

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


All Articles