這不是關於what is a class
或what is an object
的問題。我應該如何反對模型類藍圖和具體類?
我想確定一個設計模式,以供重用。
我有一個類的藍圖,其中包含一個與字段名稱和該字段的屬性值鍵入的地圖。這個映射值描述了一個特定類的字段。
class FieldDescriptor {
public FieldDescriptor(String name, int length, boolean isKey) {
....
}
...
}
class ConcreteClass {
final public static Map<String, FieldDescriptor> fields;
static {
Map<String, FieldDescriptor> myFields = new HashMap<String, FieldDescriptor>();
myFields.put("PERSON_CODE", new FieldDescriptor("PERSON_CODE", 10, true);
myFields.put("FUN_FUN_FUN", new FieldDescriptor("FUN_FUN_FUN", 6, false);
myFields.put("JEDI_POWER_RATING", new FieldDescriptor("JEDI_POWER_RATING", 9000, true);
fields = Collections.unmodifiableMap(myFields);
}
private String personCode;
private String funFunFun;
private String jediPowerRating;
public void setPersonCode(String personCode) {
this.personCode = transformField(fields.get("PERSON_CODE"), personCode);
}
...
}
整個reason for the maddness is the transformField call on the setters
。這是我爲什麼創建地圖的核心。
但是我想從我的課程中抽象出來,因爲我想以這種方式構建更多的類,並且能夠一般地或通過接口參考map
。
我強烈地感覺到地圖應該封裝在一個單獨的類中!但是,ConcreteClass的實例將如何發生?
任何人都可以確定一個合適的設計模式?
對不起,如果我的術語不正確。 – JavaRocky 2011-04-17 23:14:31
對字段描述符的小修改:JEDI_POWER_RATING需要超過9000. – BoffinbraiN 2011-04-18 13:50:05