我具有以下层次结构
一个双重通用类,名为
public class Service<T, U>
{
private final Supplier<T>
private final Function<T, U>
private final Consumer<U>
// more fields and methods ...
}
该类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);
}
}
基于此,我想提供一个易于使用的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
不起作用,因为我要重用的所有现有方法都返回ServiceBuilder
,而不是某些类扩展了ServiceBuilder
...
是否有一些已知的模式可以使用?
Function<SomeObject, U> mapper
和SomeObject
都不知道DummySupplier
。该信息必须放置在某个地方,例如SpecializedServiceBuilder
: