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
In other words, subclasses are responsible to create the instance of the class. The Factory Method Pattern is also known as Virtual Constructor.
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.
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.
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".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With