generics - Java DAO factory dynamic returned object type -
i'm trying write simple dao create entity objects based on type stored in string field. how return type changed dynamicly? method findbyid() of userdao class should return user class object. same method productdao should return product. don't want implement findbyid in every class extends dao, should done automatically.
example code:
class dao { protected string entityclass = ""; public (???) findbyid(int id) { // db query return (???)entityfromdatabase; // how this? } } class userdao extends dao { protected string entityclass = "user"; } class productdao extends dao { protected string entityclass = "product"; } class user extends entity { public int id; public string name; }
modify to
class dao<t> { // protected string entityclass = ""; public t findbyid(int id) { return (t)entityfromdatabase; // how this? } } class userdao extends dao<user> { //protected string entityclass = "user"; } class productdao extends dao<product> { //protected string entityclass = "product"; } class user extends entity { public int id; public string name; }
Comments
Post a Comment