簡體   English   中英

Java泛型。 對我來說有什么好處?

[英]Java Generics. What benefit in my case?

現在,我開始研究基於MVC的小型Web應用程序。 現在,我嘗試使用DAO模式為模型布局實現主類。

因此,首先,我創建兩個實體類(例如):Author和Book:

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

    }

和書籍類:

  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

        }

下一步,我看到Book類和Author類都具有getId()setId() 因此,我為Entity類創建了Persistent接口:

 package myProject.model.entity;

        public interface Persistent {

                public long getId();
                public void setId(long id); 


        }

所以,首先我的問題是:

model包的正確實現?

下一步,我開始實現dao包的實現類。

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);
}

下一步:創建接口AuthorDaoBookDao ,以擴展基礎dao interface Dao

但是兩個接口:AuthorDao和BookDao-目前為空。 您怎么看-正常情況下,接口為空? 這是我的第二個問題。

最后一步,我創建包model.dao.hibernate並將其添加到類AuthorDaoHibernate和BookDaoHibernate中,這兩個類都實現了AuthorDao和BookDao接口。

我現在的主要問題是:

我的界面Dao使用Persistent類型的對象,並且我不使用泛型。 一切都很好。

您的看法-如果我對Dao接口和泛型進行重新設計,我有什么好處:

package myProject.model.dao;

import java.util.List;

import myProject.model.entity.Persistent;

public interface Dao<Persistent> {

    T get(long id);

    List<T> getAll();

    void save(T persistent);

    void delete(long id);
}

我的Dao類僅適用於持久性實體-沒有其他任何對象類型...

在我看來,您真的有任何理由使用泛型嗎?

泛型可以極大地提高代碼的可讀性,並減少由於錯誤的轉換而引起的錯誤。

我們正在使用與您所描述的類似的東西(請注意,需要接口實現)。

這是一個基本示例(為簡便起見,我將getter和setter方法省略):

@MappedSuperClass
class BaseEntity {
  @Id
  private int id;
}

@Entity
class UserEnity extends BaseEntity {
  //user stuff like name
}

class BaseDAO<T extends BaseEntity> {
  public T findById(int id) { 
    ... 
  }
  //other generic CRUD methods
}

@Stateless
class UserDAO extends BaseDAO<UserEntity> {
  //additional user specific methods
}

然后使用UserDAO將是這樣的:

 UserDAO userDao; //some injection or lookup

 //no explicit cast needed here, thanks to generics
 UserEntity user = userDao.findById(userId);

 //compiler error due to the generic parameter being UserEntity and AnotherEntity doesn't extend that
 AnotherEntity a = userDao.findById(someId);

如果要使用泛型,則應按以下方式定義Dao:

public interface Dao<T extends Persistent> {
    .....................
    void save(T persistent);
    ...................
}

現在,當您擴展它時,您將必須創建僅接受Book的保存:

public class Book extends Dao<Book> {
    .....................
    void save(Book persistent);
    ...................
}

這樣做的好處是您不能將Author傳遞給BookDao 這不會通過編譯。

順便說一句,如果您使用的是Hibernate,JPA或其他ORM解決方案,則實際上不必為每個實體創建DAO。 一個通用的dao可以解決您的所有需求。

這里沒有理由。 如果它是唯一的,那么根據定義它不是通用的! 列表getAll()將完成任務。

ArrayList是泛型的,因為有時有時會返回Persistent。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM