C# 返回对象和列表<;对象>;从在c中使用工厂模式谈起#
我有两节课C# 返回对象和列表<;对象>;从在c中使用工厂模式谈起#,c#,design-patterns,factory-pattern,C#,Design Patterns,Factory Pattern,我有两节课 public class LoginModel : IBaseInterface { public string EmailAddress { get; set; } public string Password { get; set; } } 及 IBaseInterface是一个接口,没有任何定义 public static IBaseInterface Create(ObjectsCollection Type) { switch (Typ
public class LoginModel : IBaseInterface
{
public string EmailAddress { get; set; }
public string Password { get; set; }
}
及
IBaseInterface是一个接口,没有任何定义
public static IBaseInterface Create(ObjectsCollection Type)
{
switch (Type)
{
default:
return null;
case ObjectsCollection.UsersModel:
return new UsersModel();
case ObjectsCollection.LoginModel:
return new LoginModel();
}
}
对于对象,一切都很正常。但是如何使用此工厂返回列表
和列表
如果有人能帮我解决这个问题就好了
谢谢测试这个…我希望它能帮上忙
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; }
}
}
使用系统;
使用System.Collections.Generic;
命名空间控制台应用程序1
{
内部课程计划
{
私有静态void Main(字符串[]args)
{
var type1=Create().InternalList;
如果(类型1!=null)
控制台。写入线(“OK”);
控制台。写入线(“完成”);
Console.ReadLine();
}
公共静态T Create(),其中T:AbstractFactory
{
开关(类型(T).名称)
{
违约:
返回默认值(T);
案例“UsersModel”:
将新的UsersModel()返回为T;
案例“LoginModel”:
将新的LoginModel()返回为T;
}
}
}
内部抽象类AbstractFactory:IBaseInterface
{
公共列表内部列表{get;set;}
}
内部接口IBaseInterface
{
}
内部类UsersModel:AbstractFactory
{
公共用户模型()
{
InternalList=新列表();
}
public int UserID{get;set;}
公共字符串用户名{get;set;}
公共字符串电子邮件地址{get;set;}
public int RoleID{get;set;}
公共字符串RoleName{get;set;}
公共int ProjectID{get;set;}
公共字符串ProjectName{get;set;}
公共int PMID{get;set;}
公共字符串PMEmailAddress{get;set;}
公共字符串PMEmailName{get;set;}
公共int DMID{get;set;}
公共字符串DMEmailAddress{get;set;}
公共字符串DMEmailName{get;set;}
}
内部类LoginModel:AbstractFactory
{
公共登录模型()
{
InternalList=新列表();
}
公共字符串电子邮件地址{get;set;}
公共字符串密码{get;set;}
}
}
仅添加ObjectsCollection是一个枚举:公共枚举ObjectsCollection{UsersModel,LoginModel}可能,您的问题是这不是使用工厂模式的好方法?也许你需要两个工厂,一个用于项目,一个用于项目集合?我想你不应该实际使用这两种方法,只在你想要的地方使用你想要的新东西。我希望这个工厂也返回列表类型的对象。你应该像Neil建议的那样在工厂类中编写两个工厂方法。在一个方法中执行两种不同的操作是一个坏主意。关键是IBasisInterface
和List
没有任何共同之处,因此无法(也没有必要)从一个成员返回这两种方法。
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; }
}
}