Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38719
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2019-01-29 10:11 -0800 |
| Message-ID | <c9bc00ee-744c-49e2-b026-08a68841cf48@googlegroups.com> (permalink) |
| Subject | How to make my library independent using DI |
| From | mike <mikaelpetterson@hotmail.com> |
Hi,
I am creating a library that can be used with different java applications.
Sometimes I need data from different applications but I want to avoid a direct dependency to these applications.
In my library I have the following classes:
package scrap;
public interface MyDataClass {
String productNumber();
String version();
String time();
}
and
public class MyDataClassImpl implements MyDataClass{
private ExternalDataService externalDataService;
public MyDataClassImpl(ExternalDataService externalDataService){
this.externalDataService = externalDataService;
}
@Override
public String productNumber() {
//Here we have a dependency to an external library call it x
return externalDataService.productNumber();
}
@Override
public String version() {
//Here we have a dependency to an external library call it x
return externalDataService.version();
}
@Override
public String time() {
return calcTime();
}
private String calcTime () {
return "";
}
}
as well as:
package scrap;
public interface ExternalDataService {
public String productNumber();
public String version();
}
Then in the application using this lib I have:
package other;
import scrap.ExternalDataService;
public class AHelper implements ExternalDataService{
@Override
public String productNumber() {
return methodA();
}
@Override
public String version() {
return methodB();
}
public String methodA() {
return "";
}
String methodB() {
return "";
}
}
I have two questions:
1. How can I inject AHelper into MyDataClassImpl without creating a "hard dependency" without using a DI container?
2. If I used a container like Guice how would that look like?
All hints welcome.
br,
Mike
Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread
How to make my library independent using DI mike <mikaelpetterson@hotmail.com> - 2019-01-29 10:11 -0800
Re: How to make my library independent using DI Martin Gregorie <martin@mydomain.invalid> - 2019-01-29 19:29 +0000
Re: How to make my library independent using DI Andreas Leitgeb <avl@logic.at> - 2019-01-30 10:11 +0000
Re: How to make my library independent using DI mike <mikaelpetterson@hotmail.com> - 2019-01-30 06:13 -0800
Re: How to make my library independent using DI Andreas Leitgeb <avl@logic.at> - 2019-02-01 14:13 +0000
Re: How to make my library independent using DI Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-02-04 16:33 +0100
csiph-web