I have three major classes, lets say A, B and C.
A instantiates both B and C. B also depends on C. B accesses C’s object through setter/getter methods. This is fine.
My problem is that A’s implementation is tightly coupled with B’s and C’s implementation.
I want to decouple this.
I don’t want to use the spring framework. Is there any other way? I thought of creating factories for their(B,C) initialization s but that means whenever A or C will need to access B they have to create a new instance of the Factory again.. this doesn’t seem right.
How should I solve this?
I am not sure if the IOC container is useful here?
I have three major classes, lets say A, B and C. A instantiates both
Share
Ashould not directly instantiateBorC. It should instead accept instances of them as constructor parameters:This decouples
Afrom the details of instantiatingBs andCs, by injecting it with those dependencies (thus, dependency injection).To decouple
Afrom the implementation, you’ll want to create interfaces for the functionality ofBandCthatAcares about. Say,BableandCable. ThenAcan accept as its constructor parameters aBableand aCable, and doesn’t care what class happens to implement those interfaces, or how the implementation works—just that it conforms to the contracts laid out byBableandCable.Here is a fully fleshed-out example of what I am talking about: https://gist.github.com/2402514