Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Daniele Futtorovic Newsgroups: comp.lang.java.programmer Subject: Re: How to make my library independent using DI Date: Mon, 4 Feb 2019 16:33:34 +0100 Organization: A noiseless patient Spider Lines: 130 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Mon, 4 Feb 2019 15:33:37 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="e2fc2f58b26de9be6948b64feb8df723"; logging-data="674"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18tcam2Bq7sAhG2E00SP7wr" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.0 Cancel-Lock: sha1:82FaXL8UHr0xX77Xl3TsKeDRXt0= In-Reply-To: X-Antivirus-Status: Clean Content-Language: en-US X-Antivirus: AVG (VPS 190204-2, 02/04/2019), Outbound message Xref: csiph.com comp.lang.java.programmer:38724 On 2019-01-29 19:11, mike wrote: > 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 > You don't need an "ExternalDataService". It doesn't matter whether the data is internal or external. You just need a data class and a data provider class. public interface Data { String productNumber(); String version(); String time(); } public interface DataProvider extends Supplier {} In order to get hold of DataProvider instances, I would suggest looking into the java.util.ServiceLoader mechanism (see Javadoc ). In your own API then, you simply try to load a DataProvider. If you find one, get your data from there. If not, simply throw up. -- DF.