java - 包装对静态方法/变量的访问的模式名称是什么?

标签 java design-patterns proxy wrapper

继问题 How do the Proxy, Decorator, Adapter, and Bridge Patterns differ? 之后,您如何描述我需要多次实现的以下模式?

场景是我从第三方类引用静态方法或变量,但我想将它隐藏在接口(interface)后面,以便我可以模拟它进行测试。

例如,在 Java 中,commons-lang 库有一个带有常量 IS_OS_WINDOWS 等的 SystemUtils 类。我想运行独立于底层操作系统并模拟各种操作系统的测试,所以我按如下方式包装对常量的访问:

interface ISystemUtils {
    boolean isOsWindows();
}

class SystemUtilsImpl implements ISystemUtils {
    @Override
    public boolean isOsWindows() {
        return SystemUtils.IS_OS_WINDOWS;
    }
}

这是代理、通用“包装器”还是其他东西?

最佳答案

这叫做 Facade :

A facade is an object that provides a simplified interface to a larger body of code, such as a class library. A facade can:

  • make a software library easier to use, understand and test, since the facade has convenient methods for common tasks;
  • make the library more readable, for the same reason
  • reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system
  • wrap a poorly designed collection of APIs with a single well-designed API.

关于java - 包装对静态方法/变量的访问的模式名称是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41161336/

相关文章:

proxy - Jenkins作为docker容器运行时无法访问互联网

c# - 当用户在代理后面时从客户端应用程序访问 WCF 服务

java - Android Gradle非法规则

java - 查找给定范围内的史密斯数

java - Selenium WebDriver : Verify Print Window dialog displayed on the page

javascript - 如何通过消除双重等待和 DRY 代码进行重构?

Java 方法无界类型或类返回

mysql - 记录录入——如何管理?

java - Java 中的 while 循环中条件语句不起作用?

github - 如何让 Ionic 在代理后面工作? (错误 : getaddrinfo ENOTFOUND github. com github.com:443)