适配器模式:将一个接口转换成客户希望的一个接口,使接口不兼容的那些类可以一起工作
interface IAdaptee
{
void SpecialRequest();
}
/// <summary>
/// 被适配者
/// </summary>
class Adaptee:IAdaptee
{
void IAdaptee.SpecialRequest()
{
SpecialRequest();
}
private void SpecialRequest()
{
Console.WriteLine("原有行为");
}
}
//客户需要接口
interface ITarget
{
void Requst();
}
//适配者
class Adaptor:ITarget
{
private IAdaptee adaptee;
public Adaptor(IAdaptee adaptee)
{
this.adaptee = adaptee;
}
public void Requst()
{
adaptee.SpecialRequest();
}
}
class Program
{
static void Main()
{
ITarget target = new Adaptor(new Adaptee());
target.Requst();
Console.Read();
}
}