标签:style blog http io ar 使用 sp on 2014
上一次谈设计模式,我谈到了装饰者模式,今天我要谈与之很相似的另一个结构型的设计模式:适配器模式。最后还会结合外观模式进行适当点评
namespace Adapter
{
public class Adaptee
{
public void AdapteeOperation()
{
Console.WriteLine("Adaptee Operation");
}
}
}
namespace Adapter
{
public interface Target
{
void UserOperation();
}
}
namespace Adapter
{
public class Adapter : Target
{
private Adaptee _adaptee = new Adaptee();
public void UserOperation()
{
this._adaptee.AdapteeOperation();
}
}
}
namespace Adapter
{
class Program
{
static void Main(string[] args)
{
Target target = new Adapter();
target.UserOperation();
Console.ReadKey();
}
}
}
可参见ADO.NET中的抽象DataAdapter以及具体的SqlDataAdapter、OracleDataAdapter的设计
| 共同点 | 不同点 | |
| 装饰者模式 | 对象包装 | 不改变接口,加入新的职责 |
| 适配器模式 | 不改变职责,改变接口 | |
| 外观模式 | 简化高层接口,统一调用 |
标签:style blog http io ar 使用 sp on 2014
原文地址:http://www.cnblogs.com/fecktty2013/p/designpatterns-adapter.html