I\'m experimenting with using generics to support a configurable structure of delegating objects (decorators, wrappers). I want to build a chain of delegators that implement
Under your definition, a Delegator is a Delegator of itself (like Comparable is for example), however it seems the intention is that Delegator is a Delegator of a super class. Luckily, generics has a way of expressing this:
static class DelegatorChain> {}
This says that the "Delagator type must be a super class of T". With this change, the rest of your original code compiles:
static interface Delegator {}
static class DelegatorChain> {}
static interface Foo {}
static class FooDelegator implements Delegator, Foo {}
public static void main(String[] args) {
DelegatorChain chain = new DelegatorChain();
}
Also, anytime you use a generic super bound, your code looks really cool :)
Note: This following was originally the "first option" in the question.
There is another way to get your code to compile, but it is inferior because it loses the connect between the Delegator type and what it's delegating from:
// Not recommended, but will allow compile:
static class FooDelegator implements Delegator, Foo {}
// However, this also compiles :(
static class FooDelegator implements Delegator, Bar {}