I'm trying to implement the fluet interface pattern. So basicly the idea is to have chaining methods, but i'am mixing it with the strategy pattern, so i can have multiple implementations of "Foo"
I have this abstract class:
public abstract class Foo<T extends BarData> { T data; public Foo(T data) { this.data = data; } public Foo<T> fooMethod { // do stuff with data return this; } } This is another implementation of Foo that provides another method:
public class Bar extends Foo<BarData> { public Bar(BarData data) { super(data); } public Bar barMethod() { // do more stuff with data return this; } } Fluent Interface:
public interface FluentInterface<T extends BarData, S extends Foo<T>> { S initialize(); } Basic implementation:
public class BasicFluentInterfaceImplementation implements FluentApi<BarData, Bar> { @Override Bar initialize() { // prepares data return new Bar(data); } } Using all together:
public class Main { public static void main(String[] str) { FluentInterface api = new BasicFluentInterfaceImplementation(); var firstReturn = api.initialize(); // Here it returns Bar var secondReturn = firstReturn.fooMethod(); // As Bar extends Foo i can call the fooMethod() var thirdReturn = secondReturn.barMethod(); // Here is the problem, i can't call the barMethod, because fooMethod already returned an intance of Foo } } How can i make Foo return a instance of Bar? Considering that Bar can be anything that extends Foo.
class Bar<T extends BarData> extends Foo<T>FluentInterface#initializeandFoo#fooMethodreturns aFoo, and this is where game is over.varalong with generics. You could have hard time with wildcard captures.