开发者

represent identifier hierarchy - java

开发者 https://www.devze.com 2023-03-11 23:51 出处:网络
I have a need to have XYZIdentifier object which will be used in composition in other class to identify the object.

I have a need to have XYZIdentifier object which will be used in composition in other class to identify the object.

However, Depending on the use-case (a variable in the client object), the identifier can be either a String, long, or even a Class.

Something like IntegerIdentifier, StringIdentifier, FooIdentifier and some interface defined which can be generic.

How can I create th开发者_如何学Pythonis design?


Not sure what your full context is, but part of the solution may involve generics to fetch the kind of result you need, like:

public <T extends IdentifierBase> T getIdentifier(Class<T> kindYouWant) { ... }

The idea being you tell it which flavor you need, and it could either convert with best-effort, or return Null if that kind isn's available.

Like I said, not sure of your whole question...

0

精彩评论

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

关注公众号