using System;
namespace MyApp
{
class Program
{
static void Main()
{
ITarget target = new Adapter();
target.TheMethod();
Console.ReadKey();
}
}
class Adaptee
{
public void MyMethod()
{
Console.WriteLine("The method of Adaptee...");
}
}
interface ITarget
{
void TheMethod();
}
class Adapter : Adaptee,ITarget
{
public void TheMethod()
{
MyMethod();
}
}
}
本文通过C#代码示例展示了适配器模式的基本原理及应用。适配器模式允许不兼容接口的对象一起工作,通过将一个类的接口转换成客户希望的另一个接口,从而使原本因接口不兼容而不能一起工作的那些类可以一起工作。
2783

被折叠的 条评论
为什么被折叠?



