Asp.net 如何将Factory模式与接口一起使用

Asp.net 如何将Factory模式与接口一起使用,asp.net,interface,factory-pattern,Asp.net,Interface,Factory Pattern,我有以下界面: public interface IPlateSubCategory<T> { T GetItem(int plateID); } public class Thermo : IPlateSubCategory<ThermoItem> { public ThermoItem GetItem(int plateID) { // code that implements GetItem } } public class Thic

我有以下界面:

public interface IPlateSubCategory<T> {
   T GetItem(int plateID);
}
public class Thermo : IPlateSubCategory<ThermoItem> {
   public ThermoItem GetItem(int plateID) {
      // code that implements GetItem
   }
}
public class Thickness : IPlateSubCategory<ThicknessItem> {
   public ThicknessItem GetItem(int plateID) {
      // code that implements GetItem
   }
}
public class Density : IPlateSubCategory<DensityItem> {
   public DensityItemGetItem(int plateID) {
      // code that implements GetItem
   }
}

任何帮助都将不胜感激。谢谢

您已将
IPlateSubCategory
定义为泛型。工厂方法需要返回此接口的实现。如果在*项目类上放置支持接口,例如:

public class ThermoItem : IItem
{
}
然后,您可以将工厂更改为如下所示:

    public static IPlateSubCategory<IItem> GetPlateSubCategory(Categories cat)
    {
        switch (cat)
        {
            case Categories.Thermo:
                return new Thermo();
            case Categories.Thickness:
                return new Thickness();
            case Categories.Density:
                return new Density();
            default:
                throw new ArgumentOutOfRangeException("cat");
        }
    }
publicstaticiplatesubcategory GetPlateSubCategory(Categories cat)
{
开关(cat)
{
案件类别:
返回新的Thermo();
外壳类别。厚度:
返回新厚度();
病例类别:密度:
返回新密度();
违约:
抛出新ArgumentOutOfRangeException(“cat”);
}
}

需要为方法或类定义不会编译IItem的,并且在其中定义它将是工厂编写方式的一个问题,因为这样我们就不需要categories变量,因为调用代码会知道类型。您的设计中是否有IPlaceSubCategory接口不是泛型的?“只是想知道。”罗伯托-不,我不知道。事实上,这就是我遇到问题的地方。我不知道如何创建一个可以使用通用接口的工厂。
    public static IPlateSubCategory<IItem> GetPlateSubCategory(Categories cat)
    {
        switch (cat)
        {
            case Categories.Thermo:
                return new Thermo();
            case Categories.Thickness:
                return new Thickness();
            case Categories.Density:
                return new Density();
            default:
                throw new ArgumentOutOfRangeException("cat");
        }
    }