Factory (object-oriented programming)
Factory (object-oriented programming)
Main page
hub-image

Factory (object-oriented programming)

logo
Community Hub0 subscribers
What are your thoughts?
Be the first to start a discussion here.
Be the first to start a discussion here.
Factory (object-oriented programming)

In object-oriented programming, a factory is an object for creating other objects; formally, it is a function or method that returns objects of a varying prototype or class from some method call, which is assumed to be new. More broadly, a subroutine that returns a new object may be referred to as a factory, as in factory method or factory function. The factory pattern is the basis for a number of related software design patterns.

In class-based programming, a factory is an abstraction of a constructor of a class, while in prototype-based programming a factory is an abstraction of a prototype object. A constructor is concrete in that it creates objects as instances of one class, and by a specified process (class instantiation), while a factory can create objects by instantiating various classes, or by using other allocation means, such as an object pool. A prototype object is concrete in that it is used to create objects by being cloned, while a factory can create objects by cloning various prototypes, or by other allocation means.

A factory may be implemented in various ways. Most often it is implemented as a method, in which case it is called a factory method. Sometimes it is implemented as a function, in which case it is called a factory function. In some languages, constructors are factories. However, in most languages they are not, and constructors are invoked in a way that is idiomatic to the language, such as by using the keyword new, while a factory has no special status and is invoked via an ordinary method call or function call. In these languages, a factory is an abstraction of a constructor, but not strictly a generalization, as constructors are not factories.

Terminology differs as to whether the concept of a factory is a design pattern – in Design Patterns there is no factory pattern, but instead two patterns (factory method pattern and abstract factory pattern) that use factories. Some sources refer to the concept as the factory pattern, while others consider the concept a programming idiom, reserving the term factory pattern or factory patterns to more complicated patterns that use factories, most often the factory method pattern; in this context, the concept of a factory may be referred to as a simple factory. In other contexts, particularly the Python language, the term factory is used, as in this article. More broadly, factory may be applied not just to an object that returns objects from some method call, but to a subroutine that returns objects, as in a factory function (even if functions are not objects) or factory method. Because in many languages factories are invoked by calling a method, the general concept of a factory is often confused with the specific factory method pattern design pattern.

OOP provides polymorphism on object use by method dispatch, formally subtype polymorphism via single dispatch determined by the type of the object on which the method is called. However, this does not work for constructors, as constructors create an object of some type, rather than use an existing object. More concretely, when a constructor is called, there is no object yet on which to dispatch.

Using factories instead of constructors or prototypes allows one to use polymorphism for object creation, not only object use. Specifically, using factories provides encapsulation, and means the code is not tied to specific classes or objects, and thus the class hierarchy or prototypes can be changed or refactored without needing to change code that uses them – they abstract from the class hierarchy or prototypes.

More technically, in languages where factories generalize constructors, factories can usually be used anywhere constructors can be, meaning that interfaces that accept a constructor can also in general accept a factory – usually one only need something that creates an object, rather than needing to specify a class and instantiation.

For example, in Python, the collections.defaultdict class has a constructor which creates an object of type defaultdict whose default values are produced by invoking a factory. The factory is passed as an argument to the constructor, and can be a constructor, or any thing that behaves like a constructor – a callable object that returns an object, i.e., a factory. For example, using the list constructor for lists:

See all
User Avatar
No comments yet.