使用泛型的生成器
Builder with generics
我有以下层次结构
双重通用 class 命名为
public class Service<T, U>
{
private final Supplier<T>
private final Function<T, U>
private final Consumer<U>
// more fields and methods ...
}
class ServiceBuilder<T, U>
的生成器,具有通常的流利 API 生成器
public class ServiceBuilder<T, U>
{
private Supplier<T> supplier;
private Function<T, U> mapper;
private Consumer<U> consumer;
// more fields and methods ....
public ServiceBuilder<T, U> withSupplier(Supplier<T> supplier)
{
this.supplier = supplier;
return this;
}
// more fields and methods ....
public Service<T, U> build()
{
return new Service(supplier, mapper, consumer);
}
}
基于此,我想提供一个简单易用的Supplier of T,比方说DummySupplier
public class DummySupplier implements Supplier<SomeObject>
{
public SomeObject get()
{
return new SomeObject();
}
}
我想要一个利用这个供应商的 ServiceBuilder<SomeObject, T>
,有效地修复 T
这样我只需要一个 Function<SomeObject, U> mapper
和一个 Consumer<U> consumer>
构建我的服务。
我该如何处理?扩展 ServiceBuilder
不起作用,因为我想重用其所有现有方法 return ServiceBuilder
而不是某些 class 扩展 ServiceBuilder
...
是否有一些已知的模式可供处理?
Function<SomeObject, U> mapper
和 SomeObject
都不认识 DummySupplier
。
此信息必须放在某个地方,例如 SpecializedServiceBuilder
:
public class SpecializedServiceBuilder<U> extends ServiceBuilder<SomeObject, U> {
ServiceBuilder withSomeObjectFunction(Function<SomeObject, U> mapper) {
this.supplier = new DummySupplier();
this.mapper = mapper;
return this;
}
}
我不明白你为什么需要一个指定的子类,你可以创建一个工厂方法给你一个标准 ServiceBuilder<SomeClass, U>
。
public static <U> ServiceBuilder<SomeClass, U> withDummyBuilder() {
return new ServiceBuilder<SomeClass, U>().withSupplier(SomeClass::new);
}
然后您可以再次调用 withSupplier
并覆盖 new SomeClass()
供应商,但我不明白为什么这是个问题。
我有以下层次结构
双重通用 class 命名为
public class Service<T, U>
{
private final Supplier<T>
private final Function<T, U>
private final Consumer<U>
// more fields and methods ...
}
class ServiceBuilder<T, U>
的生成器,具有通常的流利 API 生成器
public class ServiceBuilder<T, U>
{
private Supplier<T> supplier;
private Function<T, U> mapper;
private Consumer<U> consumer;
// more fields and methods ....
public ServiceBuilder<T, U> withSupplier(Supplier<T> supplier)
{
this.supplier = supplier;
return this;
}
// more fields and methods ....
public Service<T, U> build()
{
return new Service(supplier, mapper, consumer);
}
}
基于此,我想提供一个简单易用的Supplier of T,比方说DummySupplier
public class DummySupplier implements Supplier<SomeObject>
{
public SomeObject get()
{
return new SomeObject();
}
}
我想要一个利用这个供应商的 ServiceBuilder<SomeObject, T>
,有效地修复 T
这样我只需要一个 Function<SomeObject, U> mapper
和一个 Consumer<U> consumer>
构建我的服务。
我该如何处理?扩展 ServiceBuilder
不起作用,因为我想重用其所有现有方法 return ServiceBuilder
而不是某些 class 扩展 ServiceBuilder
...
是否有一些已知的模式可供处理?
Function<SomeObject, U> mapper
和 SomeObject
都不认识 DummySupplier
。
此信息必须放在某个地方,例如 SpecializedServiceBuilder
:
public class SpecializedServiceBuilder<U> extends ServiceBuilder<SomeObject, U> {
ServiceBuilder withSomeObjectFunction(Function<SomeObject, U> mapper) {
this.supplier = new DummySupplier();
this.mapper = mapper;
return this;
}
}
我不明白你为什么需要一个指定的子类,你可以创建一个工厂方法给你一个标准 ServiceBuilder<SomeClass, U>
。
public static <U> ServiceBuilder<SomeClass, U> withDummyBuilder() {
return new ServiceBuilder<SomeClass, U>().withSupplier(SomeClass::new);
}
然后您可以再次调用 withSupplier
并覆盖 new SomeClass()
供应商,但我不明白为什么这是个问题。