Consider providing static factory methods insteads of constructors

后端 未结 5 1285
清酒与你
清酒与你 2020-12-06 08:38

The normal way for a class to allow a client to obtain an instance is to provide a public contructor. Another way to do that is providing a public static factory method, whi

5条回答
  •  有刺的猬
    2020-12-06 09:07

    I would say to the original author of the question that static factory methods are a tool. Like all tools, they have uses for which they are best suited, other uses for which they are passable, and other things for which they are poorly adaptable. To cite a real world example, a hammer is great at driving nails, adequate for jacking open a sealed crate with the nail-removing end (a crowbar would still be much better), but useless for planing down a rough surface.

    Factory methods refer to one of a set of creational design patterns, i.e. paradigms for creating objects. In some creational design patterns such as "Builder" and "Prototype", the use of the new operator for object creation isn't merely discouraged, it's considered harmful to the overall design goal. The creational design patterns that people talk about are...

    1. Factory methods
    2. Abstract factory methods
    3. Singleton pattern
    4. Builder
    5. Prototype

    Generally speaking, a factory method is used to create an object from a group of related subclasses based on data that the user or designer supplies to the method. More concretely, though, a static factory method gives you control over object creation even when the object that is returned is the same every time. This can be very important when, for example, the process of creating an object is very expensive in terms of time and resources. In a situation like this, using a new operator to create objects might incur a terrible performance penalty.

    One solution might be to maintain a reusable pool of objects. By using a static factory method, the application designer can provide logic to return a free, existing object if one is available. This would then save the potentially high cost of building a new object. This is exactly what is done with network database connections by connection managers that provide "connection pooling." Rather than build a new database connection every single time a client makes a request, connection objects are assigned from a pool of existing objects if one is available. This is an example of a circumstance in which using the new operator would actually be harmful to application performance and would undermine the software engineer's design goals.

    A good time to consider using a factory method for creating objects rather than the new operator would be:

    • The object that would be created is belongs to one of a few possible subclasses of objects that could be created depending on supplied data.
    • There is a good reason to have more control over the object creation process than would be possible in a constructor, eg. the Singleton design pattern requires a static factory method.

    A bad time to consider using a factory method would be:

    • Simple, lightweight objects

    It's all about enumerating the software design problems, and then deciding which tools are best for solving it. Static factory methods are good for some things and not so good for others ... just like any tool.

提交回复
热议问题