473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Naming factory functions

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
10 1862
* 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
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
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
* 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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Shemp | last post by:
We are having a debate in our organization about naming conventions for abstract classes. The three proposed methods are: 1) Prefix the class name with abstract. Example: AbstractUser 2)...
39
by: Marco Aschwanden | last post by:
Hi I don't have to talk about the beauty of Python and its clear and readable syntax... but there are a few things that striked me while learning Python. I have collected those thoughts. I am...
17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
6
by: Dave | last post by:
Hello all, Please see my question embedded in comment form below. Thanks, Dave #include <iostream> #include <boost/shared_ptr.hpp>
4
by: anonymous.user0 | last post by:
Using the dotnet v1.1 framework (so no generics possible). I'd like to create a bunch of Factory classes that all inherit from a single abstract base Factory class. Each Factory is responsible...
5
by: ma740988 | last post by:
Consider: #include "handyfactory.h" #include <iostream> struct Shape { virtual void print() const=0; };
7
by: Steven T. Hatton | last post by:
I have a couple questions about the design pattern presented in the example quoted below. I can appreciate why the destructor is protected, but why is it not virtual? I am forced to assume that I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.