2017-05-12 22 views
1

使用工厂模式,我有两个班返回对象和列表<object>在C#

public class LoginModel : IBaseInterface 
{ 
    public string EmailAddress { get; set; } 
    public string Password { get; set; } 
} 

public class UsersModel : IBaseInterface 
{ 
    public int UserID { get; set; } 
    public string UserName { get; set; } 
    public string EmailAddress { get; set; } 
    public int RoleID { get; set; } 
    public string RoleName { get; set; } 
    public int ProjectID { get; set; } 
    public string ProjectName { get; set; } 
    public int PMID { get; set; } 
    public string PMEmailAddress { get; set; } 
    public string PMEmailName { get; set; } 
    public int DMID { get; set; } 
    public string DMEmailAddress { get; set; } 
    public string DMEmailName { get; set; } 
} 

IBaseInterface是intergace,有没有什么定义。

public static IBaseInterface Create(ObjectsCollection Type) 
    { 
     switch (Type) 
     { 
      default: 
       return null; 
      case ObjectsCollection.UsersModel: 
       return new UsersModel(); 
      case ObjectsCollection.LoginModel: 
       return new LoginModel(); 
     } 
    } 

一切工作正常的对象。但是如何使用这个工厂返回List<UsersModel>List<LoginModel>

如果有人请帮我解决这个问题,会不错。

感谢

+0

只是增加ObjectsCollection是一个枚举: 公共枚举ObjectsCollection { UsersModel, LoginModel } – Anurag

+2

可能的话,你的问题是这不是使用工厂模式的好方法吗?也许你需要2个工厂,一个用于物品,一个用于收集物品?我怀疑你不应该使用任何一种,只是在你想要的地方新增你想要的东西。 – Neil

+0

我想这个工厂也返回列表类型的对象。 – Anurag

回答

0

测试这个...我希望它可以帮助

using System; 
using System.Collections.Generic; 

namespace ConsoleApplication1 
{ 
    internal class Program 
    { 
     private static void Main(string[] args) 
     { 
      var type1 = Create<LoginModel>().InternalList; 

      if (type1 != null) 
       Console.WriteLine("OK"); 

      Console.WriteLine("Finish"); 

      Console.ReadLine(); 
     } 

     public static T Create<T>() where T : AbstractFactory 
     { 
      switch (typeof(T).Name) 
      { 
       default: 
        return default(T); 

       case "UsersModel": 
        return new UsersModel() as T; 

       case "LoginModel": 
        return new LoginModel() as T; 
      } 
     } 
    } 

    internal abstract class AbstractFactory : IBaseInterface 
    { 
     public List<IBaseInterface> InternalList { get; set; } 
    } 

    internal interface IBaseInterface 
    { 
    } 

    internal class UsersModel : AbstractFactory 
    { 
     public UsersModel() 
     { 
      InternalList = new List<IBaseInterface>(); 
     } 

     public int UserID { get; set; } 
     public string UserName { get; set; } 
     public string EmailAddress { get; set; } 
     public int RoleID { get; set; } 
     public string RoleName { get; set; } 
     public int ProjectID { get; set; } 
     public string ProjectName { get; set; } 
     public int PMID { get; set; } 
     public string PMEmailAddress { get; set; } 
     public string PMEmailName { get; set; } 
     public int DMID { get; set; } 
     public string DMEmailAddress { get; set; } 
     public string DMEmailName { get; set; } 
    } 

    internal class LoginModel : AbstractFactory 
    { 
     public LoginModel() 
     { 
      InternalList = new List<IBaseInterface>(); 
     } 

     public string EmailAddress { get; set; } 
     public string Password { get; set; } 
    } 
}