0

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?

instanceOfObject
  • 2,936
  • 5
  • 49
  • 85

1 Answers1

1

Only difference in C21 and C22 is the implementation of getCaller() which is fixed and can anyways be extracted from parameters of start.

Since you have to change only a single method, you will be better off with a single non-abstract implementation C1 instead of an abstract C1 and a pair of C21 and C22. You can do something like this:

Caller caller;
public start(SomeType1 p1, SomeType2 p2, SomeType3 p3) {
    caller = new Caller(p1, p2, p3);
    ...
    // Do other stuff
}

public Caller getCaller() {
    return caller;
}

In case there are other classes in addition to C21 and C22 inheriting C1, you may want to keep your C1 abstract, and add a non-abstract C2 with an above implementation.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523