假设我有一个带抽象构建器的抽象类,它们都由3个独立的子类继承:
public abstract class Role {
protected string name;
protected int propertyA;
protected int propertyB;
protected abstract static class RoleBuilder<T extends Role, B extends RoleBuilder<T,B>> {
protected T role;
protected B roleBuilder;
protected abstract T getRole();
protected abstract B thisBuilder();
protected RoleBuilder(Hero h) {
role = getRole();
roleBuilder = thisBuilder();
role.name = h.name;
role.propertyA = h.propertyA;
role.propertyB = h.propertyB;
}
public T build() {
return role;
}
}
}
public class Healer extends Role {
int propertyC;
public Healer() {
}
public static final class HealerBuilder extends Role.RoleBuilder<Healer, HealerBuilder> {
@Override protected Healer getRole() {
return new Healer();
}
@Override protected HealerBuilder thisBuilder() {
return this;
}
public HealerBuilder(Hero h) {
super(h);
}
public HealerBuilder setPropertyC(int i) {
role.propertyC = i;
return roleBuilder;
}
}
}
我在枚举中使用枚举方法实例化healer类(以及其他两个子类),如下所示:
public enum RoleTypes {
BRUISER{
...
},
DAMAGE_DEALER {
...
},
HEALER {
public Healer getRole(Hero h) {
return new Healer.HealerBuilder(h)
.setPropertyC(h.getC);
.build();
}
};
abstract Role getRole(Hero h);
}
我应该在哪里验证创建这些对象的参数?
在抽象类构造函数中(我应该验证Hero对象属性,还是我可以假设该对象在创建时验证其属性是否“安全”?)和子类构建器方法?
在enum方法中?
在调用RoleType.getRole(h)的工厂方法中,我的应用程序的其余部分用于实际创建Role类型的对象?