C# 工厂设计模式通用返回类型
我想用一个通用的返回类型实现工厂设计模式。我已经创建了这个示例,但无法使其运行。我的工厂如何返回泛型类型,以及如何在主类中使用它。这是我的密码:C# 工厂设计模式通用返回类型,c#,generics,design-patterns,factory,C#,Generics,Design Patterns,Factory,我想用一个通用的返回类型实现工厂设计模式。我已经创建了这个示例,但无法使其运行。我的工厂如何返回泛型类型,以及如何在主类中使用它。这是我的密码: namespace ConsoleApplication1 { using System; using System.Collections.Generic; class MainApp { static void Main() { Factory fac = ne
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
class MainApp
{
static void Main()
{
Factory fac = new Factory();
IPeople<T> pep = fac.GetPeople(PeopleType.RURAL);
Console.WriteLine(pep.GetList());
}
}
public interface IPeople<T>
{
List<T> GetList();
}
public class Villagers : IPeople<DomainReturn1>
{
public List<DomainReturn1> GetList()
{
return new List<DomainReturn1>();
}
}
public class CityPeople : IPeople<DomainReturn2>
{
public List<DomainReturn2> GetList()
{
return new List<DomainReturn2>();
}
}
public enum PeopleType
{
RURAL,
URBAN
}
/// <summary>
/// Implementation of Factory - Used to create objects
/// </summary>
public class Factory
{
public IPeople<T> GetPeople(PeopleType type)
{
switch (type)
{
case PeopleType.RURAL:
return (IPeople<DomainReturn1>)new Villagers();
case PeopleType.URBAN:
return (IPeople<DomainReturn2>)new CityPeople();
default:
throw new Exception();
}
}
}
public class DomainReturn1
{
public int Prop1 { get; set; }
}
public class DomainReturn2
{
public int Prop2 { get; set; }
}
}
命名空间控制台应用程序1
{
使用制度;
使用System.Collections.Generic;
类MainApp
{
静态void Main()
{
工厂fac=新工厂();
IPeople pep=fac.GetPeople(PeopleType.RURAL);
Console.WriteLine(pep.GetList());
}
}
公共接口IPeople
{
List GetList();
}
公共阶层村民:IPeople
{
公共列表GetList()
{
返回新列表();
}
}
公共类城市人口:IPeople
{
公共列表GetList()
{
返回新列表();
}
}
公共枚举人员类型
{
农村,,
城市的
}
///
///工厂的实现-用于创建对象
///
公营工厂
{
公共IPeople GetPeople(PeopleType类型)
{
开关(类型)
{
案例PeopleType.RURAL:
返回(i人民)新村民();
案例PeopleType.URBAN:
返回(IPeople)新的CityPeople();
违约:
抛出新异常();
}
}
}
公共类域返回1
{
公共int Prop1{get;set;}
}
公共类域返回2
{
公共int Prop2{get;set;}
}
}
您不必将枚举
传递到工厂,可以使用t
类型参数选择合适的产品,如下所示:
public class Factory
{
public IPeople<T> GetPeople<T>()
{
if(typeof(T) == typeof(DomainReturn1))
return (IPeople<T>)new Villagers();
if(typeof(T) == typeof(DomainReturn2))
return (IPeople<T>)new CityPeople();
throw new Exception();
}
}
Factory factory = new Factory();
var product1 = factory.GetPeople<DomainReturn1>();
var product2 = factory.GetPeople<DomainReturn2>();
公共类工厂
{
公共互联网用户获取个人信息()
{
if(typeof(T)=typeof(DomainReturn1))
返回(i人民)新村民();
if(typeof(T)=typeof(DomainReturn2))
返回(IPeople)新的CityPeople();
抛出新异常();
}
}
您可以这样使用它:
public class Factory
{
public IPeople<T> GetPeople<T>()
{
if(typeof(T) == typeof(DomainReturn1))
return (IPeople<T>)new Villagers();
if(typeof(T) == typeof(DomainReturn2))
return (IPeople<T>)new CityPeople();
throw new Exception();
}
}
Factory factory = new Factory();
var product1 = factory.GetPeople<DomainReturn1>();
var product2 = factory.GetPeople<DomainReturn2>();
工厂=新工厂();
var product1=factory.GetPeople();
var product2=factory.GetPeople();
您不必将枚举
传递到工厂,可以使用t
类型参数选择合适的产品,如下所示:
public class Factory
{
public IPeople<T> GetPeople<T>()
{
if(typeof(T) == typeof(DomainReturn1))
return (IPeople<T>)new Villagers();
if(typeof(T) == typeof(DomainReturn2))
return (IPeople<T>)new CityPeople();
throw new Exception();
}
}
Factory factory = new Factory();
var product1 = factory.GetPeople<DomainReturn1>();
var product2 = factory.GetPeople<DomainReturn2>();
公共类工厂
{
公共互联网用户获取个人信息()
{
if(typeof(T)=typeof(DomainReturn1))
返回(i人民)新村民();
if(typeof(T)=typeof(DomainReturn2))
返回(IPeople)新的CityPeople();
抛出新异常();
}
}
您可以这样使用它:
public class Factory
{
public IPeople<T> GetPeople<T>()
{
if(typeof(T) == typeof(DomainReturn1))
return (IPeople<T>)new Villagers();
if(typeof(T) == typeof(DomainReturn2))
return (IPeople<T>)new CityPeople();
throw new Exception();
}
}
Factory factory = new Factory();
var product1 = factory.GetPeople<DomainReturn1>();
var product2 = factory.GetPeople<DomainReturn2>();
工厂=新工厂();
var product1=factory.GetPeople();
var product2=factory.GetPeople();
DomainReturn1和DomainReturn2有一个基类?为什么需要这个?你能说得更清楚些吗?您的案例不需要泛型hereDomainReturn1和DomainReturn2有一个基类?为什么需要这个?你能说得更清楚些吗?在这里,您的案例不需要泛型