Re: Trouble with factory pattern

From: Alf P. Steinbach (alfps_at_start.no)
Date: 10/16/03


Date: Thu, 16 Oct 2003 14:52:50 GMT

On Thu, 16 Oct 2003 16:22:04 +0200, Boogie El Aceitoso <frr149@telefonica.net> wrote:

>I'd like to have a function factory that returns objects of a class hierarchy.
>What's the best way to deal with the fact that different subclasses will have
>different constructor arguments?

If different subclasses _require_ different constructor arguments this
should be reflected in the factory functions' argument lists. It's not
a C++ question but a design question. If there is a requirement then the
client code will have to supply those arguments, in one form or another.

>How do you keep the factory interface as clean as possible? O:-)

That depends on how you're planning to use that, as well as personal
preference (what _you_, and/or your coworkers, regard as "clean").



Relevant Pages

  • Re: Trouble with factory pattern
    ... If different subclasses _require_ different constructor arguments this ... a C++ question but a design question. ... preference (what _you_, and/or your coworkers, regard as "clean"). ...
    (comp.lang.cpp)
  • Re: Trouble with factory pattern
    ... If different subclasses _require_ different constructor arguments this ... a C++ question but a design question. ... preference (what _you_, and/or your coworkers, regard as "clean"). ...
    (comp.object)
  • comparison of protected members vs abstract classes
    ... The only thing subclasses need do is fill in some blanks -- data ... create a constructor in the parent and pass it up the stack ... Child constructor also gains ...
    (comp.lang.java.programmer)
  • Re: abstract constructor performance?
    ... > the performance hit of the call to the abstract constructor under load. ... my code creates about 200 instances of the subclasses ... If the abstract class has no constructor, then there's no call to it? ... The call itself has a negligible performance penalty - if performance was a ...
    (comp.lang.java.programmer)
  • Re: Executing a method in the parent class automatically after the constructor
    ... several classes that inherit from A. Each of these classes should call ... if the subclasses have accessible constructors. ... It seems a peculiar thing to want to do, ... run at the end of A's own constructor. ...
    (comp.lang.java.programmer)