I have the following code and architecture (in fact this is a very simplified version) consting of a abstract and a concrete class.
public abstract class AbstractProcessor {
public void updateDataFromUrl(String url) {
//Download url
//Decrypt, and do a lot of stuff,
String data = "abc"; //Result from downloading
String processed = processData(data);
//Do a lot of other things with the transformed data
}
public abstract String processData(String data);
}
final class ConcreteProcessor extends AbstractProcessor {
public void updateData(int year, int month, int day) {
String url = String.format("http://%d.%d.%d", year, month, day);
updateDataFromUrl(url);
}
@Override
public String processData(String data) {
//Process the data
//---------------
//PROBLEM:
//----------------
//Need access to year, month, day ....
return null;
}
}
The updateDataFromUrl
method contains a lot of code (booth, before and after the processData
call) which I want to reuse in several Processors, for this reason I put the code into a abstract class.
The problem is: I want to access the data which was provided to the newly added updateData
method (here year
, month
, day
). As the call flows through the abstract class which does not know of these parameters, this information gets lost. How can I modify this architecture to keep this information?
The following solutions came to my mind, all with obvious drawbacks:
- Provide the abstract method with the url and extract the parameters from there again. (Problem: What is there are parameters which I only need in the
procecssData
method and never in the url?) - Split the
updateDataFromUrl
method into two two methods (the part before theprocessData
call and the part afterwards). Now use these methods directly in the ConcreteProcessor. (Problem: TheupdateDataFromUrl
method has a lot of context which I need both, before and after theprocessData
call. How can I get this data transfered between the newly created methods?)