Spring 继承:父类中的主方法,与子类中一些方法的特定实现有关
我需要实现两个类似的过程,这两个过程的逻辑基本相同,但一些参数/方法可能不同。我想知道将主逻辑提取到父类,并将几个方法的结果指定给子类,这是否是一种好的做法。比如:Spring 继承:父类中的主方法,与子类中一些方法的特定实现有关,spring,inheritance,template-method-pattern,Spring,Inheritance,Template Method Pattern,我需要实现两个类似的过程,这两个过程的逻辑基本相同,但一些参数/方法可能不同。我想知道将主逻辑提取到父类,并将几个方法的结果指定给子类,这是否是一种好的做法。比如: abstract class Parent{ protected CommonDao commonDao; protected String specStatus; protected abstract int getDbResult(); public Parent(CommonDao commonDao){ th
abstract class Parent{
protected CommonDao commonDao;
protected String specStatus;
protected abstract int getDbResult();
public Parent(CommonDao commonDao){
this.commonDao = commonDao;
}
public String mainLogic(){
if(commonMethod()){
//..
}
int specDbResult = getDbResult();
//some logic here
return specStatus;
}
private boolean commonMethod(){
//..
return true;
}
}
@Service
public Child1 extends Parent(){
@Autowired
public Child1(CommonDao commonDao){
super(commonDao);
super.specStatus = specStatus1;
}
@Override
protected String getDbResult(){
commonDao.getResult1();
}
}
@Service
public Child2 extends Parent(){
@Autowired
public Child2(CommonDao commonDao){
super(commonDao);
super.specStatus = specStatus2;
}
@Override
protected String getDbResult(){
commonDao.getResult2();
}
}
如果它看起来不是一个干净的代码,在这种情况下,您会推荐什么解决方案?
提前感谢使用依赖注入,组合显然是比继承更好的方法 dao作为一个属性对于父子关系来说是不够的 请确保您已经理解了有效Java的第16项,就像任何程序员一样 按要求编辑:
@Service
public class NotAChild{
@Autowired
CommonOps1 commonOps1;
@Autowired
CommonOps2 commonOps2;
private boolean commonMethod(){
int result1 = commonOps1.getDbResult();
int result2 = commonOps2.getDbResult();
....
return result1 + result2;
}
}
@Component
public class CommonOpS1{
@Autowired
CommonDao commonDao;
protected String getDbResult(){
commonDao.getResult1();
}
}
@Component
public class CommonOpS2{
@Autowired
CommonDao commonDao;
protected String getDbResult(){
commonDao.getResult2();
}
}
所有其他NotAChildN对象都可以使用CommonOps的通用方法
因此,您不会使用依赖注入来使用继承,组合显然是一种比继承更好的方法 dao作为一个属性对于父子关系来说是不够的 请确保您已经理解了有效Java的第16项,就像任何程序员一样 按要求编辑:
@Service
public class NotAChild{
@Autowired
CommonOps1 commonOps1;
@Autowired
CommonOps2 commonOps2;
private boolean commonMethod(){
int result1 = commonOps1.getDbResult();
int result2 = commonOps2.getDbResult();
....
return result1 + result2;
}
}
@Component
public class CommonOpS1{
@Autowired
CommonDao commonDao;
protected String getDbResult(){
commonDao.getResult1();
}
}
@Component
public class CommonOpS2{
@Autowired
CommonDao commonDao;
protected String getDbResult(){
commonDao.getResult2();
}
}
所有其他NotAChildN对象都可以使用CommonOps的通用方法
因此,您不会使用继承您可以研究它,谢谢您的评论。它看起来与我的代码类似,它也是一种算法,需要执行哪些步骤,但在我的代码中,大多数子类之间的逻辑应该相同(因此粘贴到父类)。区别在于基本上只有几个参数,并且调用另一个dao方法。你可以调查一下,谢谢你的评论。它看起来与我的代码类似,它也是一种算法,需要执行哪些步骤,但在我的代码中,大多数子类之间的逻辑应该相同(因此粘贴到父类)。区别在于基本上有几个参数,并且调用了另一个dao方法。您能详细说明一下吗?在这种情况下,我不知道如何重写我的代码,所以我假设commonMethod()是我的算法。但是,当有许多通用操作时,自动布线又如何呢?框架将创建您的组件。自动布线将为您提供服务。如果您有许多组件,则可以自动连接服务所需的组件。我增强了示例这并不是我想要的-我确实需要合并结果,我需要使用一次和另一次来使用第二次。模板方法在这里似乎是一个更好的解决方案,我认为Result1+result2在我看来就像是组合结果。为什么不呢?请你详细说明一下好吗?在这种情况下,我不知道如何重写我的代码,所以我假设commonMethod()是我的算法。但是,当有许多通用操作时,自动布线又如何呢?框架将创建您的组件。自动布线将为您提供服务。如果您有许多组件,则可以自动连接服务所需的组件。我增强了示例这并不是我想要的-我确实需要合并结果,我需要使用一次和另一次来使用第二次。模板方法在这里似乎是一个更好的解决方案,我认为Result1+result2在我看来就像是组合结果。为什么不是呢?