Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Parameterized Factory with additional parameter requires new interface? Date: Fri, 28 Oct 2016 11:12:05 -0400 Organization: A noiseless patient Spider Lines: 97 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 28 Oct 2016 15:11:44 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="eba2875249e1f2a483e6a6056f04352b"; logging-data="5741"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX184ETnQoJf9Jim+BWuM6rkP" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 In-Reply-To: Cancel-Lock: sha1:UgZqQVANTNg1Es3rySZrv0Sl3do= Xref: csiph.com comp.lang.java.programmer:35877 On 10/28/2016 9:17 AM, Patrick Roemer wrote: > Responding to mike: >> public interface Factory { >> >> /** >> * Creates a new equipment type instance >> * >> * @param resource >> * @return equipment instance of type T >> */ >> public T create(R data); >> >> } >> >> >> Now we need an additional create method that takes an additional parameter of type P. >> >> >> public interface Factory { >> >> /** >> * Creates a new equipment type instance >> * >> * @param resource >> * @return equipment instance of type T >> */ >> public T create(R resource); >> >> /** >> * Creates a new equipment type instance >> * >> * @param resource >> * @param parameter >> * @return equipment instance of type T >> */ >> public T create(R resource,P parameter); >> } >> >> >> Then in the code I get: >> >> Incorrect number of arguments for type Factory; it cannot be parameterized with arguments > > If this was valid... > > Factory factory = ... > > ...how should compiler (and/or runtime) handle this? > > XXXresource res = ... > XXXtype t = factory.create(res, "foo"); > >> Does this mean we have to create a new parameterized interface for each additional create() method that we have? > > What additional create() methods do you expect? > > Either your new design is valid. This basically would mean that you need > a P in addition to an R to be able to create a T, but that there's an > implied default value for any conceivable P that can be used to create a > T only given an R, so the "old" method signature is kept as a > convenience for this case. Then you should be able to come up with a > legitimate (not just "dummy") concrete type for P in your existing > instantiations. > > Or the two factory approaches are independent. There might be types R > where there simply is no additional P value required to create a T. > Conversely, one could imagine types R and P such that the second method > could be implemented, but not the first one, i.e. there's no default > value for P. Then these two approaches should go to different interfaces. Another possibility is to "smuggle" the parameter to the factory by packaging it with the resource: class Smuggler { private final R resource; private final P parameter; Smuggler(R resource, P parameter) { this.resource = resource; this.parameter = parameter; } R getResource() { return resource; } P getParameter() { return parameter; } } class MyFactory implements Factory> { public T create(Smuggler data) { R resource = data.getResource(); P parameter = data.getParameter(); ... } } (Double-check my ; I've not done so ...) -- esosman@comcast-dot-net.invalid "Don't be afraid of work. Make work afraid of you." -- TLM