Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35877

Re: Parameterized Factory with additional parameter requires new interface?

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Parameterized Factory with additional parameter requires new interface?
Date 2016-10-28 11:12 -0400
Organization A noiseless patient Spider
Message-ID <nuvprg$5jd$1@dont-email.me> (permalink)
References <d1c8beb2-3ef7-4965-afa0-4cb0902c02fb@googlegroups.com> <nuvj57$gus$1@newsreader4.netcologne.de>

Show all headers | View raw


On 10/28/2016 9:17 AM, Patrick Roemer wrote:
> Responding to mike:
>> public interface Factory<T, R> {
>>
>>     /**
>>      * 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<T, R,P> {
>>
>>     /**
>>      * 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<T,R,P>; it cannot be parameterized with arguments <XXXtype, XXXresource>
>
> If this was valid...
>
> Factory<XXXtype, XXXresource> 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<R, P> {
	    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<T, Smuggler<R, P>> {
	    public T create(Smuggler<R, P> data) {
	        R resource = data.getResource();
	        P parameter = data.getParameter();
	       ...
	    }
	}

(Double-check my <type parameters>; I've not done so ...)

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Parameterized Factory with additional parameter requires new interface? mike <mikaelpetterson@hotmail.com> - 2016-10-27 23:51 -0700
  Re: Parameterized Factory with additional parameter requires new interface? j4n bur53 <janburse@fastmail.fm> - 2016-10-28 10:27 +0200
  Re: Parameterized Factory with additional parameter requires new interface? Andreas Leitgeb <avl@logic.at> - 2016-10-28 11:59 +0000
  Re: Parameterized Factory with additional parameter requires new interface? Patrick Roemer <sangamon@netcologne.de> - 2016-10-28 15:17 +0200
    Re: Parameterized Factory with additional parameter requires new interface? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-28 11:12 -0400
      Re: Parameterized Factory with additional parameter requires new interface? j4n bur53 <janburse@fastmail.fm> - 2016-10-28 18:07 +0200
        Re: Parameterized Factory with additional parameter requires new interface? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-28 16:43 -0400

csiph-web