Z
Z
zlodiak2019-03-16 19:33:21
Python
zlodiak, 2019-03-16 19:33:21

Does composition violate the Single Responsibility Principle from solid?

Please help me figure out whether the presence of composition in the class violates the principle of a single responsibility from solid. The fact is that when composition is used, then in fact the object is extended by the functionality of another object. If he uses it (and he certainly will, otherwise why did he connect), then it turns out that he is responsible not only for himself, but also for another object.
I'll try to explain the above with the help of code examples:
Here is the code that clearly violates the principle because the AreaCalc instance is responsible for both calculating the amount and displaying it.
Here is the code that does not violate the principle, because the AreaCalc instance is responsible for calculating the amount, and the AreaOutput instance is responsible for the output
Here is the code, which also uses AreaCalc to calculate the sum, and AreaOutput to display. BUT with composition, AreaOutput is built into AreaCalc
Is it true that composition violates the Single Responsibility Principle from solid?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-03-16
@zlodiak

No, it doesn't. Composition creates dependency, not responsibility. If the dependent class respects D , and the dependency class respects I and L , then everything is fine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question