Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collection of Interfaces to pick from, Java

Tags:

java

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); 
like image 550
James Raitsev Avatar asked Jan 20 '26 23:01

James Raitsev


1 Answers

You should probably change your design. Either:

  1. the client just uses the methods on DataContainer, without caring what implementation it is, in which case the cast can be avoided, or
  2. the client is actually coupled to the implementation, so should just use the implementation types directly (e.g. with two different methods)
like image 127
artbristol Avatar answered Jan 24 '26 02:01

artbristol