适配器模式

适配器模式:将一个类的接口转换成客户希望的另一个接口,Adapter使原本由于接口不兼容而不能一起工作的那些类可以一起工作。

结构图:

客户可以对接的接口类:

    class Target
    {
        public virtual void Request()
        {
            Console.WriteLine("普通请求!");
        }
    }

 客户需要使用适配器才能使用的接口:

    class Adaptee
    {
        public void SpecialRequest()
        {
            Console.WriteLine("特殊请求!");
        }
    }

 适配器的定义:继承与Target类

    class Adapter : Target
    {
        Adaptee ad = new Adaptee();
        public override void Request()
        {
            ad.SpecialRequest();
        }
    }

 主函数的调用:

    class Program
    {
        static void Main(string[] args)
        {
            Target ta = new Target();
            ta.Request();

            Target sta = new Adapter();
            sta.Request();
            Console.ReadKey();
        }
    }
  原本不可以使用的接口,通过适配器之后可以使用了。
原文地址:https://www.cnblogs.com/lmfeng/p/2617966.html