Kid101 :
Below is my Transformer Interface
public interface Transformer<BusinessObject, O extends State>
{
public O transformToState(BusinessObject input);
}
This is one of my Transformer Impl
public class GoldTransformer implements Transformer<BusinessObject, Gold>
{
@Override
public Gold transformToState(BusinessObject input) {
GoldBO goldbo= (GoldBO) input; // redundant casting line
//do some transformation with BO to make it a state with some business logic
}
}
Here is my another Transformer Impl
public class SilverTransformer implements Transformer<BusinessObject, Sliver>
{
@Override
public Gold transformToState(BusinessObject input) {
SilverBO goldbo= (SilverBO) input; // redundant casting line
// again do some transformation with BO to make it a state with some business logic
}
}
Both SilverBO
and GoldBO
Implements BusinessObject
which is a marker interface. And Silver
and Gold
extend State
. I really find the casting annoying and redundant is there a better way to use generics here? or a better pattern to use? I don't want to make any changes to state
i.e. gold and silver.
Mureinik :
You could generalize the interface on the input BusinessObject
too:
public interface Transformer<I extends BusinessObject, O extends State> {
public O transformToState(I input);
}
public class GoldTransformer implements Transformer<GoldBO, Gold> {
@Override
public Gold transformToState(GoldBO input) {
// Code...
}
}