fordeka
fordeka

Reputation: 979

Is the single responsibility principle directly related to class' dependencies?

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?

Upvotes: 2

Views: 219

Answers (3)

Claudio Redi
Claudio Redi

Reputation: 68400

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.

Upvotes: 0

D Stanley
D Stanley

Reputation: 152521

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.

Upvotes: 0

Jon
Jon

Reputation: 437336

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".

Upvotes: 6

Related Questions