public abstract class BaseHolidayPackageVariant {
private Integer variantId;
private HolidayPackage holidayPackage;
private String holidayPackageType;
}
public class LandHolidayPackageVariant extends BaseHolidayPackageVariant{
}
public class FlightHolidayPackageVariant extends BaseHolidayPackageVariant{
private Destination originCity;
}
public class HolidayPackage{
ArrayList<BaseHolidayPackageVariant> variants;
BaseHolidayPackageVariant defaultVariant;
}
在運行時,我怎麼能知道,如果在多個變[]給定對象爲LandPackageVariant
類型或FlightPackageVariant
沒有做各種各樣的東西:
if(holidayPackageType.equals("LandHolidayPackageVariant")
obj = (LandHolidayPackageVariant)variant[i];
else if(holidayPackageType.equals("FlightHolidayPackageVariant")
obj = (FlightHolidayPackageVariant)variant[i];
這個問題源於從設計的問題,我問here
同意關於特定類型。否則我不知道如何建模。例如如果您查看「FlightHolidayPackageVariant」,它會添加一個成員:「OriginCity」。在給定的'HolidayPackageVariant'變體[]中,當我渲染一個'FlightHolidayPackageVariant'時,我想呈現'OriginCity'的名字。我無法從'BaseHolidayPackageVariant'中檢索到這個文件。 – brainydexter 2012-03-22 14:04:49
增加了一個可能解決這個問題的例子。 – 2012-03-22 14:50:08
感謝您的編輯,我明白了您的觀點。我的腦袋裏有一個相關的問題,我似乎無法回答。遵循以不同方式呈現變體的同一個示例,我還想將(視圖)aka渲染細節保留在我的對象模型之外。我如何使用您提供的解決方案來支持? – brainydexter 2012-03-22 18:59:37