• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

parameterized factory method

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Is there a kind of extension or concept for using factory pattern with are parameterized factory method, i.e.
I want to create objects which can differ in parameter type and amount.



Now it's not possible to just use the Factory Pattern, because the constructors of these objects need different parameters.

Has anyboy an idea how to solve this problem best? Or is there even a patten?

Thanks...
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you give a concrete example? I'm not sure what you mean.
 
P. Naz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

usually you have something like this:



No parameter is passed to the constructors. I want something like this.



The Constructors need some parameters passed. But they differ in type and amount, so it is not possible to create a (compiler) save solution.

Have you any idea how to solve this problem best?
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is coming from how the classes are designed, i.e. different constructor signatures.
The constructors should be the same if they are to be considered/referenced with the same interface type.

The construction of a residential house and an apartment building is not the same, even though
humans live in both structures.

The Factory Method design pattern does not address problems that are created by poor object/class design.

The solution to the problem is to fix the design of the classes, or stop trying to abuse the Factory Method pattern.


Anyway, if you wanted to go forward with your "design" as is, you could add a parse routine to extract the objects needed for the various constructors. This does not change anything about the Factory Method design pattern however. It can be implemented in many different ways.

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact the code does not depict the Factory Method design pattern at all. It's just a plain creation method.

Anyway, how does the caller of the createCar method know which/how many parameters to pass into the method???
 
P. Naz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ilja Preuss wrote:Anyway, how does the caller of the createCar method know which/how many parameters to pass into the method???



It's automated. All the information come from a xml file.

I know that this is not actually the factory pattern. But maybe you know other/better solutions than this that still keep the creation automated.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if the parameters are coming from XML, then it's hard to have a "compile time safe" solution, because the XML had to be checked at compile time for that.

It might be worthwhile to look into using Reflection, though. How you would use that depends a lot on your actual problem, though (assuming that you don't really want to create cars), and on what the XML looks like.
 
reply
    Bookmark Topic Watch Topic
  • New Topic