Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Naming factory functions

Reply
Thread Tools

Naming factory functions

 
 
Chris Croughton
Guest
Posts: n/a
 
      03-24-2005
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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-24-2005
* 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?
 
Reply With Quote
 
 
 
 
Jeff Schwab
Guest
Posts: n/a
 
      03-24-2005
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?
 
Reply With Quote
 
Chris Croughton
Guest
Posts: n/a
 
      03-25-2005
On Thu, 24 Mar 2005 13:35:53 GMT, Alf P. Steinbach
<(E-Mail Removed)> 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
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-25-2005
* Chris Croughton:
> On Thu, 24 Mar 2005 13:35:53 GMT, Alf P. Steinbach
> <(E-Mail Removed)> 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?
 
Reply With Quote
 
Andrew McDonagh
Guest
Posts: n/a
 
      03-25-2005
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/Pa...ry.aspx#_self2

http://www.tml.hut.fi/~pnr/GoF-model...t-factory.html
 
Reply With Quote
 
Andrew McDonagh
Guest
Posts: n/a
 
      03-25-2005
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.
 
Reply With Quote
 
Chris Croughton
Guest
Posts: n/a
 
      03-25-2005
On Fri, 25 Mar 2005 23:03:55 +0000, Andrew McDonagh
<(E-Mail Removed)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/Pa...ry.aspx#_self2
>
> http://www.tml.hut.fi/~pnr/GoF-model...t-factory.html


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

However, it is growing on me...

Chris C
 
Reply With Quote
 
Andrew McDonagh
Guest
Posts: n/a
 
      03-26-2005
Chris Croughton wrote:
> On Fri, 25 Mar 2005 23:03:55 +0000, Andrew McDonagh
> <(E-Mail Removed)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/Pa...ry.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-model...t-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
 
Reply With Quote
 
Chris Croughton
Guest
Posts: n/a
 
      03-26-2005
On Sat, 26 Mar 2005 00:37:18 +0000, Andrew McDonagh
<(E-Mail Removed)2s.com> wrote:

> Chris Croughton wrote:
>> On Fri, 25 Mar 2005 23:03:55 +0000, Andrew McDonagh
>> <(E-Mail Removed)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/Pa...ry.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-model...t-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract factory and Factory pattern C# ASP .Net 4 07-31-2008 03:22 PM
Can't find property: java.naming.factory.initial joepareti Software 0 01-03-2008 03:02 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
Jboss and java.naming.factory.initial error shoa Java 0 07-26-2005 01:55 AM
Abstract Factory or Factory Method pattern question.... Medi Montaseri C++ 17 09-03-2003 06:50 AM



Advertisments