我有一個班級,它返回三個屬性。第一個屬性依賴於某個參數,第二個屬性依賴於第一個屬性,第三個屬性依賴於第二個屬性。如何設計我的班級
實現這種類型的最佳方式是什麼?對於這個類型有沒有合適的設計模式?下面我粘貼了我的代碼的兩個版本,這兩個版本都工作,我不知道哪個更好(或者我太複雜了)?
第一招:
class Initializer
{
private string lastCode;
private int lastPackage;
private int lastBox;
public Initializer(int machineNumber)
{
lastCode = GetLastCodeFromDatabase(machineNumber);
lastPackage = GetLastPackageByLastCode(lastCode);
lastBox = GetLastBoxByLastPackage(lastPackage);
}
public string LastCode
{
get { return lastCode; }
}
public int LastPackage
{
get { return lastPackage; }
}
public int LastBox
{
get { return lastBox; }
}
private string GetLastCodeFromDatabase(int machineNumber)
{
using (InitializerTableAdapter adapterGetLastCode = new InitializerTableAdapter())
{
return Convert.ToString(adapterGetLastCode.GetLastCodeByMachineNumber(machineNumber));
}
}
private int GetLastPackageByLastCode(string lastCode)
{
using (InitializerTableAdapter adapterGetLastPackage = new InitializerTableAdapter())
{
return Convert.ToInt32(adapterGetLastPackage.GetLastPackageByLastCode(lastCode));
}
}
private int GetLastBoxByLastPackage(int lastPackage)
{
using (InitializerTableAdapter adapterGetLastPackage = new InitializerTableAdapter())
{
return Convert.ToInt32(adapterGetLastPackage.GetLastBoxByPackageNumber(lastPackage));
}
}
}
二:
class Initializer
{
public static string LastCode(int machineNumber)
{
return GetLastCodeFromDatabase(machineNumber);
}
public static int LastPackage(string lastCode)
{
return GetLastPackageByLastCode(lastCode);
}
public static int LastBox(int lastPackage)
{
return GetLastBoxByLastPackage(lastPackage);
}
private static string GetLastCodeFromDatabase(int machineNumber)
{
using (InitializerTableAdapter adapterGetLastCode = new InitializerTableAdapter())
{
return Convert.ToString(adapterGetLastCode.GetLastCodeByMachineNumber(machineNumber));
}
}
private static int GetLastPackageByLastCode(string lastCode)
{
using (InitializerTableAdapter adapterGetLastPackage = new InitializerTableAdapter())
{
return Convert.ToInt32(adapterGetLastPackage.GetLastPackageByLastCode(lastCode));
}
}
private static int GetLastBoxByLastPackage(int lastPackage)
{
using (InitializerTableAdapter adapterGetLastPackage = new InitializerTableAdapter())
{
return Convert.ToInt32(adapterGetLastPackage.GetLastBoxByPackageNumber(lastPackage));
}
}
}
在這個類的幫助下,我在啓動時設置了我的應用程序的GUI,所以它只運行一次,我需要一次獲取所有屬性。 – sventevit 2009-10-10 16:20:12