Putting all files in the same object?
I have the following class hierarchy:
public enum Bonus{
FP,
PRECOIL,
//some ohter types
};
public interface Generator{
public Object generate();
};
public class DateGenerator implements Generator{
public String queryString;
public Integer order;
//GET, SET
public Date generate(){
//implementation
}
};
public class BonusEnumGenerator implements Generator{
public Date bonusAppliedDate;
public String description;
//GET, SET
public Bonus generate(){
//implementation
}
}
So I need to supply these parameters to generate objects in a generic Object.
Is it good if I create a class say GeneratorParams
and put all parameters for all implementations Generator
. In my specific case, I would:
public class GeneratorParams{
public String queryString;
public Integer order;
public Date bonusAppliedDate;
public String description;
}
It annoys me that I put params that are not related to each other in the same object. It's good?
Why do I want to put all parameters in one object?
Beucase I want to write more generic code. In this case, I would:
public interface Generator{
public Object generate();
public void applyParams(GeneratorParams params);
};
and applyParams
will be implemented for each type Generator
.
So I could write something like this:
Generator g = GeneratorPrototypeFactory.createGenerator(TypeId typeId);
GeneratorParams p;
//Getting params
g.applyParams(p);
Object generated = g.generate();
How can I solve this in a more appropriate way?
source to share
just create an GeneratorParams
abstract class and move any generic (in Date
and BonusEnum
generator) parameter:
public abstract class GeneratorParams {
public String commonParam;
//GET, SET
}
then add two subclasses:
public class DateGeneratorParams extends GeneratorParams {
public String queryString;
public Integer order;
// GET, SET
}
public class BonusEnumGeneratorParams implements GeneratorParams {
public Date bonusAppliedDate;
public String description;
// GET, SET
}
now we implement the corresponding applyParam
in DateGenerator
and BonusEnumGenerator
:
public class DateGenerator implements Generator {
public String queryString;
public Integer order;
//GET, SET
public Date generate(){
//implementation
}
@Override
public void applyParams(GeneratorParams params) throws InvalidParamException {
if(!(params instanceof DateGeneratorParams))
throw new InvalidParamException();
else
// assign values
}
}
and
public class BonusEnumGenerator implements Generator {
public Date bonusAppliedDate;
public String description;
//GET, SET
public Bonus generate() {
//implementation
}
public void applyParams(GeneratorParams params) throws InvalidParamException {
if(!(params instanceof BonusEnumGeneratorParams))
throw new InvalidParamException();
else
// assign values
}
}
source to share