Utility vs. Composition vs. Inheritance for JAX-RS Response

358 views Asked by At

I am thinking about writing a utility class that creates and returns a JAX-RS Response. The goal is to simplify and standardize the generation of Response's. Here's the idea:

public Object success (Class targetClass, String methodName, Object responseEntity) {

    URI location = UriBuilder.fromResource(targetClass).path(targetClass, methodName).build(localVar1, localVar2);

    LogInfo(location, methodName); //log4J stuff

    return Response.created(location).entity(responseEntity).build();
}

And there would be additional methods for other response types (e.g. canceled, error, etc).

I was curious about how someone else may solve this design problem, perhaps using inheritance or composition.

How have to solved this in the past? Did you create a single utility class as well, or did you use inheritance or composition to design the solution? Why did you go with that design?

1

There are 1 answers

0
joelittlejohn On

This looks like a low-level utility to me. I say don't over think it - creating some kind of bespoke response inheritance hierarchy and or new family of composed types (for the sake of added 'patterniness') feels unnecessary.

I'd create a simple response utility, extract a private method for the commons parts, and rethink this simple design if (and only if) your requirements become more complex.