1

我正在维护旧版 SDK。SDK 对于不同的平台会有不同的表现。它使用静态变量,并且需要在任何 API 调用之前创建静态变量,如下所示:

静态全局变量(旧代码)的示例:

SDKContext.init();
VideoManager manager = new VideoManager();
public void VideoManager#search() {
    SDKContext.search();
}

VideoManager 之类的组件无处不在。我的直觉告诉我应该改用局部变量,但不能确定。

局部变量(我的首选方式):

SDKContext context = new SDKContext();
// Or even using singleton
SDKContext context = SDKContext.getInstance();
VideoManager manager = new VideoManager(context);
public void VideoManager#search() {
     context.search();
}

这两种方法的优缺点是什么?欢迎任何建议/建议。

4

3 回答 3

0

您可以尝试一些 IoC 容器。我将为您制作“单件”,其中一些甚至具有“自动布线”功能。

于 2013-10-24T13:07:28.230 回答
0

I don't think you can call it a FACADE.

Facade is, compared to normal approach, simplified version of API, covering more complex operations underneath. In you case API of VideoManager#search() in both cases - the one you call not-facade, and the one that you claim to be a facade - is exactly the same. Moreover, method body is nearly the same too. Replacing global variable with object does not have anything common with being a facade, it's just a decision you can make during the implementation.

Global variable (or singleton) can simplify your code (in such case, where you need to pass it around to a lot of places, it can be worth of give it a try), at cost of harder testing (mocking) it - the decision is up to you.

于 2013-10-24T05:09:28.860 回答
0

静态使单元测试变得困难。

方法2要好得多。它对 IoC 友好且易于测试。

于 2013-10-24T08:41:16.577 回答