Frage

If a class is dependent through interfaces on more than one other class is that a violation of SRP, or is it only a violation if both interfaces are expected to change, or is this the wrong line of thought altogether?

War es hilfreich?

Lösung

The SRP is in principle entirely orthogonal to what and how many dependencies the class might have.

A class can have a single responsibility, but if that responsibility is complex to carry out it may have many dependencies on "sub-contractors" that take care of the menial work. In essence, your single responsibility is "coordinate these people so that X happens".

Andere Tipps

Not necessarily. A class could still have one responsibility but have multiple dependencies. Having multiple dependencies is often a sign that a class is doing to much, but it's not proof of that.

If the class just call methods on those dependent interfaces, that wouldn't' be a violation of SRP since it's orchestrating a process calling different methods on different components, that would be the responsability.

It's hard to provide an exact answer without a specific context but in short: having dependencies with multiple interfaces it's not a violation of SRP per se.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top