您创建实例以调用静态方法的模式的名称
通常这样做是为了使遗留代码可测试。例如,可能有大量静态调用,例如
int importantNumber = DataAccess.LoadValue();
我创建一个可以实例化的类来调用这些调用,该类通常位于接口后面,例如
public int LoadValue(){
return DataAccess.LoadValue();
}
然后我可以使用 DI 或其他内容,并将原始调用替换为
int importantNumber = _dataAccessInstance.LoadValue();
Is There a name对于这个模式?我在想“适配器”,但它似乎比这更具体。
Usually done to make legacy code testable. For example, there might be a load of static calls like
int importantNumber = DataAccess.LoadValue();
and I create a class which can be instantiated to call these, which is normally behind an interface, like
public int LoadValue(){
return DataAccess.LoadValue();
}
Then I can use DI or whatever and replace the original call with
int importantNumber = _dataAccessInstance.LoadValue();
Is there a name for this pattern? I was thinking 'Adapter', but it seems more specific than that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代理人。
它看起来像包装器之一——代理、适配器或装饰器。装饰器并不真正适合,因为你没有增加任何价值;如果您要从一个接口映射到另一个接口,则适配器适合;我认为代理就是答案,因为您使用它来协调对工具包的访问。
如果您要简化对非常大的代码库中的工具子集的访问,则可能是外观。
Proxy.
It looks like one of the wrappers -- proxy, adapter or decorator. Decorator doesn't really fit as you're not adding any value; adapter fits if you're mapping from one interface to another; I think proxy is the answer, as you're using it to mediate access to the toolkit.
Could be facade if you're simplifying access to a subset of tools from a very large library of code.
这是一个代理。
适配器更改类的接口,使其更易于与其他类一起使用。
外观是创建单个接口,以便更轻松地与多个对象的接口进行交互。它改变了抽象级别。
装饰器不会更改接口,但会添加额外的功能
代理充当具有相同接口和相同最终功能的另一个对象的代理
It's a proxy.
An adapter changes the interface of a class to make it easier to use with other clases
A facade is the creation of a single interface to make interacting with the interfaces of several objects easier. It changes the level of abstraction.
A decorator does not change the interface but adds extra functionality
A proxy acts as a surrogate to another object with the same interface and same end functionality