C#适配器模式实践一:委托

需求:

开发组早有一套类库,产品组也有对应的类,但是他们不想用开发组里面的函数名称,想按照产品组的规则来命名。

解决:通过委托写一个类来解决这个问题,也就是适配器模式

代码:

public class Development
{
    public string SampleFunction(string strName)
    {
        return "Hello,"+strName;
    }
}

delegate string MatchFunction(string strName);

public class Adapter
{
    public string ProductionFunction(string strName)
    {
        MatchFunction operation = new MatchFunction(new Development().SampleFunction);
        return operation(strName);
    }
}

public class Production : Adapter
{
   
}

调用代码为:

        Production ProductionObject = new Production();
        string strCongratulation = ProductionObject.ProductionFunction("Catvi");        

原文地址:https://www.cnblogs.com/catvi/p/1952963.html