tags:

views:

158

answers:

3

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.

+3  A: 

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 what T is, but wants to expose a strongly-typed API in terms of T. I can't see that fitting in with your policy example.

Jon Skeet
A: 

What you are trying to do is make sure that a Thing always has a policy ie. a Thing cannot exist with a policy. This requires only constructor mechanism or abstraction like,

public abstract class Thing {
   public abstract Policy getPolicy();
   public void doSomething() {
       getPolicy().execute();
   }
}
Sandy
A: 

Even the generic version wouldn't avoid "passing the policy to the constructor" because Java generics use type erasure. You can, however, implement the GenericThing(Class<T> clazz) constructor but that's overkill:

public class GenericThing<T extends IPolicy> {

    private T policy;

    public GenericThing(Class<T> clazz) throws InstantiationException, IllegalAccessException {
     policy = clazz.newInstance();
    }

    public GenericThing(T policy) {
     this.policy = policy;
    }

    public void doSomething() {
     this.policy.execute();
    }
}

I very much agree with Jon's answer. You are not really taking advantage of this being strongly typed.

bruno conde
Why the downvote? Did I sad anything wrong? Please explain ...
bruno conde