Naming factory functions

C

Chris Croughton

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
 
A

Alf P. Steinbach

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

Jeff Schwab

Chris said:
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?
 
C

Chris Croughton

* 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
 
A

Alf P. Steinbach

* Chris Croughton:
* 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.

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


'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();
 
C

Chris Croughton

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.

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

However, it is growing on me...

Chris C
 
A

Andrew McDonagh

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

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() );
}
}


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
 
C

Chris Croughton

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.)
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).
see my comments above.

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

Chris C
 
A

Andrew McDonagh

Chris said:
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.

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top