Suppose I have a POJO User,which I add some static method to do the CRUD operation:
public class User {
private String name;
private int age;
// getter and setter omitted
//
public static void add(User user) {
// add person
}
public static int delete(Object id) {
// delete person
return 1;
}
}
Since I have other entities like User,so I want to make the add and delete method abstracted,something like this;
public class?interface Entity<T>{
static add(T t);
static delete(Object id);
}
public class User extends Entity<User>{
@override
static add(User user){
// add ...
}
....
}
Is this possible?
Don’t make the CRUD methods static in the entity classes, create a generic DAO interface instead and then implement concrete DAO class for each entity type.
Don’t use static methods for such scenario. A better and more OOP approach is to use a Dependency Injection framework that creates only one instance of each concrete DAO class to save memory and time (for creating new DAO instances again and again) and reuses those instances in all places in your application that need to access them.