We have code that looks like this:
Service<E,F> pipeline = new MyServiceDecorator2(new MyServiceDecorator1(new MyService()));
This is then executed as F f = pipeline.apply(new E("E"))
We'd like this to read something like:
Service<A,B> myService = new MyService();
MyServiceDecorator1 myServiceDecorator1 = new MyServiceDecorator1();
MyServiceDecorator2 myServiceDecorator2 = new MyServiceDecorator2();
Service<E,F> pipeline = myServiceDecorator2.andThen(myServiceDecorator1).andThen(myService);
// This should still be doable i.e., the end goal
F f = pipeline.apply(new E("E"));
We've tried various tricks but we can't get the return types to line up correctly. The above would throw an error - we just added andThen
to each Decorator class manually just to understand the flow, like so:
public <J,K> Service andThen(Service<J,K> next) {
this.service = next;
return next;
}
This returns the type of the "next item" in the chain. I tried some tricks with next/prev
references/pointers to go up the chain but nothing seems to work. Is this even possible?
Here's a REPL showing the code with a print statement showing progression through the decorators.
Context: We have a fair amount of code that can be simplified into "decorators" to implement the "pipe and filter" pattern so that we can provide a basic "framework" to allow developers to apply the same pattern/thinking to solve the same problem vs. copy/pasta or reinventing the wheel. The above is an "example" of what we intend to achieve.
Aucun commentaire:
Enregistrer un commentaire