By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Naming factory functions

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
* 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?
Jul 23 '05 #2

P: n/a
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?
Jul 23 '05 #3

P: n/a
On Thu, 24 Mar 2005 13:35:53 GMT, Alf P. Steinbach
<al***@start.no> 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
Jul 23 '05 #4

P: n/a
* Chris Croughton:
On Thu, 24 Mar 2005 13:35:53 GMT, Alf P. Steinbach
<al***@start.no> 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?
Jul 23 '05 #5

P: n/a
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
Jul 23 '05 #6

P: n/a
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.
Jul 23 '05 #7

P: n/a
On Fri, 25 Mar 2005 23:03:55 +0000, Andrew McDonagh
<ne**@andrewcdonagh.f2s.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
Jul 23 '05 #8

P: n/a
Chris Croughton wrote:
On Fri, 25 Mar 2005 23:03:55 +0000, Andrew McDonagh
<ne**@andrewcdonagh.f2s.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
Jul 23 '05 #9

P: n/a
On Sat, 26 Mar 2005 00:37:18 +0000, Andrew McDonagh
<ne**@andrewcdonagh.f2s.com> wrote:
Chris Croughton wrote:
On Fri, 25 Mar 2005 23:03:55 +0000, Andrew McDonagh
<ne**@andrewcdonagh.f2s.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
Jul 23 '05 #10

P: n/a
Chris Croughton wrote:
On Sat, 26 Mar 2005 00:37:18 +0000, Andrew McDonagh
<ne**@andrewcdonagh.f2s.com> wrote:

Chris Croughton wrote:
On Fri, 25 Mar 2005 23:03:55 +0000, Andrew McDonagh
<ne**@andrewcdonagh.f2s.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.


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-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.


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

Jul 23 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.