0

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

Ouney
  • 1,164
  • 1
  • 10
  • 22
  • 1
    It.. depends. Honestly - it isn't quite clear what you're asking. If you're 100% sure you will never need a direct request specification and you will every time know how to generate a request - go for the second option since it allows the user of your request processor do the job without knowing about the request itself and it's generation - so that is "the simpler - the better" – Alma Do Sep 23 '16 at 13:44
  • I think the best answer you are going to get is "it depends" as given by Almo Do. Also I think this question is better suited for http://programmers.stackexchange.com/ as it is a conceptual question – Balkrishna Rawool Sep 23 '16 at 14:02
  • 1
    @BalkrishnaRawool when referring other sites, it is often helpful to point that [cross-posting is frowned upon](http://meta.stackexchange.com/tags/cross-posting/info) – gnat Sep 23 '16 at 14:05
  • @gnat: true. I wished to say move the question from here to Programmers Community. – Balkrishna Rawool Sep 23 '16 at 14:07
  • Generics maybe! – ddarellis Sep 23 '16 at 15:07

0 Answers0