我可以在java中使用方法链接来创建抽象构建器类,而不进行不安全的操作吗? [英] Can I have an abstract builder class in java with method chaining without doing unsafe operations?

查看:107
本文介绍了我可以在java中使用方法链接来创建抽象构建器类,而不进行不安全的操作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为某些构建器类提供一个抽象基类,以便在Builder实现之间轻松地重用代码。我希望我的构建者支持方法链,因此方法必须返回最具体类型的this实例。我想我可以用泛型来做这件事。不幸的是,我没有设法使用不安全的操作。是否有可能?



以下是我如何尝试它(以及它如何工作)的示例代码。我想避免在foo()(这会导致未经检查的警告)中投射到T,可以这样做吗?

 <$ c $ ().build()。foo().bar()。build()方法来创建一个新的构造函数, ; 
}
}


抽象类AbstractBuilder< T扩展AbstractBuilder<>>
{
public T foo()
{
//设置一些属性
return(T)this;
}
}


类TheBuilder扩展AbstractBuilder< TheBuilder>
{
public TheBuilder bar()
{
//设置其他属性
return this;
}

public Object build()
{
return new Object();



$ div $解析方案

你要在 AbstractBuilder , T >。



使用抽象保护方法来获取这个类型 T

 抽象类AbstractBuilder< T extends AbstractBuilder< T>> {
protected abstract T getThis();

public T foo(){
//设置一些属性
return getThis();
}
}


类TheBuilder扩展AbstractBuilder< TheBuilder> {
@Override protected TheBuilder getThis(){
return this;
}
...
}

或者,泛型类型参数,依赖于协变返回类型,并使客户端的代码更清晰(尽管通常它们会使用 TheBuilder ,而不是基类的大部分实现细节),if使实现更加冗长。


I'm trying to have an abstract base class for some builder classes so I can easily reuse code between the Builder implementations. I want my builders to support method chaining therefore a method has to return "this" instance of the most specific type. I figured I could probably do this with generics. Unfortunatly I did not manage to do it without using unsafe operations. Is it possible?

Sample code of how I'm trying it (and how it works) below. I'd like to avoid casting to T in "foo()" (which causes an unchecked warning), can this be done?

public class Builders
{
   public static void main( final String[] args )
   {
      new TheBuilder().foo().bar().build();
   }
}


abstract class AbstractBuilder<T extends AbstractBuilder<?>>
{
   public T foo()
   {
      // set some property
      return (T) this;
   }
}


class TheBuilder extends AbstractBuilder<TheBuilder>
{
   public TheBuilder bar()
   {
      // set some other property
      return this;
   }

   public Object build()
   {
      return new Object();
   }
}

解决方案

You want to declare T as extends AbstractBuilder<T> in AbstractBuilder.

Use an abstract protected method to get this of type T.

abstract class AbstractBuilder<T extends AbstractBuilder<T>> {
    protected abstract T getThis();

    public T foo() {
        // set some property
        return getThis();
    }
}


class TheBuilder extends AbstractBuilder<TheBuilder> {
    @Override protected TheBuilder getThis() {
        return this;
    }
    ...
}

Alternatively, drop the generic type parameter, rely on covariant return types and make the code cleaner for clients (although usually they would be using TheBuilder rather than the largely implementation detail of the base class), if making the implementation more verbose.

这篇关于我可以在java中使用方法链接来创建抽象构建器类,而不进行不安全的操作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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