We got a problem while generalizing the Rewards class with builder pattern. The super class, Rewards class has a builder pattern and it's sub class AnthemRewards also has a builder pattern.
Now the problem is, we want to maintain the relationship or recurring pattern with builder. The below approach works for it.
Reference: http://www.artima.com/weblogs/viewpost.jsp?thread=133275
Now the problem is, we want to maintain the relationship or recurring pattern with builder. The below approach works for it.
Reference: http://www.artima.com/weblogs/viewpost.jsp?thread=133275
public class Rewards {
private final int rewardId;
private final String rewardCode;
private Rewards(Builder builder) {
this.rewardId = builder.rewardId;
this.rewardCode = builder.rewardCode;
}
public static class Builder<T extends Builder> {
private int rewardId;
private String rewardCode;
public static getInstance() {
return new Builder();
}
public T setRewardId(int rewardId) {
this.rewardId = rewardId;
return (T) this;
}
public T setRewardCode(String rewardCode) {
this.rewardCode = rewardCode;
return (T) this;
}
public Rewards build() { return new Rewards(this); }
}
}
public class AnthemRewards extends Rewards {
private final String rewardType;
public static class Builder extends Rewards.Builder<Builder> {
private final RewardType rewardType;
public static getInstance() {
return new Builder();
}
public Builder AnthemRewards(RewardType rewardType) {
this.rewardType = rewardType
return this;
}
public AnthemReward build() { return new AnthemReward(this); }
}
protected AnthemReward(Builder builder) {
super(builder);
this.rewardType = builder.rewardType;
}
}