1

Topic: Question about Single Responsibility Principle

Greetings! We admit in one class two groups of methods are implemented - each group of methods concerns separate functionality. If one developer changed something in one group of methods it is necessary to recompile all class. It could be avoided if those two groups of methods have been spaced apart on different classes. I.e. Single Responsibility Principle allows to reduce compilation dependencies. The favor also consists In it from Single Responsibility Principle? Or still any favor is?

2

Re: Question about Single Responsibility Principle

Hello, Maxim Rogozhin, you wrote: > the favor also consists In it from Single Responsibility Principle? Or still any favor is? No, the main sense that so is more clear when the class is responsible for something one, can then pick up it more exact title, and at its reading in a context of specific functionality not to distract on the insignificant task for this functionality. Also allows to avoid appearance of superfluous dependence between multifunctions.

3

Re: Question about Single Responsibility Principle

Hello, Alexander G, you wrote: AG> Also allows to avoid appearance of superfluous dependence between multifunctions. Thanks, but could not you more in detail here this moment explain? If someone wants to use any functionality it it and from other class takes.

4

Re: Question about Single Responsibility Principle

Hello, Maxim Rogozhin, you wrote: > If someone wants to use any functionality it it and from other class takes. It will be purposely and explicitly.

5

Re: Question about Single Responsibility Principle

SRP it is a practical principle of reduction of connectivity generally, not only in compile time. Connectivity in the project should be reduced for reduction of an amount of ghost effects, increase of stability to changes in the code. Accordingly, SRP helps to solve both tasks. An example from runtime: we admit at your class is not only two groups of methods, but also any state corresponding to each of groups (if we speak about OOP, instead of about procedural programming most likely so it and is). At first, you can have explicit ghost effects when one of groups changes a state of the second group. Secondly, you can have the implicit ghost effects connected to life cycle of object. Uniting two different states in one object, you rigidly connect them  with each other, from the moment of creation till the moment of clearing of storage that can be bad or even it is very bad for your architecture (memory leak, leak of personal data and passwords through long-living and, terrifying,  objects, etc.) . The example which has been not connected to OOP generally: let you have a method calculateAndPrint (Model m) which calculates, and deduces result in the console. Obviously, it unites at once two roles - computing and information output in the console. If you want for choice the user to write down result instead of the console in a DB it is necessary or to write a new method, or to break old into two according to SRP. An example from Java c a pattern singleton. Why a class containing a static field with a unique copy (self-contained singleton), unconditional harm and an antipattern? Yes it is equal for the same reason: breaks SRP, combining at once two roles - the main business role and a role connected to initialization, storage and access provision to the single copy. This violation leads to difficulties with writing of tests and to the considerable lowering of flexibility of the code. It is corrected it is trivial class-factory introduction, for example, from DI-frejmvorka type Spring or Guice.

6

Re: Question about Single Responsibility Principle

Hello, Baudolino, you wrote: B> SRP it is a practical principle of reduction of connectivity generally, not only in compile time. Connectivity in the project it is necessary to reduce for) decreases of an amount of ghost effects, increases of stability to changes in the code. Accordingly, SRP helps to solve both tasks. B> an example from runtime B> the Example which has been not connected to OOP generally B> the Example from Java c by a pattern singleton. Many thanks! Still the question - instead of whether is Interface Segregation Principle simply frequent case SRP? I.e. ISP it is simple "SRP for interfaces"?

7

Re: Question about Single Responsibility Principle

Hello, Maxim Rogozhin, you wrote: > Many thanks! Still the question - instead of whether is Interface Segregation Principle simply frequent case SRP? I.e. ISP it is simple "SRP for interfaces"? Both principles solve one task, but from the various points of view: I would not name their communication by a special case, is faster at them there is a general basic idea. ISP can be broken, even if SRP it is observed. For example, you implement algorithm of sorting and as a function parameter expect an array of user accounts. Generally possibility to compare objects is necessary to algorithm only, it identifiers and names  do not interest: it would be logical to generalize it, selecting interface Comparable or allowing to transfer in addition function  objects in a collection. Accordingly reorganization of the code from simple class Account to Account implements Comparable <Account> it also is a step aside ISP.

8

Re: Question about Single Responsibility Principle

Hello, Maxim Rogozhin, you wrote: > It is admissible in one class two groups of methods are implemented - each group of methods concerns separate functionality. It is possible to make separate interfaces for each group of methods. The class will inherit these interfaces and to implement. Clients of a class work with it through interfaces.?

9

Re: Question about Single Responsibility Principle

Hello, Maxim Rogozhin, you wrote: > the favor also consists In it from Single Responsibility Principle? Or still any favor is? What for generally division of the program into classes, functions, methods, etc. is necessary? Why not to write the program one big piece? The answer consists that to the person so easier to understand the program. To the person, convenience of the compiler, , the processor and an operating system in this question for the second time. And so, everyones Principle, their favor consists that they structure thought of the programmer, and do the program of more clear. It is more for what they are not necessary.