I would like to create several versions of the same type of dependency tree / chain that use different implementations for some interfaces in this tree / chain. What is the best practice / guice sample to use in this case?
Here is a concrete example of my problem.
I have a Writer interface that could potentially be a file creator or a std-out creator that will be located on a sheet of my dependency hierarchy. Something like that:
interface Writer { ... } class FileWriter implements Writer { ... } class StdOutWriter implements Writer { ... }
Another registrar interface is used to add an indirectness layer to the records. For instance:
interface Logger { ... } class LoggerImpl{ @Inject public Logger(Writer out){ ... } public void log(String message){ out.println(message); } }
Then there is a client who uses the registrar.
class Client{ @Inject public Client(Logger logger){ ... } public void do(){ logger.log("my message"); } }
Now I would like to use two types of hierarchy in my program:
- Client -> LoggerImpl -> FileWriter
- Client -> LoggerImpl -> StdOutWriter
Is there a good way to connect this device without using a separate Guice module for 1 and 2?
Ideally, I would like to have a ClientFactory class as follows:
interface ClientFactory{ public Client stdOutClient(); public Client fileClient();
Can anyone come up with a way to link this with this factory or any other way?
I would also like a solution that scales when I have more variety of longer trees / chains of dependencies. Thanks!
source share