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


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

Re: Parameterized Factory with additional parameter requires new interface?

From Andreas Leitgeb <avl@logic.at>
Newsgroups comp.lang.java.programmer
Subject Re: Parameterized Factory with additional parameter requires new interface?
Date 2016-10-28 11:59 +0000
Organization A noiseless patient Spider
Message-ID <slrno16fdm.uka.avl@login.logic.tuwien.ac.at> (permalink)
References <d1c8beb2-3ef7-4965-afa0-4cb0902c02fb@googlegroups.com>

Show all headers | View raw


mike <mikaelpetterson@hotmail.com> wrote:
> Hi,
> We have the following factory interface today:
> public interface Factory<T, R> {
>     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> {
>     public T create(R resource);
>     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>

You could either change those places to specify a dummy third 
Type argument, but I'd rather go a different route:

Give the new Factory a different name, like:

public interface SpecialFactory<T,R,P> extends Factory<T,R> {
    public T create(R resource,P parameter);
}

Then you don't need to change the places that only need the first
method.

> Does this mean we have to create a new parameterized interface
> for each additional create() method that we have?

"have to" is too strong, as you could also change all uses of Factory
to add a third (dummy) argument.

Another (equally bad) alternative would be using raw-types, Suppress-
Warnings-annotations and explicit casts.

For a cleaner solution, you should refactor your Factory hierarchy,
if there are so many extra methods to offer of which each requires
its own Type parameter. If you worry about the combinations blow
up complexity exponentially, then make a separate (non-hierarchical)

Interface for each overload, and a single implementation that 
implements all of the separate interfaces.

> If that is the case can you help me understand why?
Because Java has no optional Type parameters, 

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