I tried irreputable's approach and tried "Inline Method" on the getResponse()
method, but it was inlining some duplicate code (which is why we extracted that duplicate code into getResponse()
in the first place). I should have clarified that in my question that there is more code in getResponse()
than what I showed.
As for making a Factory, I couldn't justify introducing more boilerplate and LOC into the class, especially since I have to pass a lot of data into the Factory Method and/or Inner Class.
What we did instead was, we wrapped the the anonymous inner classes with a method like so:
private TransformerStrategy createTransformerStrategyWithABCValues(Param1 param1, Param2 param2, IVar ivar, IVar1 ivar2) {
return new TransformerStrategy() {
public FooRequest transform(FooResponse response) {
return FooRequestTransformer.transform(
param1,
param2,
iVar1,
iVar2);
}
};
}
Now the calling method looks like this:
public List<Foo> getFooForSomeFlow1(Param1 param1, Param2 param2, ...){
FooResponse fooResponse = anotherService.baz(fooRequest);
TransformerStrategy myTransformerStrategy = createTransformerStrategyWithABCValues(param2, param2, iVar1, iVar2);
FooResponse fooResponse = getResponse(fooResponse, myTransformerStrategy);
...//other code
}
In the process of doing this, we found that the 5 strategies had some duplication, so we were able to get that down to 3 strategies. We got rid of the StrategyProvider class as well as the inner class that was provided.
With this approach it's now somewhat easier to debug/maintain, and we were able to cut quite a bit of duplication out of the code.