java通用演员问题

时间:2011-07-24 14:53:55

标签: java generics casting

我想实现一种传输对象模式。所以,我有一个通过BeanUtils.copyProperties(..,..)填充对象属性的方法(常见的apache)。

以下是代码:

public abstract class BaseTO<TObject> {

    public Long id;

    /***
     * Obtains id of the object that´s transfered
     * @return object´s id
     */
    public Long getId() {
        return id;
    }

    /****
     * set transfered object´s id 
     * @param id object´s id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /***
     * Fill transfer object properties.
     * @param entity entity to be transfered
     * @return self reference
     */
    public BaseTO<TObject> build(TObject entity){
        try {
            BeanUtils.copyProperties(this, entity);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        customDataTransformation(entity);
        return this;
    }

    protected void customDataTransformation(TObject entity) {
    }


}

CustomerTO Class

public class CustomerTO extends BaseTO<Customer> {

    private String name;
    private String surname;
    private String email;
    private String sex;
    private DocumentType documentType;
    private String documentNumber;

    --- getters and setters

    @Override
    protected void customDataTransformation(Customer entity) {
        this.sex = Sex.MALE.equals(entity.getSex()) ? "M" : "F";
    }


}

问题

CustomerTO toEdit = (CustomerTO) (customerId!=null ? new CustomerTO().build(entityObject):new CustomerTO());

你可以看到这里必须转发给(CustomerTO)。我希望如果有可能避免这种情况,使代码更简单。

是否可以公共BaseTO构建(TObject实体)可以返回子类的对象??

我希望能够清楚。

提前致谢。

2 个答案:

答案 0 :(得分:1)

也许试试这个:

    class BaseTO<TObject, R extends BaseTO<TObject,R>> {

    public R build(TObject entity) {

然后是CustomerTO:

class CustomerTO extends BaseTO<Customer, CustomerTO> {

或更少限制,仅更改build签名:

public <X extends BaseTO<TObject>> X build(TObject entity) {

但是恕我直言更好的方法只是将构造函数添加到TO with TObject参数。

public BaseTO(TObject entity) {
        try {
            BeanUtils.copyProperties(this, entity);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        customDataTransformation(entity);
}

然后在每个扩展类中创建简单的构造函数

     public CustomerTO(Customer entity) {
        super(entity);
    }

忘了build方法并简单地使用它

CustomerTO toEdit = (customerId!=null ? new CustomerTO(entityObject):new CustomerTO());

答案 1 :(得分:1)

编译:

public class BaseTO<T> {

    public BaseTO<T> build(T entity) {
       return this;
    }

    public static class CustomerTO extends BaseTO<String> {

       @Override public CustomerTO build(String string) {
           return (CustomerTO) super.build(string);
       }
}

但您必须为build的所有子类重写BaseTO。你只需要刻录一次演员而不是每次调用build

编辑:在上面的评论中看到@Paul提出的观点。你可能会遭受“给男人一把锤子,一切看起来像钉子一样。”