Gurwinder Gurwinder - 2 months ago 11
Java Question

Designing Services with different types of parameters java

I am working on designing a common

interface for atleast 5 concrete implementations. Now all these services require different types of inputs. I thought of creating a
class to hold parameters, so that interface could remain common. but then, for some implementations, some of the fields will be unused. I also thought of using
to hold my params, but that is also not good(casts and if-elses everywhere). Also, I thought of doing was to create a class with Static methods
Service.responseAsPerFirstImplementation(p1, t1, i1)
and such. But, this way is not good coding. Please suggest how I should design between Modular design, flexibility vs variability of parameters?

Is below the good way of doing it?

public class Client {

public static void main(String[] args) {
System.out.println(Services.response(new UserParam(1, new Date())));
System.out.println(Services.response(new PatternParam("core")));



I think the core of the question is if your parameters come from some "generic", unstructured sourcelike HTTP request parameters or command line arguments or some structured source - I would put a Swing form there as you know in advance which UI elements you have.

In case of "generic" parameters you have to convert these parameters into something your services can process. You can do this manually or with some library/framework. For instance, you can use annotations to describe how your HTTP request and its parameters map to your controllers/services/methods:

public class TrainRunController {

    @RequestMapping(value = "/{year}/{month}/{day}/{trainNumber}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public TrainRun getTrainRun(@PathVariable Integer year, @PathVariable Integer month, @PathVariable Integer day,
                                @PathVariable String trainNumber) { ... }

Similarly with command-line parameters - you can use somethings like args4j to map CLI parameters into Java object first and then call your services appropriately.

Hope this helps.