vendredi 17 mai 2019

Can I avoid explicit cast with generics?

I have an interface

public interface Query {

}

and its implementation:

public class UserQuery implements Query {
// specific properties to query a user
}

and another interface

public interface Queries{

protected void runQuery(Query q);
}

and its implementation which make use of it:

public UserQueries extends Queries{

@Override
protected void runQuery(Query q){

// can I avoid this explicit cast with generic type parameters or other design patterns?
// for example Query<UserQuery> ?
var uq = (UserQuery) q;
..

}

}

All works, however, how can I avoid the cast in runQuery(Query q) (Maybe with runQuery(Query<T> q))? Are there any better design-pattern for such uses cases as above?

Aucun commentaire:

Enregistrer un commentaire