Hello,
I've got 2 trees of classes (I'll show only one level here):
B1 extends A1
and
B2 extends A2
1's classes need 2's classes to do their work. Can I do the following:
class A1 {
private A2 dependency;
public A2 getDependency() {
return this.dependency;
}
public void setDependency(A2 dependency) {
this.dependency=dependency;
}
}
class B1 {
public void someMethod() {
((B2) getDependency()).doJob(); //***
}
}
Dependency injection:
<bean name="b1" class="B1">
<property name="dependency">
<bean class="B2"> <!-- *** -->
<property>
<bean>
Is it safe to do class cast in the line marked with "***"? What is the best practice in this case?
The thing is that I want to use OOP in my Service and DAO classes, but I'm not sure what is the best way to provide all Services with their dependencies (DAOs) without providing each Service subclass with DAO of concrete type and geters/seters for that DAO.
Thank you.


Reply With Quote