Results 1 to 6 of 6

Thread: Prototype or Container-Aware?

  1. #1
    Join Date
    May 2006
    Posts
    2

    Question Prototype or Container-Aware?

    I have a situation where I need to create an undetermined number of instances of a particular class. Rather than programatically call getBean() and be container-aware I am considering injecting one instance and cloning it.

    I believe this would be following the Prototype pattern, staying container-agnostic and following the Dependency Injection approach.

    What are your thoughts?

    Thanks in advance,
    codeGorilla

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    I would suggest prototype - hiding the instance management is one of the advantages of IoC - the component does not have to be aware of how its dependencies are created/retrieved.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Quote Originally Posted by codegorilla
    I have a situation where I need to create an undetermined number of instances of a particular class. Rather than programatically call getBean() and be container-aware I am considering injecting one instance and cloning it.

    I believe this would be following the Prototype pattern, staying container-agnostic and following the Dependency Injection approach.

    What are your thoughts?

    Thanks in advance,
    codeGorilla
    You might also want to look at method injection: http://static.springframework.org/sp...thod-injection

    Essentially you define a Bean getYourBean() on your class and everytime you call getYourBean() spring will essentially factory.call getBean() for you.

    Maybe that would be better than cloning because all the dependencies on yourBean will be wired up for you.

    Just a thought.

  4. #4
    Join Date
    May 2006
    Posts
    2

    Default

    Thanks for the feedback. I'll review the method injection approach and compare this with using a prototype. I will be needing this solution in several places throughout my project. My main concern now will be maintainability... which approach is easier to follow.

    I'll post my results soon,
    codeGorilla

  5. #5
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    Another technique I've used is to have a property which takes a org.springframework.beans.factory.ObjectFactory. You can inject an instance of this using a ObjectFactoryCreatingFactoryBean in your XML file. Its a good alternative to using BeanFactoryAware. It is still Spring specific, but less intrusive than BeanFactoryAware and doesn't require your object be clonable.

    Although I don't often like my code to be Spring specific, there are a few places where this sometimes happens
    • Data Access
    • JMS
    • Special factories, particularly if my prototypes takes arguments when they are created


    But I still strive to keep my domain objects and stateless services container agnostic. And when I can't, its usually a sign for a collaborator to be created to hide these details behind an interface.
    Bill

  6. #6
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Quote Originally Posted by codegorilla
    Thanks for the feedback. I'll review the method injection approach and compare this with using a prototype. I will be needing this solution in several places throughout my project. My main concern now will be maintainability... which approach is easier to follow.

    I'll post my results soon,
    codeGorilla
    To be clear; the method injection will inject a bean that in your case should be defined as singleton=false, i.e. the bean will be a prototype.

    Just so we are on the same page

Posting Permissions

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