How do Generics and Fields assist DAO Pattern in Standalone Java applications -


//interface dao      public abstract class basedao<t extends basedto> {       public void update(t t) throws dbexception {      field[] fieldstoinsert = t.getclass().getdeclaredfields();     //code update database object academic or event     }       public integer create(t t) throws dbexception {       field[] fieldstoinsert = t.getclass().getdeclaredfields();     //code create academic or event in database     }  }     //concrete daos public class academicdao extends basedao<academicdto> { //provide implementation }  public class eventdao extends basedao<eventdto> { //provide implementation }    //transfer object public class academicdto extends basedto {    string title;    string surname;    //getters , setters }   public class basedto {      protected integer id;      public integer getid() {         return id;     }      public void setid(integer id) {         this.id = id;     } } 

hello guys, have sample code on me follows above structure create small java application manage academics , events. leniently following pattern

1- experts familiar pattern more me. understand why generics used in case daos can extend , implement generic base class. great if 1 can show how generics here may advantageous using example.

2 - have witnessed use of java fields. there link between generics , fields?

i document dao pattern in academic report, finding difficult understand how generics , reflect field play part here. support flexibility , loose coupling?

the code you've provided reusable set of logic load , persist entities. many times, in application of non-trivial size, you'll wind persisting many different types of objects. in example, can define many objects necessary, define logic save , load once. asking dto field objects there, can @ data construct queries loading , saving.

generics allow use pattern while maintaining type safety. academicdao can handle acadmeicdto. can't use academicdao store eventdto. generics allow instance of class rely on more specific type when dealing field objects. if didn't have generics, basedao take object, , wouldn't able access methods except object provides because jvm wouldn't know class provided, has limit it's knowledge of object. using getclass().getdeclaredfields() bypasses limitation because getclass() returns actual class of object parameter.

field way use reflection access values of properties in each dto. if had access fields directly, gettitle(), couldn't reuse generic base class persistence. happen when needed access eventdto? have provide logic that. field allows skip logic.

edit:

to explain mean accessing getid, following within basedao because t known basedto getid() method defined:

public abstract class basedao<t extends basedto> {      public boolean update(t t) throws dbexception {         integer id = t.getid();         field[] fields = t.getclass().getdeclaredfields();         // assuming have db object execute queries using bind variables:         boolean success = db.execute("update table set ... id = ?", id.intvalue());         return success;     } } 

if had instead (in non-generic class):

public boolean update(object o) throws dbexception {     // line doesn't work, since object doesn't have getid() method.     integer id = t.getid();     field[] fields = o.getclass().getdeclaredfields();      boolean success = db.execute("update table set ... id = ?", id.intvalue());     return success; } 

you'd have through field objects, or ask id field , assume existed.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -