Hi,

I have a Service interface and multiple sub-interfaces that extend it,
with their own Implementations. How do I cast a Resource to the most
appropriate implementation?

The following client code checks sub-interfaces first and falls back
to the base interface:

    if (serviceRes.canAs(com.atomgraph.linkeddatahub.model.dydra.Service.class))
service = serviceRes.as(com.atomgraph.linkeddatahub.model.dydra.Service.class);
    else if (serviceRes.canAs(...) service = serviceRes.as(...);
    ...
    else service =
serviceRes.as(com.atomgraph.linkeddatahub.model.Service.class); //
base interface

It does not look like a good solution however. I could probably move
this logic into the base ServiceImplementation, but I would still have
to modify it every time I add a new sub-interface that extends
Service.

Is there a more generic solution to this?


Martynas

Reply via email to