继问题 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/