Consider the following example -
i) A class Processor
- contains a method process()
which calls a Rest webservice given url
, JsonObject
(request object) and a class
object to which response will be parsed.
class Processor{
process(String URL,JsonObject requestObject,Class<?> responseClass(){
//Calls webservice and parses the response.
}
}
Now, i need to call this method from various places. So i do something like -
//Prepare data
//Call process with this data.
This is one way of doing it. We prepare data at various places and calls process method.
Now, another way of doing it can be to define an interface say RequestGenerator
(with a method generate()
) and modifying process(String,JsonObject,Class)
to process(RequestGenerator)
.
interface RequestGenerator {
Request generate();
}
//Request.java
class Request{
String URL;
JsonObject requestObject;
Class<?> responseClass;
}
//Processor.java
process(RequestGenerator rg){
Request req = rg.generate();
//Calling webservice and parsing.
}
Now, whenever i need to call process() method , i can provide my implementation of generate method to create Request Object.
My question is are there any merits of second method here. To me it seems more towards object world, but i am not able to understand if it is better?
Thanks