This discussion is archived
2 Replies Latest reply: Sep 4, 2010 12:12 PM by 843793 RSS

Policy-based design.

843793 Newbie
Currently Being Moderated
Hi,

I have a while loop, and the user should be able to decide when the loop stops. After x seconds, after x loops, ... This problem should be implemented according to policy-based design. I know how to do this in C++ but can't get it to work in Java.

What I do now is the following.

There is a class Auctioneer with the method "start()" where the policies should be applicable:
public <E extends AbstractEndAuctionPolicy> void start(E policy) { //use policy here }
Because AbstractEndAuctionPolicy has the method "endAuction()", we are able to do: policy.endAuction(). In C++ there is no need for "extends AbstractEndAuctionPolicy" ...

But I can not figure out how to use this method, the following is not working:
this.auctioneer.start<NewBidPolicy>(n);
Hope you guys can help me and inform me a bit about policy-based design in Java because Google is not giving me answers.

Thanks in advance.
  • 1. Re: Policy-based design.
    843793 Newbie
    Currently Being Moderated
    Ok, I figured out that NewBidPolicy has to extend AbstractEndAuctionPolicy in the class definition. But now I don't see the advantage of generics anymore. It's just inheritance now?

    Why use
    public <E extends AbstractEndAuctionPolicy> void start(E policy) { //use policy here }
    instead of
    public void start(AbstractEndAuctionPolicy policy) { //use policy here }
    ?

    Edited by: MrDckx on Sep 4, 2010 5:33 AM
  • 2. Re: Policy-based design.
    843793 Newbie
    Currently Being Moderated
    MrDckx wrote:
    Ok, I figured out that NewBidPolicy has to extend AbstractEndAuctionPolicy in the class definition. But now I don't see the advantage of generics anymore. It's just inheritance now?
    What was the advantage before? (And, looking at collections at least, there's always a point to generics.)

    The only reason you don't have to specify a bound in C++ is that the compiler will actually see if the template compiles for the given type arguments. So essentially you have a bound as well, it's just not visible and it is structural.

    With kind regards
    Ben