我在看代理模式,对我而言,它似乎很像装饰器,适配器和桥模式.我误会了什么吗?有什么不同?为什么我会使用Proxy模式而不是其他模式?你过去在现实世界的项目中如何使用它们?
考虑以下代码:
public class MyClass()
{
public MyClass()
{
}
public DoSomething()
{
using (var service = new CustomerCreditServiceClient())
{
var creditLimit = service.GetCreditLimit(
customer.Firstname, customer.Surname, customer.DateOfBirth);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我们现在想重构它以松散地耦合它.我们最终得到这个:
public class MyClass()
{
private readonly ICustomerCreditService service;
public MyClass(ICustomerCreditService service)
{
this.service= service;
}
public DoSomething()
{
var creditLimit = service.GetCreditLimit(
customer.Firstname, customer.Surname, customer.DateOfBirth);
}
}
Run Code Online (Sandbox Code Playgroud)
看起来不错吧?现在任何实现都可以使用该接口,一切都很好.
如果我现在说实现是一个WCF类并且重构之前的using语句是有原因的,那该怎么办?ie /关闭WCF连接.
所以现在我们的接口必须实现一个Dispose方法调用,或者我们使用工厂接口来获取实现并在其周围放置一个using语句.
对我来说(尽管这个主题是新的),这似乎是一个漏洞的抽象.我们不得不在代码中添加方法调用,只是为了实现处理内容的方式.
有人可以帮助我理解这一点并确认我是对还是错.
谢谢
Can we Say facade and Adapter are more or less in design patterns ?
Run Code Online (Sandbox Code Playgroud)
维基百科对此的解释是:-
Adapter Converts one interface to another so that it matches what the client is
expecting while Facade Provides a simplified interface.
Run Code Online (Sandbox Code Playgroud)
查看 wiki http://en.wikipedia.org/wiki/Facade_pattern中的 UML 表示和适配器模式http://en.wikipedia.org/wiki/Adapter_pattern我无法区分它们之间的太多。有人可以向我解释两者的主要区别吗?