I need to implement two similar processes which basicaly do the same logic but a few parameters/methods may differ. I'm wondering if it would be a good practice to extract the main logic to a parent class and specify a result of a few methods to child classes. Something like:
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();
}
}
If it doesn't seem to be a clean code, what solution would you recommend in such case? Thanks in advance