Lets imagine that we have a large data structure (lets call it Configuration
) and various client classes (lets call them Services
). Each service needs just one or two fields from Configuration
.
If we inject the whole Configuration
object to the various Service
s, does this violate the ISP?
Does the fact that Configuration
is technically a data structure and not a literal Java interface change the fundamental point behind ISP?
The practical dilemma I am facing is how configuration should become available to the various parts of my system. I have the option to use Spring’s @Value
annotation, through which each component gets precisely what it needs and nothing more. My second option is to make a Configuration
instance available as a whole. In the second case, every component of the system would get the configuration of the whole system instead of getting only the few parts that it actually needs.
I am trying to understand whether the second option violates ISP.
Advertisement
Answer
Strictly according to Bob Martin, yes. His example of applying ISP to a stack
certainly seems to imply that data structures are included.
On the other hand, this question is often asked of the Dependency Inversion Principle, e.g. here, here, and here. As I have answered in regard to the DIP, I don’t think it makes sense to apply it to a data structure. Your mileage may vary when applying object-oriented principles to procedural-programming constructs.