This tutorial will try to explain how to use multiple generic parameters in Java and also along the way the advantage of using this feature.
Normally I use this when I have a method in a base class that returns a generic type. For example:
The example doesn't make sense in business but if you will look at it as a piece of code you can see that you can get the dynamic type specified in Child in Parent class.
Now a more logical example :-) Assume that have the following:
entity User, Role;
service: UserService, RoleService
bean: UserBean, RoleBean
Often we do a CRUD for these types of entities and so we need dynamic page generation and extendable entity, bean and service. That will do common things like for example in service: find, create, delete, update.
Let's start with the entity User and Role, both of them should extend a BaseEntity. I'll omit the annotation since I'm lame :-).
Now what if I have an unknown action and I want to get the BaseBean with the correct type? Here's how I did it:
Normally I use this when I have a method in a base class that returns a generic type. For example:
public class Parent<E extends BaseType> {
public E e;
public E getType() {
return e;
}
}
public class Child extends Parent {
public Child() {
super(Type.class);
}
}
public class Type extends BaseType {
}
The example doesn't make sense in business but if you will look at it as a piece of code you can see that you can get the dynamic type specified in Child in Parent class.
Now a more logical example :-) Assume that have the following:
entity User, Role;
service: UserService, RoleService
bean: UserBean, RoleBean
Often we do a CRUD for these types of entities and so we need dynamic page generation and extendable entity, bean and service. That will do common things like for example in service: find, create, delete, update.
Let's start with the entity User and Role, both of them should extend a BaseEntity. I'll omit the annotation since I'm lame :-).
public interface IEntity {
public Long getId();
}
public class BaseEntity implements Serializable, IEntity {
private Long id;
}
public class User extends BaseEntity {
private String username;
}
public class Role extends BaseEntity {
private String roleName;
}
Now recently I encountered a problem where I want to parameterized the entity and service in a baseBean. So I first configure the base service:
public interfaces IPersistenceService {
public void create();
public void remove();
//...
}
public abstract class PersistenceService<E extends IEntity> implements IPersistenceService<E> {
public void create() { //.. }
public void remove() { //.. }
}
//Why extends? because whether the IEntity is a class or interface, E always extends it.
Now the much complicated part, how do I pass the entity and service dynamically to a base bean?
public class BaseBean<E extends IEntity, T extends PersistenceService<E>> {
private E entity;
@Inject
private T persistenceService;
public void create() {
persistenceService.create();
}
}
public class UserBean<User, UserService> extends BaseBean {
public UserBean() {
super(User.class, UserService.class);
}
}
public class RoleBean<User, RoleService> extends BaseBean {
public RoleBean() {
super(Role.class, RoleService.class);
}
}
Also take note that I was successful in injecting a generic service :-).
Now what if I have an unknown action and I want to get the BaseBean with the correct type? Here's how I did it:
public BaseBean<? extends IEntity, ? extends PersistenceService<?>> getBaseBean() {
return null;
}
Post a Comment