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.
What is the naming convention for the Factory Method?
-
24-06-2022 - |
Question
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()
.
Thanks
Solution
OTHER TIPS
There are no strong conventions related to this design pattern. The best practice would be using of "Do
" or "Make
" prefixes to mark a factory method in some languages, which have not embedded virtual or abstract methods (for example, javascript).