Java泛型。我的情况有什么好处? [英] Java Generics. What benefit in my case?

查看:149
本文介绍了Java泛型。我的情况有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此时我开始基于MVC开发小型Web应用程序。
现在我尝试使用DAO模式实现模型布局的主要类。

因此,首先创建两个实体类(例如):Author and Book :

  package myProject.model.entity; 

import java.io.Serializable;

public class Author实现Serializable {

private static final long serialVersionUID = 7177014660621405534L;

私人长ID;
private String firstName;
private String lastName;

public Author(){
}
// getter和setter方法在这里

}

和Book类:

  package myProject.model。实体; 

import java.io.Serializable;

public class Book实现Serializable {

private static final long serialVersionUID = 7177014660621405534L;

私人长ID;
私有字符串标题;
私有字符串描述;

public Book(){
}
// getter和setter方法在这里

}

在下一步中,我看到Book和Author都有 getId() SETID()
因此,我为我的实体类创建了接口 Persistent

 打包myProject.model.entity; 

public interface Persistent {

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



$ / code $ / pre

因此,首先我的问题是: p>

对于模型包是否正确实现?



开下一步,我开始为包 dao 实现类。

  package myProject.model.dao; 

import java.util.List;

导入myProject.model.entity.Persistent;

public interface Dao {

持久get(long id);

void save(永久持久);

void delete(long id);
}

下一步:创建接口 AuthorDao BookDao ,它们扩展了基地台界面Dao



但是这两个接口:AuthorDao和BookDao - 在这一刻都是空的。
您认为什么 - 在正常情况下,接口是空的?这是我的第二个问题。



在最后一步中,我创建包 model.dao.hibernate 并添加到包类为AuthorDaoHibernate和BookDaoHibernate - 两个类都实现了AuthorDao和BookDao接口。



现在我的主要问题是:



我的界面 Dao 使用对象类型 Persistent ,我不使用泛型。如果我重新工作 Dao ,你会怎么想 - 我有什么好处?接口机智泛型:

  package myProject.model.dao; 

import java.util.List;

导入myProject.model.entity.Persistent;

public interface Dao< Persistent> {

T get(long id);

列表< T>得到所有();

void save(T persistent);

void delete(long id);
}

我的Dao类只能用于持久化实体 - 没有任何其他对象类型.. 。

你真的有什么理由在我使用泛型吗?

p>泛型可以极大地提高代码的可读性并减少可能来自错误转换的错误。



我们使用类似于您所描述的内容(请注意,接口<



下面是一个基本示例(我将把getter和setter放在brevitiy中):

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

@Entity
类UserEnity扩展BaseEntity {
//用户类似名称
的东西}

class BaseDAO< T扩展了BaseEntity> {
public T findById(int id){
...
}
//其他通用CRUD方法
}

@Stateless
class UserDAO扩展了BaseDAO< UserEntity> {
//额外的用户特定方法
}

然后使用UserDAO像这样:

  UserDAO userDao; //一些注入或查找

//不需要显式转换,这要感谢泛型
UserEntity user = userDao.findById(userId);

//由于泛型参数为UserEntity和AnotherEntity,编译器错误不会扩展该
AnotherEntity a = userDao.findById(someId);


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.

So, first of all I create two entity classes (for example): Author and 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

    }

and Book class:

  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

        }

On next step, I see, that classes Book and Author both have getId() and setId(). so, I create interface Persistent for my Entity classes:

 package myProject.model.entity;

        public interface Persistent {

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


        }

So, first my question:

It is correct implementation for model package?

On the next step, I start implement classes for package 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);
}

Next step: create interfaces AuthorDao and BookDao, that extend base dao interface Dao

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.

And on the last step I create package model.dao.hibernate and add to the package to class AuthorDaoHibernate and BookDaoHibernate - both class implements AuthorDao and BookDao interfaces.

And My main question now:

my interface Dao work with objects type Persistent and I dont use Generics. And all ok and nice.

What do you thinks - what benefits I have, if I re-work Dao interface wit Generics:

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

My Dao classes work only with persistent entities - no any other object type...

Do you really any reasons in me case use Generics?

解决方案

Generics can greatly improve code readability and reduce errors that could come from wrong casting.

We're using something similar to what you described (note that there are interfaces and implementations needed).

Here's a basic example (I'll leave the getters and setters out for brevitiy):

@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
}

Using UserDAO would then be like this:

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

这篇关于Java泛型。我的情况有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆