繁体   English   中英

通用对象的创建模式

[英]Creational pattern for generic objects

在以下情况下,有人可以提供最好的方法来返回具体实现的方法吗? 说我有:

public interface IThing<TInput> where TInput : RequestBase
{
    string Process(T input);
}

然后是多种实现:

public class Thing1<T> : IThing<T> where T : ReqThing1
public class Thing2<T> : IThing<T> where T : ReqThing2

在我的调用类中,包装这些类的构造并以一种干净,可测试的方式返回我想要的IThing的最佳方法是什么? 谢谢

我不太了解您想要什么,但是有一个主意:

public abstract class RequestBase
{
}

public class ReqThing1 : RequestBase
{
}

public class ReqThing2 : RequestBase
{
}

public interface IThing<T> where T : RequestBase
{
    string Process(T input);
}

public class Thing1 : IThing<ReqThing1>
{
    public string Process(ReqThing1 input)
    {
        throw new System.NotImplementedException();
    }
}

public class Thing2 : IThing<ReqThing2>
{
    public string Process(ReqThing2 input)
    {
        throw new System.NotImplementedException();
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        var thing1 = new Thing1();
        var thing2 = new Thing2();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM