How to name factory like methods?

后端 未结 11 1880
别跟我提以往
别跟我提以往 2021-01-29 19:41

I guess that most factory-like methods start with create. But why are they called \"create\"? Why not \"make\", \"produce\", \"build\", \"generate\" or something el

相关标签:
11条回答
  • 2021-01-29 20:24

    Factory method doesn't dictate on method name. You can have as many methods you want in your factory, provided all of them return the object from same family.

    For more details, visit the url http://xeon2k.wordpress.com

    0 讨论(0)
  • 2021-01-29 20:27

    Some random thoughts:

    • 'Create' fits the feature better than most other words. The next best word I can think of off the top of my head is 'Construct'. In the past, 'Alloc' (allocate) might have been used in similar situations, reflecting the greater emphasis on blocks of data than objects in languages like C.

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

    • 'Construct' is close, but it is usually used to describe a specific stage in the process of creating an object (allocate/new, construct, initialise...)

    • 'Build' and 'Make' are common terms for processes relating to compiling code, so have different connotations to programmers, implying a process that comprises many steps and possibly a lot of disk activity. However, the idea of a Factory "building" something is a sensible idea - especially in cases where a complex data-structure is built, or many separate pieces of information are combined in some way.

    • 'Generate' to me implies a calculation which is used to produce a value from an input, such as generating a hash code or a random number.

    • 'Produce', 'Generate', 'Construct' are longer to type/read than 'Create'. Historically programmers have favoured short names to reduce typing/reading.

    0 讨论(0)
  • I think it stems from “to create an object”. However, in English, the word “create” is associated with the notion “to cause to come into being, as something unique that would not naturally evolve or that is not made by ordinary processes,” and “to evolve from one's own thought or imagination, as a work of art or an invention.” So it seems as “create” is not the proper word to use. “Make,” on the other hand, means “to bring into existence by shaping or changing material, combining parts, etc.” For example, you don’t create a dress, you make a dress (object). So, in my opinion, “make” by meaning “to produce; cause to exist or happen; bring about” is a far better word for factory methods.

    0 讨论(0)
  • 2021-01-29 20:30

    Personally I like instantiate and instantiateWith, but that's just because of my Unity and Objective C experiences. Naming conventions inside the Unity engine seem to revolve around the word instantiate to create an instance via a factory method, and Objective C seems to like with to indicate what the parameter/s are. This only really works well if the method is in the class that is going to be instantiated though (and in languages that allow constructor overloading, this isn't so much of a 'thing').

    Just plain old Objective C's initWith is also a good'un!

    0 讨论(0)
  • 2021-01-29 20:32

    I'd call it UriFactory.Create()

    Where,

    UriFactory is the name of the class type which is provides method(s) that create Uri instances.

    and Create() method is overloaded for as many as variations you have in your specs.

    public static class UriFactory
    {
        //Default Creator
        public static UriType Create() 
        {
        }
    
        //An overload for Create()
        public static UriType Create(someArgs) 
        {
        }
    }
    
    0 讨论(0)
  • 2021-01-29 20:34

    I like new. To me

    var foo = newFoo();
    

    reads better than

    var foo = createFoo();
    

    Translated to english we have foo is a new foo or foo is create foo. While I'm not a grammer expert I'm pretty sure the latter is grammatically incorrect.

    0 讨论(0)
提交回复
热议问题