Factory object
Encyclopedia
In object-oriented computer programming
, a factory is an object
for creating other objects. It is an abstraction
of a constructor
, and can be used to implement various allocation schemes. For example, using this definition, singletons implemented by the singleton pattern
are formal factories.
A factory object typically has a method
for every kind of object it is capable of creating. These methods optionally accept parameter
s defining how the object is created, and then return the created object.
Factory objects are used in situations where getting hold of an object of a particular kind is a more complex process than simply creating a new object. The factory object might decide to create the object's class
(if applicable) dynamically, return it from an object pool
, do complex configuration on the object, or other things.
These kinds of objects have proven useful and several design pattern
s have been developed to implement them in many languages. For example, several "GoF patterns", like the "Factory method pattern
", the "Builder
" or even the "Singleton" are implementations of this concept. The "Abstract factory pattern
" instead is a method to build collections of factories.
s and frameworks
where library code needs to create objects of types which may be subclassed by applications using the framework. They are also used in test-driven development
to allow classes to be put under test.
Factories determine the actual concrete type of object
to be created, and it is here that the object is actually created. As the factory only returns an abstract pointer, the client code does not know - and is not burdened by - the actual concrete type of the object which was just created. However, the type of a concrete object is known by the abstract factory. In particular, this means:
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...
, a factory is an object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...
for creating other objects. It is an abstraction
Abstraction (computer science)
In computer science, abstraction is the process by which data and programs are defined with a representation similar to its pictorial meaning as rooted in the more complex realm of human life and language with their higher need of summarization and categorization , while hiding away the...
of a constructor
Constructor (computer science)
In object-oriented programming, a constructor in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created...
, and can be used to implement various allocation schemes. For example, using this definition, singletons implemented by the singleton pattern
Singleton pattern
In software engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system...
are formal factories.
A factory object typically has a method
Method (computer science)
In object-oriented programming, a method is a subroutine associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time...
for every kind of object it is capable of creating. These methods optionally accept parameter
Parameter
Parameter from Ancient Greek παρά also “para” meaning “beside, subsidiary” and μέτρον also “metron” meaning “measure”, can be interpreted in mathematics, logic, linguistics, environmental science and other disciplines....
s defining how the object is created, and then return the created object.
Factory objects are used in situations where getting hold of an object of a particular kind is a more complex process than simply creating a new object. The factory object might decide to create the object's class
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...
(if applicable) dynamically, return it from an object pool
Object pool
In computer programming, an object pool is a software design pattern. An object pool is a set of initialised objects that are kept ready to use, rather than allocated and destroyed on demand. A client of the pool will request an object from the pool and perform operations on the returned object...
, do complex configuration on the object, or other things.
These kinds of objects have proven useful and several design pattern
Design pattern (computer science)
In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that...
s have been developed to implement them in many languages. For example, several "GoF patterns", like the "Factory method pattern
Factory method pattern
The factory method pattern is an object-oriented design pattern to implement the concept of factories. Like other creational patterns, it deals with the problem of creating objects without specifying the exact class of object that will be created.The creation of an object often requires complex...
", the "Builder
Builder pattern
The builder pattern is an object creation software design pattern. The intention is to abstract steps of construction of objects so that different implementations of these steps can construct different representations of objects...
" or even the "Singleton" are implementations of this concept. The "Abstract factory pattern
Abstract factory pattern
The abstract factory pattern is a software design pattern that provides a way to encapsulate a group of individual factories that have a common theme. In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interfaces to create the...
" instead is a method to build collections of factories.
Usage
Factory objects are common in toolkitToolkit
A toolkit is an assembly of tools; set of basic building units for graphical user interfaces.Things called toolkits include:* Abstract Window Toolkit* Accessibility Toolkit* Adventure Game Toolkit* B-Toolkit* Battlefield Mod Development Toolkit...
s and frameworks
Software framework
In computer programming, a software framework is an abstraction in which software providing generic functionality can be selectively changed by user code, thus providing application specific software...
where library code needs to create objects of types which may be subclassed by applications using the framework. They are also used in test-driven development
Test-driven development
Test-driven development is a software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new...
to allow classes to be put under test.
Factories determine the actual concrete type of object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...
to be created, and it is here that the object is actually created. As the factory only returns an abstract pointer, the client code does not know - and is not burdened by - the actual concrete type of the object which was just created. However, the type of a concrete object is known by the abstract factory. In particular, this means:
- The client code has no knowledge whatsoever of the concreteConcrete (disambiguation)Concrete is a composite building material made from the combination of aggregate and cement binder.Concrete may also refer to:*Reinforced concrete, also called ferroconcrete in some countries, is concrete with embedded reinforcement...
typeData typeIn computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...
, not needing to include any header fileHeader fileSome programming languages use header files. These files allow programmers to separate certain elements of a program's source code into reusable files. Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers...
s or classClass (computer science)In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...
declarationDeclaration (computer science)In programming languages, a declaration specifies the identifier, type, and other aspects of language elements such as variables and functions. It is used to announce the existence of the element to the compiler; this is important in many strongly-typed languages that require variables and their...
s relating to the concrete type. The client code deals only with the abstract type. Objects of a concrete type are indeed created by the factory, but the client code accesses such objects only through their abstract interface. - Adding new concrete types is done by modifying the client code to use a different factory, a modification which is typically one line in one file. This is significantly easier than modifying the client code to instantiate a new type, which would require changing every location in the code where a new object is created.