开发者

Is there benefit in a generified interface?

开发者 https://www.devze.com 2023-02-09 17:11 出处:网络
Recently in an answer it was suggested to me that this: public interface Operation<R extends OperationResult, P extends OperationParam> {

Recently in an answer it was suggested to me that this:

public interface Operation<R extends OperationResult, P extends OperationParam> {

    public R execute(P param);
}

Is better than this:

public interface Operation {

    public OperationResult execute(OperationParam param);
}

I however can't see any benefit in using the first code block over the second one ...

Given th开发者_运维技巧at both OperationResult and OperationParam are interfaces an implementer needs to return a derived class anyway and this seems quite obvious to me.

So do you see any reason the use the first code block over the second one ?


This way you can declare your Operation implementations to return a more specific result, e.g.

 class SumOperation implements Operation<SumResult, SumParam>

Though whether this is of any value to your application depends entirely on the situation.

Update: Of course you could return a more specific result without having a generic interface, but this way you can restrict the input parameters as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消