这是什么设计模式——适配器、提供者、委托、模板方法,还是……?

发布于 2024-12-09 19:18:10 字数 2601 浏览 1 评论 0原文

这是一个简单的设计模式问题:

作为我当前项目的一部分,我编写了一个接口,该接口执行数据库搜索(使用 Web 服务和相关客户端存根)并返回结果 - 随后将由 struts 操作用作对JSON 请求。

接口是这样的:

    public interface DynamicSearchProvider {

        JSONObject getSearchResultsAsJSONObject(DatatablesRequestParams params) 
                throws JSONException;

    }

然后对于每个特定类型的对象,将实现上述的具体版本,以便它将调用相关的Web服务并返回结果。

基本上,据我所知,它只是一堆业务逻辑的包装。

问题是,你会怎么称呼这个?我不喜欢“提供者”这个词,因为它相当含糊。是否有一个明确定义的设计模式?

理想情况下,我更愿意使用 Spring 顺便说一句,但不幸的是我不能在这个项目中,因为它是遗留代码库的一部分...

编辑:

这是它的使用位置:

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;

    }

等等...

所以,对于一个特定的对象类型,实现了上述 Action 类的具体版本(顺便说一句,它是 stuts action),并且它将引用上述“Provider”的实现......就像这样:

public class PolicyDynamicSearchAction extends GenericDynamicSearchAction {

    @Override
    public final DynamicSearchProvider getDynamicSearchProvider() {

        return new PolicyDynamicSearchProvider();

    }
}

希望

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
}
}

它能让它更清楚。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

病女 2024-12-16 19:18:10

从你的“一堆业务逻辑”猜测它是一个外观。不过,恕我直言,按照设计模式命名类通常不是一个好主意。首先,类可以同时实现多种设计模式,其次,这种方法在重构过程中很难维护。

我认为对于外观来说这是特别错误的想法,因为调用者不应该意识到该方法作为外观的事实。

Provider 听起来不错,但 DynamicSearchService 对我来说听起来更好。

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.

娇俏 2024-12-16 19:18:10

这不是我的专业领域,但我认为这听起来像策略模式

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文