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?

有帮助吗?

解决方案

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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top