Reputation: 96391
Consider that object wrapped in DataContainer
will be handed out to client
// Will be handed out
public interface DataContainer
In order to use it, client currently needs to know to cast the object to:
public interface ConcreteObject_1_Container extends DataContainer
public interface ConcreteObject_2_Container extends DataContainer
Is it possible to offer both ConcreteObject1Container
and ConcreteObject2Container
as options to be chosen from, similar to how Enum
options can be picked?
Instead of user magically knowing to use FileContainer
// user knows
FileContainer fileContainer = (FileContainer)
ContainerFactory.getContainerFor(DataSource.FILE,
TREAT_AS_SOURCE);
I'd like to
// user selects
FileContainer fileContainer = (GenericContainer.FileContainer)
ContainerFactory.getContainerFor(DataSource.FILE,
TREAT_AS_SOURCE);
Upvotes: 3
Views: 87
Reputation: 32407
You should probably change your design. Either:
DataContainer
, without caring what implementation it is, in which case the cast can be avoided, orUpvotes: 6
Reputation: 7108
If you can make getConatainerFor a generic method, then it will be able to return the specific type using type inference.
Upvotes: 0