Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Andreas Leitgeb Newsgroups: comp.lang.java.programmer Subject: Re: How to make my library independent using DI Date: Fri, 1 Feb 2019 14:13:06 -0000 (UTC) Organization: A noiseless patient Spider Lines: 36 Message-ID: References: Reply-To: avl@logic.at Injection-Date: Fri, 1 Feb 2019 14:13:06 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="595fc5a7c9905ef2e7132c98353a9de0"; logging-data="7541"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19NhaEHcWtIzk+dYGRcuAYM" User-Agent: slrn/1.0.3 (Linux) Cancel-Lock: sha1:95wsaRcaPlR3QFWk3TMbW19KQxw= Xref: csiph.com comp.lang.java.programmer:38723 mike wrote: > I need to create an instance of AHelper, in the application using the library, > and inject it into my lib. Otherwise I will not be able to access the data from > version() and productNumber(). > I mean shall I call: > MyDataClassImpl( new AHelper()) from the application? That's surely the most straightforward thing to do. There might be other options (even as ugly as involving static fields - don't do that, unless the application really only needs a single instance for the whole VM) > And at what point in time should this be done. Apparently exactly when you need the instance of MyDataClassImpl if more classes of the library need a reference to an > And if the MyDataClassImpl needs more external data, let's say > from BHelper() also, do add the following in my app: > MyDataClassImpl(new AHelper(),new BHelper()) ? If the new data is *necessary* you just add new methods to the interface in the lib, and it will require other applications to upgrade along with the lib (or stick to older versions of the lib) If you're on Java8 or newer, and if it makes sense, you might add default-implementations to the interface for the new methods, so the lib will remain compatible with old apps, (but not with Java7 or older, then) Different helpers (imho) only make sense, if they cover different parts of the library, and aren't always necessary at the same time.