What is a Factory in OOP

前端 未结 1 1587
陌清茗
陌清茗 2021-01-12 22:31

My understanding of \"factory-related\" design patterns and their OOP-implementations has always been pretty simple.

  • A \"Factory method\" is a
相关标签:
1条回答
  • 2021-01-12 22:47

    Look at this wiki which says:

    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".[a] More broadly, a subroutine that returns a "new" object may be referred to as a "factory", as in factory method or factory function. This is a basic concept in OOP, and forms the basis for a number of related software design patterns.

    So to answer your questions specifically:

    (1)&(2) Does this mean that a factory is not a class or an object, but a piece of logic?

    No, it means that you can create other objects using an object(factory).

    (2) Is "Factory" not a pattern itself?

    There are different design patterns out of which factory pattern is one. So when you are creating objects using a factory then that patter of creating other objects is "Factory pattern"

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