Results 1 to 6 of 6
  1. #1
    veronique is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default Complex Factory with @AssistedInjection

    Hi,

    Here is what I'm trying to do. I have the following Factory class:

    Java Code:
    public interface PaymentFactory {
      public Payment create(String text);
    }
    I have it configured:

    Java Code:
    install(new FactoryModuleBuilder()
         .implement(Payment.class, RealPayment.class)
         .build(PaymentFactory.class);
    So this works for when I want to construct a type Payment. But I have bunch of similar objects with similar constuctors which I need to use PaymentFactory rather than creating new Factory for each of those objects.

    According to this doc I can create complex Factories like so:

    Java Code:
    public interface PaymentFactory {
      public Payment create(String text);
      public Payment_1 create(int text);
      public Payment_2 create(Char text);
    }
    with appropriate Guice configuration:
    Java Code:
    install(new FactoryModuleBuilder()
         .implement(Payment.class, RealPayment.class)
         .implement(Payment_1.class, RealPayment.class)
         ...
         .build(PaymentFactory.class);
    The problem is that parameter text should be type String in each of the create() calls and interface won't compile if I do this:

    Java Code:
    public interface PaymentFactory {
      public Payment create(String text);
      public Payment_1 create(String text);
      public Payment_2 create(String text);
    }
    How can I achieve this? I also tried to use abstract type like this:

    Java Code:
    public interface PaymentFactory<T> {
      public T create(String text);
    }
    But Guice won't compile saying that PaymentFactory can't be used as a key because it's not fully defined.

    Please help.

    thanks,
    ruslan

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi
    Can you show a snippet code where you define a PaymentFactory interface?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    veronique is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default

    I'm not sure I understand. This is all I have:

    public interface PaymentFactory {
    public Payment create(String text);
    }

    Then I inject it like this when I need to create a Payment instance using PaymentFactory:

    @Inject PaymentFactory payment;

    public Payment doInvoice(String text) {
    return payment.create(text);
    }

  4. #4
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    So compiler just swears that when you implementation PaymentsFactory interface you don't define all methods. That is all.
    By the way where are you from?
    Skype: petrarsentev
    http://TrackStudio.com

  5. #5
    veronique is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default

    When I do this:

    public interface PaymentFactory {
    public Payment create(String text);
    public Payment_1 create(String text);
    public Payment_2 create(String text);
    }

    Compiler complains that create() method is already defined. BTW everything works with just one create() method.

    I'm in SF.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Well, yes. You can't have mutliple methods where only the return value differs.
    So all you can do is change the name to create(), create1(), create2() (or somesuch).

    Now, of course, if Payment were the base class and Payment_1 and Payment_2 were children (as should be the case with a Factory) then you'd just need a single create, and the Factory determines (based on the input) what type of Payment object to return.

    Of course, I've not worked with guice, so I may be a little out there, but that would be my take on this problem.

Similar Threads

  1. Complex GUI 60 JComboBox + 40+ JTextBox
    By Paul_White in forum New To Java
    Replies: 8
    Last Post: 05-12-2011, 07:51 PM
  2. Complex file input
    By JMaste in forum Advanced Java
    Replies: 4
    Last Post: 12-08-2010, 03:55 AM
  3. Creating a complex toolbar in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-02-2008, 08:09 PM
  4. Complex Regular Expression HELP
    By hiklior in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 01:52 PM
  5. Complex proximity Clauses
    By peiceonly in forum Lucene
    Replies: 1
    Last Post: 08-07-2007, 05:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •