In simplest terms, what is a factory?

后端 未结 6 1522
南方客
南方客 2020-11-30 05:57

What is a factory and why would I want to use one?

6条回答
  •  萌比男神i
    2020-11-30 06:09

    In simple terms, Factory is an OO design pattern that deals with creating objects without specifying the exact class of object that is to be created.

    A good reason to use it is well defined in wikipedia:

    The creation of an object often requires complex processes not appropriate to include within a composing object. The object's creation may lead to a significant duplication of code, may require information not accessible to the composing object, may not provide a sufficient level of abstraction, or may otherwise not be part of the composing object's concerns. The factory method design pattern handles these problems by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created.

提交回复
热议问题