相关对象的对象初始化策略
我正在工作的场景是下一个:有一个基础(非抽象)类,其中有几个从中继承的类。 基于我必须创建一个或另一个类型,我的初始代码是这样的:
switch (type)
{
case BaseType.Standard:
_list.Add(Standard.Create(<params>)));
break;
case BaseType.Special:
_list.Add(Special.Create(<params>));
break;
}
开关语句简化了。基类的每个专业化都有其自身的初始化参数,这意味着客户类类具有很多参数,可以涵盖所有可能的参数,以从子类(或基类本身)初始化每个对象。 我想知道是否有人可以帮助我找到一种更好的方法来初始化每个班级(无论是孩子还是基础)。我在思考类似抽象的工厂设计模式之类的东西,但是我不确定它是否适合我的用例。
我不想每种需要初始化的每种类型或具有巨大参数列表的方法开始具有巨大的开关语句。
最小可重复的方案:
public class Client
{
public Client(int clientProperty)
{
ClientProperty = clientProperty;
}
public int ClientProperty { get; set; }
public IEnumerable<BaseType> BaseTypes { get; set; } = new List<BaseType>();
public BaseType CreateBase(BaseTypeEnum type, string paramA, int paramB, int paramC, string paramD, string paramE, int paramF)
{
switch (type)
{
case BaseTypeEnum.Base:
return new BaseType(paramA);
case BaseTypeEnum.Colored:
return new ColoredBaseType(paramA, paramB, paramC);
case BaseTypeEnum.Special:
return new SpecialBaseType(paramA, paramD, paramE, paramF);
default:
return new BaseType(paramA);
}
}
}
public enum BaseTypeEnum
{
Base,
Colored,
Special
}
public class ColoredBaseType : BaseType
{
public ColoredBaseType(string paramA, int paramB, int paramC)
: base(paramA)
{
ParamB = paramB;
ParamC = paramC;
}
public int ParamB { get; set; }
public int ParamC { get; set; }
}
public class SpecialBaseType : BaseType
{
public SpecialBaseType(string paramA, string paramD, string paramE, int paramF) : base(paramA)
{
ParamD = paramD;
ParamE = paramE;
ParamF = paramF;
}
public string ParamD { get; set; }
public string ParamE { get; set; }
public int ParamF { get; set; }
}
The scenario I'm working is the next: There's a Base (non-abstract) class that has several classes that inherit from it.
Based on a type I have to create one or another, my initial code is like this:
switch (type)
{
case BaseType.Standard:
_list.Add(Standard.Create(<params>)));
break;
case BaseType.Special:
_list.Add(Special.Create(<params>));
break;
}
Switch statement was simplified. Each specialization of the base class, has it's own initialization params, meaning that the client class is having a lot of params to cover for all the possible params to initialize each object from the child classes (or the base class itself).
I was wondering if someone could help me to figure a better way of initializing each of the classes (both childs and base). I was thinking in something like Abstract Factory design pattern, but I'm not so sure if it fits my use case.
I don't want to start having a huge switch statement per each type I need to initialize nor a method with a huge parameter list.
Minimal reproducible scenario:
public class Client
{
public Client(int clientProperty)
{
ClientProperty = clientProperty;
}
public int ClientProperty { get; set; }
public IEnumerable<BaseType> BaseTypes { get; set; } = new List<BaseType>();
public BaseType CreateBase(BaseTypeEnum type, string paramA, int paramB, int paramC, string paramD, string paramE, int paramF)
{
switch (type)
{
case BaseTypeEnum.Base:
return new BaseType(paramA);
case BaseTypeEnum.Colored:
return new ColoredBaseType(paramA, paramB, paramC);
case BaseTypeEnum.Special:
return new SpecialBaseType(paramA, paramD, paramE, paramF);
default:
return new BaseType(paramA);
}
}
}
public enum BaseTypeEnum
{
Base,
Colored,
Special
}
public class ColoredBaseType : BaseType
{
public ColoredBaseType(string paramA, int paramB, int paramC)
: base(paramA)
{
ParamB = paramB;
ParamC = paramC;
}
public int ParamB { get; set; }
public int ParamC { get; set; }
}
public class SpecialBaseType : BaseType
{
public SpecialBaseType(string paramA, string paramD, string paramE, int paramF) : base(paramA)
{
ParamD = paramD;
ParamE = paramE;
ParamF = paramF;
}
public string ParamD { get; set; }
public string ParamE { get; set; }
public int ParamF { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论