How to name factory similar methods?

I assume that most of these factory methods start with create . But why are they called " create "? Why not make , produce , build , generate , or something else? Is it just a matter of taste? Agreement? Or is there a special meaning in "create"?

 createURI(...) makeURI(...) produceURI(...) buildURI(...) generateURI(...) 

Which one would you choose at all and why?

+84
methods coding-style naming-conventions factory
Jul 30 '10 at 5:10
source share
10 answers

Some random thoughts:

  • "Create" is better suited for this function than most other words. The next best word I can think of from my head is "Construct". In the past, “Alloc” (allocate) may have been used in situations like this, reflecting a greater emphasis on data blocks than objects in languages ​​like C.

  • "Create" is a short, simple word that has a clear intuitive meaning. In most cases, people probably just choose it as the first, most obvious word that comes to mind when they want to create something. This is a common naming convention, and “object creation” is a general way of describing the process of creating objects.

  • The "construction" is close, but it is usually used to describe a certain stage in the process of creating an object (highlight / new, construct, initialize ...)

  • "Build" and "Make" are general terms for processes related to compiling code, therefore they have different connotations for programmers, implying a process that includes many steps and possibly a lot of disk activity. However, the idea of ​​creating "w770" "something" is a reasonable idea - especially in those cases when a complex data structure is built, or many individual pieces of information are somehow combined.

  • "Generate" for me means a calculation that is used to get a value from an input, for example, generating a hash code or a random number.

  • 'Produce', 'Generate', 'Construct' is longer than the / read type than 'Create'. Historically, programmers have preferred short names to reduce input / read.

+68
Jul 30 '10 at 5:27
source share

Joshua Bloch in Effective Java offers the following naming conventions

valueOf . Returns an instance that, of course, has the same value as its parameters. Such static plants are efficient type conversion methods.

- A brief alternative to valueOf popularized by EnumSet (clause 32).

getInstance - returns an instance that is described by parameters but it cannot be said that they have the same value. In the case of a singleton, getInstance takes no parameters and returns a single instance.

newInstance . Like getInstance, except that newInstance ensures that every instance returned is different from everyone else.

getType . Like getInstance, but used when the factory method is in another class. The type indicates the type of object returned by the factory.

newType . Like newInstance, but used when the factory method is in another class. The type indicates the type of object returned by the factory.

+52
Mar 16 '14 at 19:52
source share

I wanted to add a couple of points, which I do not see in other answers.

  • Although traditionally Factory means "creates objects," I like to think about it more broadly because "returns me an object that behaves as I expect." I should not always know if this is a completely new object; in fact, it may not matter to me. Therefore, in appropriate cases, you can avoid the name "Create ...", even if it will be implemented right now.

  • Guava is a good repository of factory naming ideas. He popularizes the beautiful style of DSL. examples:

     Lists.newArrayListWithCapacity(100); ImmutableList.of("Hello", "World"); 
+15
Dec 05
source share

“Create” and “make” are short, reasonably memorable and not tied to other patterns in the title that I can think of. I have also seen quite often, and I suspect that they may be "de facto standards." I would choose one and use it consistently, at least as part of the project. (Looking at my own current project, I seem to be using “make.” I hope I agree ...)

Avoid "build" because it is better suited for the Builder pattern and avoid "produce" because it calls the manufacturer / consumer.

To really continue the metaphor of the name "Factory" for the template, I will be tempted by "production", but that's too long a word.

+7
Jul 31 '10 at 16:51
source share

I think this is due to the "creation of the object." However, in English, the word "create" is associated with the concept of "make it emerge as something unique that naturally would not have evolved or which was not done by ordinary processes", and "develop from one's own thought or imagination, like a work of art or invention." Therefore, it seems that “create” is not the right word to use. To do, on the other hand, means to lead to the emergence by forming or modifying a material, combining parts, etc. Thus, in my opinion, "to make" within the meaning of "produce, cause existence, or occur, cause" is a much better word for factory methods.

+3
Feb 08 '13 at 21:23
source share

Partial agreement, partially semantics.

Methods

Factory (signaled by traditional create ) must invoke the appropriate constructors. If I saw buildURI , I would suggest that this is due to some computation or assembly from parts (and I don’t think the factory was involved). The first thing I thought when I saw generateURI was to do something random, like a new personalized download link. They are not all the same, different words cause different meanings; but most of them are not classified.

+2
Jul 30 '10 at 5:26
source share

I would call it UriFactory.Create()

Where

UriFactory is a class type name that provides methods (methods) that create instances of Uri .

and the Create() method is overloaded just like the variations that you have in your specifications.

 public static class UriFactory { //Default Creator public static UriType Create() { } //An overload for Create() public static UriType Create(someArgs) { } } 
+2
Jul 30 '10 at 5:31
source share

I would note that I saw all the verbs, but created them in some library or another, so I would not call creation a universal agreement.

Now the creation is better for me, provokes the exact meaning of the action.

So yes, this is a question (literary).

0
Jul 30 '10 at 6:09
source share

Personally, I like instantiate and instantiateWith , but that's just because of my unity and experience with Objective C. The naming conventions inside the Unity engine seem to revolve around the word instantiate to create an instance using the factory method, and Objective C seems to be with so that indicate what the / s option is. This really works well if the method is in the class to be created (although in languages ​​that allow constructor overloading, this is not so much a “thing”).

Just the old Objective-C initWith object is also good!

0
Aug 6 '15 at 20:32
source share

Factory method does not dictate the name of the method. In your factory, you can use as many methods if they all return an object from the same family.

For more information, visit the URL http://xeon2k.wordpress.com

-2
Nov 27 2018-10-11T00:
source share



All Articles