Naming factory functions

Discussion in 'C++' started by Chris Croughton, Mar 24, 2005.

  1. What do people call their factory functions? 'new' is not an option
    (yes, it can be overloaded but has to return void*).

    The context is:

    class MyClass
    {
    public:
    // Factory functions
    static MyClass* new(int);

    // Produce a copy of the class
    virtual MyClass* dup() = 0;

    // Access functions
    virtual int whatever(int) = 0;
    };

    To me it makes most sense to say:

    MyClass* mp = MyClass::new(1);

    but 'new' isn't allowed.

    Are there any quasi-standard names for the factory functions? Whatever
    the name it shouldn't repeat information which is already known
    (MyClass::newMyClass() is silly). The "make a copy" function, dup(),
    is reasonably named (OK, I like short names).

    I want a word which implies that what is returned is a pointer to a new
    version of the class ('make' and 'create' don't imply that to me)...

    Chris C
    Chris Croughton, Mar 24, 2005
    #1
    1. Advertising

  2. * Chris Croughton:
    >
    > I want a word which implies that what is returned is a pointer to a new
    > [instance] of the class ('make' and 'create' don't imply that to me)...


    If you want a description of the result then a verb isn't good, unless the
    result is an action.

    Try 'instance' or 'instanceFrom'.

    Those are good names and seem to be just what you're looking for.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Mar 24, 2005
    #2
    1. Advertising

  3. Chris Croughton

    Jeff Schwab Guest

    Chris Croughton wrote:
    > What do people call their factory functions? 'new' is not an option
    > (yes, it can be overloaded but has to return void*).


    > I want a word which implies that what is returned is a pointer to a new
    > version of the class ('make' and 'create' don't imply that to me)...


    make_new?
    Jeff Schwab, Mar 24, 2005
    #3
  4. On Thu, 24 Mar 2005 13:35:53 GMT, Alf P. Steinbach
    <> wrote:

    > * Chris Croughton:
    >>
    >> I want a word which implies that what is returned is a pointer to a new
    >> [instance] of the class ('make' and 'create' don't imply that to me)...

    >
    > If you want a description of the result then a verb isn't good, unless the
    > result is an action.


    A method or function is a verb, it's a "doing thing". The result is a
    "being thing".

    > Try 'instance' or 'instanceFrom'.


    'instanceFrom()' doesn't make much sense to me. 'newInstance()' might
    work, though...

    > Those are good names and seem to be just what you're looking for.


    'instanciate()' would be another one, to go with 'duplicate()' if I were
    using long names. I suppose if I'm using 'dup()' then I could use
    'inst()' as an abbreviation.

    MyClass* p = Myclass::inst();
    MyClass* q = p->dup();

    Thanks, I was running out of words in the thesaurus...

    Chris C
    Chris Croughton, Mar 25, 2005
    #4
  5. * Chris Croughton:
    > On Thu, 24 Mar 2005 13:35:53 GMT, Alf P. Steinbach
    > <> wrote:
    >
    > > * Chris Croughton:
    > >>
    > >> I want a word which implies that what is returned is a pointer to a new
    > >> [instance] of the class ('make' and 'create' don't imply that to me)...

    > >
    > > If you want a description of the result then a verb isn't good, unless the
    > > result is an action.

    >
    > A method or function is a verb, it's a "doing thing". The result is a
    > "being thing".


    That mind-set stands in the way of enlightenment... ;-)

    It's especially harmful for const-correctness in classes.

    E.g. when your height() accessor is named get_height(), it's likely not
    const as it should be.


    > > Try 'instance' or 'instanceFrom'.

    >
    > 'instanceFrom()' doesn't make much sense to me. 'newInstance()' might
    > work, though...
    >
    > > Those are good names and seem to be just what you're looking for.

    >
    > 'instanciate()' would be another one, to go with 'duplicate()' if I were
    > using long names. I suppose if I'm using 'dup()' then I could use
    > 'inst()' as an abbreviation.
    >
    > MyClass* p = Myclass::inst();
    > MyClass* q = p->dup();
    >
    > Thanks, I was running out of words in the thesaurus...


    Try

    MyClass* p = MyClass::instance();
    MyClass* q = p->clone();

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Mar 25, 2005
    #5
  6. Jeff Schwab wrote:
    > Chris Croughton wrote:
    >
    >> What do people call their factory functions? 'new' is not an option
    >> (yes, it can be overloaded but has to return void*).

    >
    >
    >> I want a word which implies that what is returned is a pointer to a new
    >> version of the class ('make' and 'create' don't imply that to me)...

    >
    >
    > make_new?


    I'm confused as to why 'create' does not imply to you that a new
    instance (not version) of a class is instantiated, seeing as its the
    typical name for factory methods and abstract factories.

    see:

    http://www.dofactory.com/Patterns/PatternFactory.aspx#_self2

    http://www.tml.hut.fi/~pnr/GoF-models/html/Abstract-factory.html
    Andrew McDonagh, Mar 25, 2005
    #6
  7. Jeff Schwab wrote:
    > Chris Croughton wrote:
    >
    >> What do people call their factory functions? 'new' is not an option
    >> (yes, it can be overloaded but has to return void*).

    >
    >
    >> I want a word which implies that what is returned is a pointer to a new
    >> version of the class ('make' and 'create' don't imply that to me)...

    >
    >
    > make_new?


    oops sorry Jeff, seemed to have replied to your post rather than Chris's.
    Andrew McDonagh, Mar 25, 2005
    #7
  8. On Fri, 25 Mar 2005 23:03:55 +0000, Andrew McDonagh
    <2s.com> wrote:

    > Jeff Schwab wrote:
    >> Chris Croughton wrote:
    >>
    >>> What do people call their factory functions? 'new' is not an option
    >>> (yes, it can be overloaded but has to return void*).

    >>
    >>> I want a word which implies that what is returned is a pointer to a new
    >>> version of the class ('make' and 'create' don't imply that to me)...

    >
    > I'm confused as to why 'create' does not imply to you that a new
    > instance (not version) of a class is instantiated, seeing as its the
    > typical name for factory methods and abstract factories.


    Hmm, I couldn't find it as a factory name.

    > see:
    >
    > http://www.dofactory.com/Patterns/PatternFactory.aspx#_self2
    >
    > http://www.tml.hut.fi/~pnr/GoF-models/html/Abstract-factory.html


    Neither of those use 'create()' as a factory name that I can see.

    However, it is growing on me...

    Chris C
    Chris Croughton, Mar 25, 2005
    #8
  9. Chris Croughton wrote:
    > On Fri, 25 Mar 2005 23:03:55 +0000, Andrew McDonagh
    > <2s.com> wrote:
    >
    >
    >>Jeff Schwab wrote:
    >>
    >>>Chris Croughton wrote:
    >>>
    >>>
    >>>>What do people call their factory functions? 'new' is not an option
    >>>>(yes, it can be overloaded but has to return void*).
    >>>
    >>>>I want a word which implies that what is returned is a pointer to a new
    >>>>version of the class ('make' and 'create' don't imply that to me)...

    >>
    >>I'm confused as to why 'create' does not imply to you that a new
    >>instance (not version) of a class is instantiated, seeing as its the
    >>typical name for factory methods and abstract factories.

    >
    >
    > Hmm, I couldn't find it as a factory name.
    >
    >
    >>see:
    >>
    >>http://www.dofactory.com/Patterns/PatternFactory.aspx#_self2
    >>


    For the above url - look at the 'real World example code

    here's a snippet from it...

    // "ConcreteCreator"

    class Resume : Document
    {
    // Factory Method implementation
    override public void CreatePages()
    {
    pages.Add( new SkillsPage() );
    pages.Add( new EducationPage() );
    pages.Add( new ExperiencePage() );
    }
    }


    >>http://www.tml.hut.fi/~pnr/GoF-models/html/Abstract-factory.html


    this is an abstractFactory pattern, not a Factory method as your post
    indicates your area of interest lies.

    To see the where the createXxxxx() method is, have a look at the
    AbstractFactory & ConcreteFactory classes.

    >
    >
    > Neither of those use 'create()' as a factory name that I can see.


    see my comments above.

    >
    > However, it is growing on me...


    Great!
    >
    > Chris C


    Andrew
    Andrew McDonagh, Mar 26, 2005
    #9
  10. On Sat, 26 Mar 2005 00:37:18 +0000, Andrew McDonagh
    <2s.com> wrote:

    > Chris Croughton wrote:
    >> On Fri, 25 Mar 2005 23:03:55 +0000, Andrew McDonagh
    >> <2s.com> wrote:
    >>
    >>>Jeff Schwab wrote:
    >>>
    >>>>Chris Croughton wrote:
    >>>>
    >>>>>What do people call their factory functions? 'new' is not an option
    >>>>>(yes, it can be overloaded but has to return void*).
    >>>>
    >>>>>I want a word which implies that what is returned is a pointer to a new
    >>>>>version of the class ('make' and 'create' don't imply that to me)...
    >>>
    >>>I'm confused as to why 'create' does not imply to you that a new
    >>>instance (not version) of a class is instantiated, seeing as its the
    >>>typical name for factory methods and abstract factories.

    >>
    >> Hmm, I couldn't find it as a factory name.
    >>
    >>>see:
    >>>
    >>>http://www.dofactory.com/Patterns/PatternFactory.aspx#_self2

    >
    > For the above url - look at the 'real World example code
    >
    > here's a snippet from it...
    >
    > // "ConcreteCreator"
    >
    > class Resume : Document
    > {
    > // Factory Method implementation
    > override public void CreatePages()


    That's 'createPages()', not 'create()', and the name implies to me that
    it doesn't create a Document or any other class object but creates pages
    within an existing object. Do you call it as

    Document* doc = Resume::CreatePages()

    for example? Or as

    Resume res;
    res.CreatePages();

    to create pages within a resume document? The latter is not what I
    understand as a factory method.

    (I don't know what "override public" does -- what is it, Java? I'm
    writing C++, as in the name of the newsgroup, and I can't find a keyword
    'override' nor that use of 'public' in ISO/IEC 14882-1998.)

    >>>http://www.tml.hut.fi/~pnr/GoF-models/html/Abstract-factory.html

    >
    > this is an abstractFactory pattern, not a Factory method as your post
    > indicates your area of interest lies.
    >
    > To see the where the createXxxxx() method is, have a look at the
    > AbstractFactory & ConcreteFactory classes.


    Again, it's createX() not X::create(). If I wanted that I'd write
    something like

    MyClass *newMyClass(...);

    at global scope, which I have done in the past but it pollutes the
    global namespace (there is no point in having it as part of another
    class).

    >> Neither of those use 'create()' as a factory name that I can see.

    >
    > see my comments above.


    Which confirm that neither of them use create() as a factory function
    name.

    Chris C
    Chris Croughton, Mar 26, 2005
    #10
  11. Chris Croughton wrote:
    > On Sat, 26 Mar 2005 00:37:18 +0000, Andrew McDonagh
    > <2s.com> wrote:
    >
    >
    >>Chris Croughton wrote:
    >>
    >>>On Fri, 25 Mar 2005 23:03:55 +0000, Andrew McDonagh
    >>> <2s.com> wrote:
    >>>
    >>>
    >>>>Jeff Schwab wrote:
    >>>>
    >>>>
    >>>>>Chris Croughton wrote:
    >>>>>
    >>>>>
    >>>>>>What do people call their factory functions? 'new' is not an option
    >>>>>>(yes, it can be overloaded but has to return void*).
    >>>>>
    >>>>>>I want a word which implies that what is returned is a pointer to a new
    >>>>>>version of the class ('make' and 'create' don't imply that to me)...
    >>>>
    >>>>I'm confused as to why 'create' does not imply to you that a new
    >>>>instance (not version) of a class is instantiated, seeing as its the
    >>>>typical name for factory methods and abstract factories.
    >>>
    >>>Hmm, I couldn't find it as a factory name.
    >>>
    >>>
    >>>>see:
    >>>>
    >>>>http://www.dofactory.com/Patterns/PatternFactory.aspx#_self2

    >>
    >>For the above url - look at the 'real World example code
    >>
    >>here's a snippet from it...
    >>
    >>// "ConcreteCreator"
    >>
    >>class Resume : Document
    >>{
    >> // Factory Method implementation
    >> override public void CreatePages()

    >
    >
    > That's 'createPages()', not 'create()', and the name implies to me that
    > it doesn't create a Document or any other class object but creates pages
    > within an existing object.


    In this example usage of 'A' factory method, yes, its creating pages
    within a document.

    As the createPages() factory method is public and defined upon a base
    class, the client code would not need to know that they have a Resume,
    Book, Magazine, Newspaper, etc objects, as the createPages(() method can
    be called polymorphically.


    > Do you call it as
    >
    > Document* doc = Resume::CreatePages()



    In this example no.

    >
    > for example? Or as
    >
    > Resume res;
    > res.CreatePages();


    In this example yes, but like I mention above, it could easily be...

    //client code
    public void HandleDocument(Document& doc)
    {
    doc.CreatePages(); // dont care what type of doc
    ...
    }

    >
    > to create pages within a resume document? The latter is not what I
    > understand as a factory method.


    Either way is good, the above example is just one way of implementing a
    Factory Method.

    You could have a Factory method return an object, but then you are very
    close to what the AbstractFactory pattern is best for.


    >
    > (I don't know what "override public" does -- what is it, Java? I'm
    > writing C++, as in the name of the newsgroup, and I can't find a keyword
    > 'override' nor that use of 'public' in ISO/IEC 14882-1998.)


    It was a C# example code, sorry about that, it was the first example
    implementation that I (well google) found. However, the language aside,
    the pattern characteristics are clearly shown, which is what I was
    looking for.

    >
    >
    >>>>http://www.tml.hut.fi/~pnr/GoF-models/html/Abstract-factory.html

    >>
    >>this is an abstractFactory pattern, not a Factory method as your post
    >>indicates your area of interest lies.
    >>
    >>To see the where the createXxxxx() method is, have a look at the
    >>AbstractFactory & ConcreteFactory classes.

    >


    yes both examples do call the factory methods createBlar(), again sorry,
    first example.

    There is certainly a good argument for simple using 'Create()' as the
    method name, when the method returns an object.

    as in

    MyClass& Create();

    because the return type is telling the reader what the method creates.

    However, when the factory method does not return anything, then
    'CreateBlar()' can be more readable.


    >
    > Again, it's createX() not X::create(). If I wanted that I'd write
    > something like
    >
    > MyClass *newMyClass(...);
    >
    > at global scope, which I have done in the past but it pollutes the
    > global namespace (there is no point in having it as part of another
    > class).
    >
    >
    >>>Neither of those use 'create()' as a factory name that I can see.

    >>
    >>see my comments above.

    >
    >
    > Which confirm that neither of them use create() as a factory function
    > name.
    >
    > Chris C
    Andrew McDonagh, Mar 26, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shoa
    Replies:
    0
    Views:
    4,990
  2. Medi Montaseri
    Replies:
    17
    Views:
    856
    Medi Montaseri
    Sep 3, 2003
  3. Harman
    Replies:
    1
    Views:
    2,504
    Moiristo
    Jul 28, 2006
  4. Replies:
    4
    Views:
    394
    Aurelien RAINONE
    Oct 20, 2006
  5. C#
    Replies:
    4
    Views:
    405
Loading...

Share This Page