Please deal with this basic question.
I have an abstract class C1
which extends another abstract class C0
and is extended by multiple sub-classes (C21 and C22)
.
@Component
public abstract class C0 {
protected abstract String getCaller();
//Some other methods.
}
.
public abstract class C1 extends C0 {
//Set of methods which are used by children and then calls methods of C0 which then use getCaller();
}
.
@Controller
public class C21 extends C1 {
@RequestMapping(value = "abc", method = RequestMethod.GET)
public String start(@RequestParam(value = "kw", required = true) String p1,
@RequestParam(value = Constant.REQUEST_PARAM_KEYWORDID, required = true) long p2) throws Exception {
//Some processing and calls controllers defined in abstract class C1
return "200";
}
@Override
protected String getCaller() {
return "C21";
}
}
.
@Controller
public class C22 extends C1 {
@RequestMapping(value = "abc", method = RequestMethod.GET)
public String start(@RequestParam(value = "kw", required = true) String p1,
@RequestParam(value = Constant.REQUEST_PARAM_KEYWORDID, required = true) long p2) throws Exception {
//Some processing and calls controllers defined in abstract class C1
return "200";
}
@Override
protected String getCaller() {
return "C22";
}
}
C0 contains an abstract method getCaller();
Callers of C21 and C22 are different but they can be identified by the parameter passed to only method start(p1,p2)
of these classes.
start(p1,p2)
does similar thing in both the classes. Only difference in C21 and C22 is the implementation of getCaller()
which is fixed and can anyways be extracted from parameters of start. So, I decided to create single class instead of C21 and C22.
I cannot create setCaller()
so, I wanted to create a final method
and a private variable caller
in abstract class C1
which could be populated with the parameters of start
method and returned in getCaller()
(which is called from abstract class C0
).
Is this the correct approach? Is there any better way or pattern for this?