I received a class, CommonAdapter
to follow it's behaviour inside a new app I must develop.
public abstract interface CommonAdapter {
public abstract Object fromBusinessToModel(Object object);
public abstract Object fromModelToBusiness(Object object);
}
When used, IMHO is a bit ugly:
@Override
public Object fromBusinessToModel(Object object) {
OwnObjectBO ownObjectBO = (OwnObjectBO) object; // ugly casting
OwnObjectBean ownObjectBean = new OwnObjectBean();
ownObjectBean.setId(Long.parseLong(ownObjectBO.getInternalCode()));
return (OwnObjectBean) ownObjectBean; // unnecessary
}
fromModelToBusiness
method is almost same but in opposite direction.
I'm in my own project, so I can modify this design without problems (and I want to correct and make it better). AFAIK using Object
is discouraged, so I tried to make my own implementation with generics in this way:
public abstract interface CommonAdapter<B,M> {
public abstract M fromBusinessToModel(B b);
public abstract B fromModelToBusiness(M m);
}
And the use will be like:
public class FooAdapter implements CommonAdapter<OwnObjectBO, OwnObjectBean> {
@Override
public OwnObjectBean fromBusinessToModel(OwnObjectBO ownObjectBO) {
OwnObjectBean ownObjectBean = new OwnObjectBean();
ownObjectBean.setId(Long.parseLong(ownObjectBO.getInternalCode()));
return ownObjectBean;
}
@Override
public OwnObjectBO fromModelToBusiness(OwnObjectBean t) {
// not used
return null;
}
}
- Are generics applied in the correct way?
- Is better this case with generics, or use
Object
is the right way? - I used
B
andM
names because are Business and Model objects. Is this ok?
Aucun commentaire:
Enregistrer un commentaire