mercredi 25 juillet 2018

How do I correctly use generics in this case?

Below is my Transformer Interface

    public interface Transformer<I, O extends State> {

    public O transformToState(I 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.

Aucun commentaire:

Enregistrer un commentaire