我如何优化 类 的结构
How can I optimize the structure of my classes
我正在努力定义我的 classes
的结构
我创建了classObjMetaClass
的对象列表。其中包含有关其对象 points
的一些元信息(points
:ObjMetaClass
class 的 属性)。
但是points
可以有两种,一种是TwoPointsPattern
class,另外两种是ThreePointsPattern
class
我担心的是,每当我想访问任何项目时,我首先需要检查当前项目中存储了哪个项目以进行投射(是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
检查?有什么建议,请
classes
的结构
ObjMetaClass
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
的子代,所以如果我没有遗漏任何东西,第一种方法会更好。
我正在努力定义我的 classes
的结构我创建了classObjMetaClass
的对象列表。其中包含有关其对象 points
的一些元信息(points
:ObjMetaClass
class 的 属性)。
但是points
可以有两种,一种是TwoPointsPattern
class,另外两种是ThreePointsPattern
class
我担心的是,每当我想访问任何项目时,我首先需要检查当前项目中存储了哪个项目以进行投射(是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
检查?有什么建议,请
classes
的结构
ObjMetaClass
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
的子代,所以如果我没有遗漏任何东西,第一种方法会更好。