[英]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.