Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Generics. What benefit in my case?
    primarykey
    data
    text
    <p>At this moment I start work on small web application based on MVC. Now I try implement main classes for Model layout using DAO pattern.</p> <p>So, first of all I create two entity classes (for example): Author and Book:</p> <pre><code>package myProject.model.entity; import java.io.Serializable; public class Author implements Serializable { private static final long serialVersionUID = 7177014660621405534L; private long id; private String firstName; private String lastName; public Author() { } // getter and setter methods here } </code></pre> <p>and Book class:</p> <pre><code> package myProject.model.entity; import java.io.Serializable; public class Book implements Serializable { private static final long serialVersionUID = 7177014660621405534L; private long id; private String title; private String description; public Book() { } // getter and setter methods here } </code></pre> <p>On next step, I see, that classes Book and Author both have <code>getId()</code> and <code>setId()</code>. so, I create interface <code>Persistent</code> for my Entity classes:</p> <pre><code> package myProject.model.entity; public interface Persistent { public long getId(); public void setId(long id); } </code></pre> <p>So, first my question: </p> <p>It is correct implementation for <code>model</code> package?</p> <p>On the next step, I start implement classes for package <code>dao</code>.</p> <pre><code>package myProject.model.dao; import java.util.List; import myProject.model.entity.Persistent; public interface Dao { Persistent get(long id); void save(Persistent persistent); void delete(long id); } </code></pre> <p>Next step: create interfaces <code>AuthorDao</code> and <code>BookDao</code>, that extend base dao <code>interface Dao</code></p> <p>But both interfaces: AuthorDao and BookDao - at this moment empty. What do you think - it in normal, that interfaces empty? It is my second question.</p> <p>And on the last step I create package <code>model.dao.hibernate</code> and add to the package to class AuthorDaoHibernate and BookDaoHibernate - both class implements AuthorDao and BookDao interfaces.</p> <p><strong>And My main question now:</strong></p> <p>my interface <code>Dao</code> work with objects type <code>Persistent</code> and I dont use Generics. And all ok and nice.</p> <p>What do you thinks - what benefits I have, if I re-work <code>Dao</code> interface wit Generics:</p> <pre><code>package myProject.model.dao; import java.util.List; import myProject.model.entity.Persistent; public interface Dao&lt;Persistent&gt; { T get(long id); List&lt;T&gt; getAll(); void save(T persistent); void delete(long id); } </code></pre> <p>My Dao classes work only with persistent entities - no any other object type... </p> <p>Do you really any reasons in me case use Generics?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload