Motivation
Innew
, 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 themselves factories.
Terminology
Terminology differs as to whether the concept of a factory is itself a design pattern – in ''Use
OOP provides polymorphism on object ''use'' by method dispatch, formally subtype polymorphism viacollections.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 itself be a constructor, or any thing that behaves like a constructor – a list
constructor for lists:
_Object_creation
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,_notably_if_complex_allocation_or_initialization_is_desired._Some_of_the_processes_required_in_the_creation_of_an_object_include_determining_which_object_to_create,_managing_the_lifetime_of_the_object,_and_managing_specialized_build-up_and_tear-down_concerns_of_the_object._The_factory_object_might_decide_to_create_the_object's__Object_creation
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,_notably_if_complex_allocation_or_initialization_is_desired._Some_of_the_processes_required_in_the_creation_of_an_object_include_determining_which_object_to_create,_managing_the_lifetime_of_the_object,_and_managing_specialized_build-up_and_tear-down_concerns_of_the_object._The_factory_object_might_decide_to_create_the_object's_class_(computer_science)">classObject creation
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, notably if complex allocation or initialization is desired. Some of the processes required in the creation of an object include determining which object to create, managing the lifetime of the object, and managing specialized build-up and tear-down concerns of the object. The factory object might decide to create the object's class (computer science)">class Class or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used differentl ...Examples
The simplest example of a factory is a simple factory function, which just invokes a constructor and returns the result. In Python, a factory functionf
that instantiates a class A
can be implemented as:
Syntax
Factories may be invoked in various ways, most often a method call (a ''factory method''), sometimes by being called as a function if the factory is a callable object (a ''factory function''). In some languages constructors and factories have identical syntax, while in others constructors have special syntax. In languages where constructors and factories have identical syntax, like Python, Perl, Ruby, Object Pascal, and F#, constructors can be transparently replaced by factories. In languages where they differ, one must distinguish them in interfaces, and switching between constructors and factories requires changing the calls.Semantics
In languages where objects are dynamically allocated, as in Java or Python, factories are semantically equivalent to constructors. However, in languages such as C++ that allow some objects to be statically allocated, factories are different from constructors for statically allocated classes, as the latter can have memory allocation determined at compile time, while allocation of the return values of factories must be determined at run time. If a constructor can be passed as an argument to a function, then invocation of the constructor and allocation of the return value must be done dynamically at run time, and thus have similar or identical semantics to invoking a factory.Design patterns
Factories are used in variousApplications
Factory objects are common inApplicability
Factories can be used when: # The creation of an object makes reuse impossible without significant duplication of code. # The creation of an object requires access to information or resources that should not be contained within the composing class. # The lifetime management of the generated objects must be centralized to ensure a consistent behavior within the application. Factories, specifically factory methods, are common inFoo
creates another object Dangerous
that can't be put under automated Dangerous
objects is placed in the virtual factory method createDangerous
in class Foo
. For testing, TestFoo
(a subclass of Foo
) is then created, with the virtual factory method createDangerous
overridden to create and return FakeDangerous
, a TestFoo
to test the functionality of Foo
without incurring the side effect of using a real Dangerous
object.
Benefits and variants
Besides use in design patterns, factories, especially factory methods, have various benefits and variations.Descriptive names
A factory method has a distinct name. In many object-oriented languages, constructors must have the same name as the class they are in, which can lead to ambiguity if there is more than one way to create an object (see overloading). Factory methods have no such constraint and can have descriptive names; these are sometimes known as alternative constructors. As an example, whenEncapsulation
Factory methods encapsulate the creation of objects. This can be useful if the creation process is very complex; for example, if it depends on settings in configuration files or on user input. Consider as an example a program that readsJava
PHP
Limitations
There are three limitations associated with the use of the factory method. The first relates toStrangeComplex
extends Complex
, then unless StrangeComplex
provides its own version of all factory methods, the call Complex
(the superclass) rather than the expected instance of the subclass. The Notes
References
* {{refend Object-oriented programming Articles with example C Sharp code Articles with example Java code Articles with example PHP code zh:工厂方法#工厂