Java Reference
In-Depth Information
public double getPrice() {
return price;
}
public String getName() {
return name;
}
public void setProperty(String property, Object value) {
validateProperty(property);
instanceProperties.put(property, value);
}
public Object getProperty(String property) {
validateProperty(property);
if (!instanceProperties.containsKey(property)) {
// Get the default initialized property from the type
return type.getInstanceProperty(property);
}
return instanceProperties.get(property);
}
public Object getStaticProperty(String property) {
if (!type.isStaticProperty(property))
throw new IllegalArgumentException("Static property "+property+
" not valid for type: "+type.getName());
return type.getStaticProperty(property);
}
private void validateProperty(String property) {
if (!type.isInstanceProperty(property))
throw new IllegalArgumentException("Property "+property+
" not valid for type: "+type.getName());
}
}
Your main class will then look like this:
public class TypeTest {
public static void main(String[] args) {
// Product types now have a discount calculator field...
BookDiscountCalculator bookDiscounter = new BookDiscountCalculator();
ProductType bookType = new ProductType("Book");
bookType.addInstanceProperty("title", null);
bookType.addInstanceProperty("author", null);
bookType.addInstanceProperty("pages", 0);
bookType.addStaticProperty("discountCalculator", bookDiscounter);
Product aBook = bookType.createProduct("My book", 44.99);
aBook.setProperty("title", aBook.getName());
aBook.setProperty("author", "Aimee, Bart and Seppe");
aBook.setProperty("pages", 540);
Search WWH ::




Custom Search