What design pattern is this - Adapter, Provider, Delegate, Template Methods, or ...?

3.5k views Asked by At

Here's a simple design patterns question:

As part of my current project, I have written an interface that performs a database search (using webservices and relevant client stubs) and returns the results - which will be used subsequently by a struts action as a response to a JSON request.

The interface is something like this:

    public interface DynamicSearchProvider {

        JSONObject getSearchResultsAsJSONObject(DatatablesRequestParams params) 
                throws JSONException;

    }

and then for each specific type of object, a concrete version of the above will be implemented so that it will call the relevant web services and returns a result.

Basically, it's just wrapper around a bunch of business logic as far as I can tell.

The question is, what would you call this? I don't like the term provider as it's quite ambiguous. Is there a well defined design pattern for this?

Ideally I would have preferred to use Spring with this by the way but unfortunately I can't in this project as it's part of a legacy code base...

EDIT:

Here's where it gets used:

public abstract class GenericDynamicSearchAction extends GenericAction {

    private static Log log = LogFactory.getLog(GenericDynamicSearchAction.class);

    /**
     * Method to be implemented by each individual search action
     */
    public abstract DynamicSearchProvider getDynamicSearchProvider();

    public final ActionForward executeAuthenticated(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException {

        log.debug("called");

        DatatablesRequestParams datatablesRequestParams = DatatablesUtils.extractDatatablesParamsFromRequest(request);

        try {


            JSONObject jsonResponse = getDynamicSearchProvider().getSearchResultsAsJSONObject(datatablesRequestParams);

            String echo = datatablesRequestParams.getEcho();

            jsonResponse.put(DatatablesUtils.ECHO_FIELD_NAME, echo);
            response.setContentType("application/json");

            String jsonResponseString = jsonResponse.toString();

            log.debug("Returning JSON response:"+jsonResponseString);

            response.getWriter().print(jsonResponseString);

        } catch (JSONException e) {

            response.setContentType("text/html");
            response.getWriter().print(e.getMessage());

        }

        return null;

    }

etc...

So, for a specific type of object, a concrete version of the above Action class (it's stuts action by the way) is implemented, and it will have a reference to an implementation of the above "Provider"... like this:

public class PolicyDynamicSearchAction extends GenericDynamicSearchAction {

    @Override
    public final DynamicSearchProvider getDynamicSearchProvider() {

        return new PolicyDynamicSearchProvider();

    }
}

and

public class PolicyDynamicSearchProvider implements DynamicSearchProvider {

    public final JSONObject getSearchResultsAsJSONObject(DatatablesRequestParams params) throws JSONException {
//some business logic that goes to webservice etc to get the info
}
}

Hope it makes it clearer.

2

There are 2 answers

7
Rostislav Matl On BEST ANSWER

Guessing from you "bunch of business logic" it is a Facade. Still, IMHO naming classes after the design pattern is not a good idea generally. First, class can implement several design patterns at once, second this approach is hard to maintain during refactorings.

I think it is especially wrong idea with the facade, as it the caller should not be aware of the fact that the method works as facade.

Provider sounds good, but DynamicSearchService sounds better to me.

2
danseagrave On

This isn't my area of expertise but I think it sounds like the Strategy pattern