Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Factory in OOP

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

  • A "Factory method" is a method inside a class that has an interface (or an abstract class) as a return type and constructs objects implementing this interface based on some internal logic.
  • A "Factory" is a class that only contains factory methods
  • An "Abstract factory" is an interface (or an abstract class) that only contains factory methods

But I recently stumbled upon Wikipeda articles on the subject (Factory, Abstract factory) that made me somewhat confused, especially about what a "Factory" is in OOP.

Here are several quotes:

  1. A subroutine that returns a "new" object may be referred to as a "factory", as in factory method or factory function.
  2. Factories are used in various design patterns
  3. The "Abstract factory pattern" is a method to build collections of factories.
  4. A factory is the location of a concrete class in the code at which objects are constructed

which arouse some questions:

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

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

(3) What does "collection" mean here? Is it just a way of saying "you can have several factories that implement the same interface (which is an abstract factory)"?

(4) What???

Can anyone clarify what this means? Is my initial understanding of factories incorrect?

like image 687
Andre Borges Avatar asked Mar 03 '16 07:03

Andre Borges


People also ask

What is a factory function?

A factory function can be defined as a function that creates an object and returns it. It is similar to constructor functions/class functions. The factory function is a very useful tool in JavaScript since it returns the object of any class directly.

What is a factory in C++?

A factory method is a static method of a class that returns an object of that class' type. But unlike a constructor, the actual object it returns might be an instance of a subclass. Another advantage of a factory method is that it can return existing instances multiple times.

What is the use of an object factory?

Typically, an object factory is quite simple and small. Its main role is to collect the information necessary to create an instance of the intended object's class and then to invoke that class's constructor. However, the complexity of the objects that it creates can vary significantly.

Which is an object factory?

An object factory is a producer of objects. It accepts some information about how to create an object, such as a reference, and then returns an instance of that object.


1 Answers

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"

like image 119
Rahul Tripathi Avatar answered Sep 30 '22 14:09

Rahul Tripathi