[英]How can I optimize the structure of my classes
我正在努力定义我的类的结构
我创建了一个ObjMetaClass
类的对象列表。 其中包含一些关于其对象points
元信息( points
: ObjMetaClass
类的一个属性)。
但是points
可以有两种类型,一种是TwoPointsPattern
类,另一种是ThreePointsPattern
类
我担心的是,每当我想访问任何项目时,我首先需要检查哪个项目存储在当前项目中以进行转换(是TwoPointsPattern
还是ThreePointsPattern
)
List<ObjMetaClass> objList = new List<ObjMetaClass>();
// some items
objList.Add(new ObjMetaClass(new TwoPointsPattern(...), isTwoPointsPattern:true, rate:124));
objList.Add(new ObjMetaClass(new ThreePointsPattern(...), isTwoPointsPattern:false, rate:654));
// access items from list
for (int i = 0; i < objList.Count; i++)
{
// check for casting object
if(objList[i].isTwoPointsPattern)
TwoPointsPattern temp = objList[i].points as TwoPointsPattern;
/* some logic or function call */
else
ThreePointsPattern temp = objList[i].points as ThreePointsPattern;
/* some logic or function call */
}
有没有改进或避免更好的办法if
检查? 任何建议,请
类的结构
对象元类
public class ObjMetaClass
{
public object points { get; internal set; }
public bool isTwoPointsPattern { get; internal set; }
internal int rate { get; set; }
public ObjMetaClass(object points, bool isTwoPointsPattern, int rate)
{
// check expected type of points object
if (points.GetType() != typeof(TwoPointsPattern) &&
points.GetType() != typeof(ThreePointsPattern))
throw new ArgumentException("Expected types TwoPointsPattern and ThreePointsPattern");
this.points = points;
this.isTwoPointsPattern = isTwoPointsPattern;
this.rate = rate;
}
}
两点模式和三点模式
public class TwoPointsPattern
{
public double FirstDate { get; internal set; }
public double FirstPrice { get; internal set; }
public double SecondDate { get; internal set; }
public double SecondPrice { get; internal set; }
public TwoPointsPattern(double FirstDate, double FirstPrice, double SecondDate, double SecondPrice)
{
this.FirstDate = FirstDate; this.FirstPrice = FirstPrice;
this.SecondDate = SecondDate; this.SecondPrice = SecondPrice;
}
}
public class ThreePointsPattern
{
public double FirstDate { get; internal set; }
public double FirstPrice { get; internal set; }
public double SecondDate { get; internal set; }
public double SecondPrice { get; internal set; }
public double ThirdDate { get; internal set; }
public double ThirdPrice { get; internal set; }
public ThreePointsPattern(double FirstDate, double FirstPrice, double SecondDate, double SecondPrice,
double ThirdDate, double ThirdPrice)
{
this.FirstDate = FirstDate; this.FirstPrice = FirstPrice;
this.SecondDate = SecondDate; this.SecondPrice = SecondPrice;
this.ThirdDate = ThirdDate; this.ThirdPrice = ThirdPrice;
}
}
第一种方法 - 继承:
public class TwoPointsPattern
{
public double FirstDate { get; internal set; }
public double FirstPrice { get; internal set; }
public double SecondDate { get; internal set; }
public double SecondPrice { get; internal set; }
public TwoPointsPattern(double FirstDate, double FirstPrice, double SecondDate,
double SecondPrice)
{
this.FirstDate = FirstDate; this.FirstPrice = FirstPrice;
this.SecondDate = SecondDate; this.SecondPrice = SecondPrice;
}
public virtual void DoSomeLogic()
{...}
}
public class ThreePointsPattern : TwoPointsPattern
{
public double ThirdDate { get; internal set; }
public double ThirdPrice { get; internal set; }
public ThreePointsPattern(double FirstDate, double FirstPrice,
double SecondDate, double SecondPrice, double ThirdDate, double ThirdPrice)
: base(FirstDate, FirstPrice, SecondDate, SecondPrice)
{
this.ThirdDate = ThirdDate; this.ThirdPrice = ThirdPrice;
}
public override void DoSomeLogic()
{...}
}
public class ObjMetaClass
{
public TwoPointsPattern points { get; internal set; }
...
}
// access items from list
for (int i = 0; i < objList.Count; i++)
{
objList[i].points.DoSomeLogic();
}
第二种方法 - 接口:
public interface ISomeLogic
{
void DoSomeLogic();
}
public class TwoPointsPattern : ISomeLogic
{
public void DoSomeLogic();
}
public class ThreePointsPattern : ISomeLogic
{
public void DoSomeLogic();
}
public class ObjMetaClass
{
public ISomeLogic points { get; internal set; }
...
}
// access items from list
for (int i = 0; i < objList.Count; i++)
{
objList[i].points.DoSomeLogic();
}
从你的代码所示的样子ThreePointsPattern
可以为孩子TwoPointsPattern
所以第一次的做法会更好,如果我没有错过任何东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.