适配器模式

适配器模式:将一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。

场景:你有动机修改一个已经投产中的接口时,适配器模式可能是最适合你的模式。

UML图:

示例代码:

    public interface ITarget
    {
        void Request();
    }
    public class Source
    {
        public void OtherRequest()
        {
            Console.WriteLine("逻辑处理");
        }
    }
    public class Adapter:Source, ITarget
    {
        public void Request()
        {
            this.OtherRequest();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            LuoJi(new Adapter());//如果存在SourceA,SourceB,处理方式类似
        }

        static void LuoJi(ITarget target)
        {
            target.Request();
        }
    }
原文地址:https://www.cnblogs.com/chenyishi/p/9109558.html