Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the naming convention for the Factory Method?

Introduction

The MacApp Macintosh application framework [App89] always declares the abstract operation that defines the factory method as Class* DoMakeClass(), where Class is the Product class.

This quote has leaded me to the question about naming conventions for the Factory Method design pattern.

Expectations

I expect to see best practices or helpful examples, which provide clear factory methods naming.

Since it could depend on a language, let's consider a set of the most popular languages: C#, Java, C++ and JavaScript.

Context

For a context I propose to consider the following class structure. We have two abstract classes: Document and Application. The Application contains a list of documents and it should be possible to create a new document. The Application has a factory method CreateDocument().

enter image description here

Thanks

like image 796
Warlock Avatar asked Aug 11 '13 08:08

Warlock


People also ask

What is the other name of factory method?

In other words, subclasses are responsible to create the instance of the class. The Factory Method Pattern is also known as Virtual Constructor.

What should be the naming convention for methods?

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters.

What is Factory Method design pattern?

The factory method is a creational design pattern, i.e., related to object creation. In the Factory pattern, we create objects without exposing the creation logic to the client and the client uses the same common interface to create a new type of object.

What is a factory method in programming?

In object-oriented programming (OOP), a factory is an object for creating other objects – formally a factory is a function or method that returns objects of a varying prototype or class from some method call, which is assumed to be "new".


1 Answers

IMHO, the method name is dependent on context and the nature of the object being created. Maybe this is why you've not found any clear conventions. For example, a Create() method might be right in one context while Open() or Build() might be more appropriate in others.

like image 104
David Osborne Avatar answered Oct 17 '22 21:10

David Osborne