I have some Java code similar to:
public class Thing { private IPolicy policy; public Thing(IPolicy policy) { this.policy = policy; } public void doSomething() { this.policy.execute(); } }
My question is: is it possible to do this with generics rather than passing the policy to the constructor? I think this means I want it to end up as
public class Thing<T extends IPolicy>
but I’m not that up on generics.
My motivations are: 1) it would make more sense to me for my program for this to be part of the type and not be involved with the constructor (which has other more relevant things to do) and 2) I’m trying to learn generics.
I don’t think that makes a lot of sense. Isn’t it the policy itself that you depend on, rather than just the type of policy? Aren’t you going to want to ask the policy questions (e.g. whether or not an action is allowed)? That means you’ll need an instance of the policy – which means it makes sense for it to be passed into the constructor.
Generics are suitable when you want a strongly typed API where part of the API depends on another type, but the implementation itself doesn’t care what the type is (potentially with some constraints). The collections API is probably the canonical example – a
List<T>doesn’t care whatTis, but wants to expose a strongly-typed API in terms of T. I can’t see that fitting in with your policy example.