Reputation: 35
Im trying to code an apps in java and i would like to know something about the injections.
I have a class A that inherits from JFrame :
public class A extends JFrame {
public A(CModel model){
B classB = new B(model);
}
}
a class B that inherits from JPanel :
public class B extends JPanel {
public CModel model;
public B(CModel model){
this.model = model;
}
}
For injecting my Model into my B class, i have to inject it into my A class. Can i do that or is it considered bad practice ? Do i have other choices?
Thanks in advance
Upvotes: 1
Views: 200
Reputation: 5829
In your design, class B requires a CModel (good practice is to make model 'final').
Class A encapsulates B; so while you could first construct B, and inject B into A, there is no good reason to do this. The user who creates A does not need to know about B.
Since B requires a CModel, it follows that A requires a CModel.
The way you have done it best describes A because the only 'state' that is required to construct A is the state required by B.
Upvotes: 1