<s>
In	O
object-oriented	B-Language
programming	I-Language
,	O
a	O
factory	O
is	O
an	O
object	O
for	O
creating	O
other	O
objects	O
;	O
formally	O
,	O
it	O
is	O
a	O
function	O
or	O
method	B-Language
that	O
returns	O
objects	O
of	O
a	O
varying	O
prototype	O
or	O
class	O
from	O
some	O
method	B-Language
call	I-Language
,	O
which	O
is	O
assumed	O
to	O
be	O
"	O
new	O
"	O
.	O
</s>
<s>
More	O
broadly	O
,	O
a	O
subroutine	O
that	O
returns	O
a	O
"	O
new	O
"	O
object	O
may	O
be	O
referred	O
to	O
as	O
a	O
"	O
factory	O
"	O
,	O
as	O
in	O
factory	B-Language
method	I-Language
or	O
factory	B-Application
function	I-Application
.	O
</s>
<s>
The	O
factory	B-Language
pattern	I-Language
is	O
the	O
basis	O
for	O
a	O
number	O
of	O
related	O
software	O
design	O
patterns	O
.	O
</s>
<s>
In	O
class-based	B-Application
programming	I-Application
,	O
a	O
factory	O
is	O
an	O
abstraction	B-Application
of	O
a	O
constructor	O
of	O
a	O
class	O
,	O
while	O
in	O
prototype-based	B-Application
programming	I-Application
a	O
factory	O
is	O
an	O
abstraction	B-Application
of	O
a	O
prototype	O
object	O
.	O
</s>
<s>
A	O
prototype	O
object	O
is	O
concrete	O
in	O
that	O
it	O
is	O
used	O
to	O
create	O
objects	O
by	O
being	O
cloned	B-Application
,	O
while	O
a	O
factory	O
can	O
create	O
objects	O
by	O
cloning	B-Application
various	O
prototypes	O
,	O
or	O
by	O
other	O
allocation	O
schemes	O
.	O
</s>
<s>
Most	O
often	O
it	O
is	O
implemented	O
as	O
a	O
method	B-Language
,	O
in	O
which	O
case	O
it	O
is	O
called	O
a	O
factory	B-Language
method	I-Language
.	O
</s>
<s>
Sometimes	O
it	O
is	O
implemented	O
as	O
a	O
function	O
,	O
in	O
which	O
case	O
it	O
is	O
called	O
a	O
factory	B-Application
function	I-Application
.	O
</s>
<s>
However	O
,	O
in	O
most	O
languages	O
they	O
are	O
not	O
,	O
and	O
constructors	O
are	O
invoked	O
in	O
a	O
way	O
that	O
is	O
idiomatic	O
to	O
the	O
language	O
,	O
such	O
as	O
by	O
using	O
the	O
keyword	O
new	O
,	O
while	O
a	O
factory	O
has	O
no	O
special	O
status	O
and	O
is	O
invoked	O
via	O
an	O
ordinary	O
method	B-Language
call	I-Language
or	O
function	O
call	O
.	O
</s>
<s>
In	O
these	O
languages	O
,	O
a	O
factory	O
is	O
an	O
abstraction	B-Application
of	O
a	O
constructor	O
,	O
but	O
not	O
strictly	O
a	O
generalization	O
,	O
as	O
constructors	O
are	O
not	O
themselves	O
factories	O
.	O
</s>
<s>
Terminology	O
differs	O
as	O
to	O
whether	O
the	O
concept	O
of	O
a	O
factory	O
is	O
itself	O
a	O
design	O
pattern	O
–	O
in	O
Design	O
Patterns	O
there	O
is	O
no	O
"	O
factory	B-Language
pattern	I-Language
"	O
,	O
but	O
instead	O
two	O
patterns	O
(	O
factory	B-Language
method	I-Language
pattern	I-Language
and	O
abstract	B-Language
factory	I-Language
pattern	I-Language
)	O
that	O
use	O
factories	O
.	O
</s>
<s>
Some	O
sources	O
refer	O
to	O
the	O
concept	O
as	O
the	O
factory	B-Language
pattern	I-Language
,	O
while	O
others	O
consider	O
the	O
concept	O
itself	O
a	O
programming	O
idiom	O
,	O
reserving	O
the	O
term	O
"	O
factory	B-Language
pattern	I-Language
"	O
or	O
"	O
factory	B-Language
patterns	I-Language
"	O
to	O
more	O
complicated	O
patterns	O
that	O
use	O
factories	O
,	O
most	O
often	O
the	O
factory	B-Language
method	I-Language
pattern	I-Language
;	O
in	O
this	O
context	O
,	O
the	O
concept	O
of	O
a	O
factory	O
itself	O
may	O
be	O
referred	O
to	O
as	O
a	O
simple	O
factory	O
.	O
</s>
<s>
More	O
broadly	O
,	O
"	O
factory	O
"	O
may	O
be	O
applied	O
not	O
just	O
to	O
an	O
object	O
that	O
returns	O
objects	O
from	O
some	O
method	B-Language
call	I-Language
,	O
but	O
to	O
a	O
subroutine	O
that	O
returns	O
objects	O
,	O
as	O
in	O
a	O
factory	B-Application
function	I-Application
(	O
even	O
if	O
functions	O
are	O
not	O
objects	O
)	O
or	O
factory	B-Language
method	I-Language
.	O
</s>
<s>
Because	O
in	O
many	O
languages	O
factories	O
are	O
invoked	O
by	O
calling	O
a	O
method	B-Language
,	O
the	O
general	O
concept	O
of	O
a	O
factory	O
is	O
often	O
confused	O
with	O
the	O
specific	O
factory	B-Language
method	I-Language
pattern	I-Language
design	O
pattern	O
.	O
</s>
<s>
OOP	O
provides	O
polymorphism	B-Application
on	O
object	O
use	O
by	O
method	B-Language
dispatch	O
,	O
formally	O
subtype	O
polymorphism	B-Application
via	O
single	O
dispatch	O
determined	O
by	O
the	O
type	O
of	O
the	O
object	O
on	O
which	O
the	O
method	B-Language
is	O
called	O
.	O
</s>
<s>
Using	O
factories	O
instead	O
of	O
constructors	O
or	O
prototypes	O
allows	O
one	O
to	O
use	O
polymorphism	B-Application
for	O
object	O
creation	O
,	O
not	O
only	O
object	O
use	O
.	O
</s>
<s>
Specifically	O
,	O
using	O
factories	O
provides	O
encapsulation	B-Application
,	O
and	O
means	O
the	O
code	O
is	O
not	O
tied	O
to	O
specific	O
classes	O
or	O
objects	O
,	O
and	O
thus	O
the	O
class	O
hierarchy	O
or	O
prototypes	O
can	O
be	O
changed	O
or	O
refactored	O
without	O
needing	O
to	O
change	O
code	O
that	O
uses	O
them	O
–	O
they	O
abstract	O
from	O
the	O
class	O
hierarchy	O
or	O
prototypes	O
.	O
</s>
<s>
Factory	B-Application
objects	I-Application
are	O
used	O
in	O
situations	O
where	O
getting	O
hold	O
of	O
an	O
object	O
of	O
a	O
particular	O
kind	O
is	O
a	O
more	O
complex	O
process	O
than	O
simply	O
creating	O
a	O
new	O
object	O
,	O
notably	O
if	O
complex	O
allocation	O
or	O
initialization	O
is	O
desired	O
.	O
</s>
<s>
The	O
factory	B-Application
object	I-Application
might	O
decide	O
to	O
create	O
the	O
object	O
's	O
class	O
(	O
if	O
applicable	O
)	O
dynamically	O
,	O
return	O
it	O
from	O
an	O
object	O
pool	O
,	O
do	O
complex	O
configuration	O
on	O
the	O
object	O
,	O
or	O
other	O
things	O
.	O
</s>
<s>
The	O
simplest	O
example	O
of	O
a	O
factory	O
is	O
a	O
simple	O
factory	B-Application
function	I-Application
,	O
which	O
just	O
invokes	O
a	O
constructor	O
and	O
returns	O
the	O
result	O
.	O
</s>
<s>
In	O
Python	O
,	O
a	O
factory	B-Application
function	I-Application
f	O
that	O
instantiates	O
a	O
class	O
A	O
can	O
be	O
implemented	O
as	O
:	O
</s>
<s>
A	O
simple	O
factory	B-Application
function	I-Application
implementing	O
the	O
singleton	O
pattern	O
is	O
:	O
</s>
<s>
Factories	O
may	O
be	O
invoked	O
in	O
various	O
ways	O
,	O
most	O
often	O
a	O
method	B-Language
call	I-Language
(	O
a	O
factory	B-Language
method	I-Language
)	O
,	O
sometimes	O
by	O
being	O
called	O
as	O
a	O
function	O
if	O
the	O
factory	O
is	O
a	O
callable	O
object	O
(	O
a	O
factory	B-Application
function	I-Application
)	O
.	O
</s>
<s>
In	O
languages	O
where	O
objects	O
are	O
dynamically	B-General_Concept
allocated	I-General_Concept
,	O
as	O
in	O
Java	O
or	O
Python	O
,	O
factories	O
are	O
semantically	O
equivalent	O
to	O
constructors	O
.	O
</s>
<s>
However	O
,	O
in	O
languages	O
such	O
as	O
C++	O
that	O
allow	O
some	O
objects	O
to	O
be	O
statically	O
allocated	O
,	O
factories	O
are	O
different	O
from	O
constructors	O
for	O
statically	O
allocated	O
classes	O
,	O
as	O
the	O
latter	O
can	O
have	O
memory	B-General_Concept
allocation	I-General_Concept
determined	O
at	O
compile	O
time	O
,	O
while	O
allocation	O
of	O
the	O
return	O
values	O
of	O
factories	O
must	O
be	O
determined	O
at	O
run	O
time	O
.	O
</s>
<s>
For	O
example	O
,	O
several	O
"	O
GoF	O
patterns	O
"	O
,	O
like	O
the	O
"	O
Factory	B-Language
method	I-Language
pattern	I-Language
"	O
,	O
the	O
"	O
Builder	O
"	O
or	O
even	O
the	O
"	O
Singleton	O
"	O
are	O
implementations	O
of	O
this	O
concept	O
.	O
</s>
<s>
The	O
"	O
Abstract	B-Language
factory	I-Language
pattern	I-Language
"	O
instead	O
is	O
a	O
method	B-Language
to	O
build	O
collections	O
of	O
factories	O
.	O
</s>
<s>
In	O
some	O
design	O
patterns	O
,	O
a	O
factory	B-Application
object	I-Application
has	O
a	O
method	B-Language
for	O
every	O
kind	O
of	O
object	O
it	O
is	O
capable	O
of	O
creating	O
.	O
</s>
<s>
Factory	B-Application
objects	I-Application
are	O
common	O
in	O
toolkits	B-Library
and	O
frameworks	B-Architecture
where	O
library	O
code	O
needs	O
to	O
create	O
objects	O
of	O
types	O
which	O
may	O
be	O
subclassed	O
by	O
applications	O
using	O
the	O
framework	O
.	O
</s>
<s>
As	O
the	O
factory	O
only	O
returns	O
an	O
abstract	B-Architecture
interface	I-Architecture
to	O
the	O
object	O
,	O
the	O
client	O
code	O
does	O
not	O
know	O
–	O
and	O
is	O
not	O
burdened	O
by	O
–	O
the	O
actual	O
concrete	O
type	O
of	O
the	O
object	O
which	O
was	O
just	O
created	O
.	O
</s>
<s>
However	O
,	O
the	O
type	O
of	O
a	O
concrete	O
object	O
is	O
known	O
by	O
the	O
abstract	B-Language
factory	I-Language
.	O
</s>
<s>
The	O
client	O
code	O
has	O
no	O
knowledge	O
whatsoever	O
of	O
the	O
concrete	O
type	O
,	O
not	O
needing	O
to	O
include	O
any	O
header	B-Language
files	I-Language
or	O
class	O
declarations	O
relating	O
to	O
the	O
concrete	O
type	O
.	O
</s>
<s>
Objects	O
of	O
a	O
concrete	O
type	O
are	O
indeed	O
created	O
by	O
the	O
factory	O
,	O
but	O
the	O
client	O
code	O
accesses	O
such	O
objects	O
only	O
through	O
their	O
abstract	B-Architecture
interface	I-Architecture
.	O
</s>
<s>
Factories	O
,	O
specifically	O
factory	B-Language
methods	I-Language
,	O
are	O
common	O
in	O
toolkits	B-Library
and	O
frameworks	B-Architecture
,	O
where	O
library	O
code	O
needs	O
to	O
create	O
objects	O
of	O
types	O
that	O
may	O
be	O
subclassed	O
by	O
applications	O
using	O
the	O
framework	O
.	O
</s>
<s>
Factory	B-Language
methods	I-Language
are	O
used	O
in	O
test-driven	O
development	O
to	O
allow	O
classes	O
to	O
be	O
put	O
under	O
test	O
.	O
</s>
<s>
If	O
such	O
a	O
class	O
Foo	O
creates	O
another	O
object	O
Dangerous	O
that	O
ca	O
n't	O
be	O
put	O
under	O
automated	O
unit	O
tests	O
(	O
perhaps	O
it	O
communicates	O
with	O
a	O
production	O
database	O
that	O
is	O
n't	O
always	O
available	O
)	O
,	O
then	O
the	O
creation	O
of	O
Dangerous	O
objects	O
is	O
placed	O
in	O
the	O
virtual	B-Application
factory	B-Language
method	I-Language
createDangerous	O
in	O
class	O
Foo	O
.	O
</s>
<s>
For	O
testing	O
,	O
TestFoo	O
(	O
a	O
subclass	O
of	O
Foo	O
)	O
is	O
then	O
created	O
,	O
with	O
the	O
virtual	B-Application
factory	B-Language
method	I-Language
createDangerous	O
overridden	O
to	O
create	O
and	O
return	O
FakeDangerous	O
,	O
a	O
fake	O
object	O
.	O
</s>
<s>
Besides	O
use	O
in	O
design	O
patterns	O
,	O
factories	O
,	O
especially	O
factory	B-Language
methods	I-Language
,	O
have	O
various	O
benefits	O
and	O
variations	O
.	O
</s>
<s>
A	O
factory	B-Language
method	I-Language
has	O
a	O
distinct	O
name	O
.	O
</s>
<s>
In	O
many	O
object-oriented	B-Language
languages	I-Language
,	O
constructors	O
must	O
have	O
the	O
same	O
name	O
as	O
the	O
class	O
they	O
are	O
in	O
,	O
which	O
can	O
lead	O
to	O
ambiguity	O
if	O
there	O
is	O
more	O
than	O
one	O
way	O
to	O
create	O
an	O
object	O
(	O
see	O
overloading	B-Application
)	O
.	O
</s>
<s>
Factory	B-Language
methods	I-Language
have	O
no	O
such	O
constraint	O
and	O
can	O
have	O
descriptive	O
names	O
;	O
these	O
are	O
sometimes	O
known	O
as	O
alternative	O
constructors	O
.	O
</s>
<s>
As	O
an	O
example	O
,	O
when	O
complex	O
numbers	O
are	O
created	O
from	O
two	O
real	O
numbers	O
the	O
real	O
numbers	O
can	O
be	O
interpreted	O
as	O
Cartesian	O
or	O
polar	O
coordinates	O
,	O
but	O
using	O
factory	B-Language
methods	I-Language
,	O
the	O
meaning	O
is	O
clear	O
,	O
as	O
illustrated	O
by	O
the	O
following	O
example	O
in	O
C#	O
.	O
</s>
<s>
When	O
factory	B-Language
methods	I-Language
are	O
used	O
for	O
disambiguation	O
like	O
this	O
,	O
the	O
raw	O
constructors	O
are	O
often	O
made	O
private	O
to	O
force	O
clients	O
to	O
use	O
the	O
factory	B-Language
methods	I-Language
.	O
</s>
<s>
Factory	B-Language
methods	I-Language
encapsulate	O
the	O
creation	O
of	O
objects	O
.	O
</s>
<s>
This	O
logic	O
can	O
be	O
encapsulated	O
in	O
a	O
factory	B-Language
method	I-Language
.	O
</s>
<s>
There	O
are	O
three	O
limitations	O
associated	O
with	O
the	O
use	O
of	O
the	O
factory	B-Language
method	I-Language
.	O
</s>
<s>
The	O
third	O
limitation	O
is	O
that	O
,	O
if	O
the	O
class	O
were	O
to	O
be	O
extended	O
(	O
e.g.	O
,	O
by	O
making	O
the	O
constructor	O
protected	O
—	O
this	O
is	O
risky	O
but	O
feasible	O
)	O
,	O
the	O
subclass	O
must	O
provide	O
its	O
own	O
re-implementation	O
of	O
all	O
factory	B-Language
methods	I-Language
with	O
exactly	O
the	O
same	O
signatures	O
.	O
</s>
<s>
For	O
example	O
,	O
if	O
class	O
StrangeComplex	O
extends	O
Complex	O
,	O
then	O
unless	O
StrangeComplex	O
provides	O
its	O
own	O
version	O
of	O
all	O
factory	B-Language
methods	I-Language
,	O
the	O
call	O
will	O
yield	O
an	O
instance	O
of	O
Complex	O
(	O
the	O
superclass	O
)	O
rather	O
than	O
the	O
expected	O
instance	O
of	O
the	O
subclass	O
.	O
</s>
<s>
The	O
reflection	B-Language
features	O
of	O
some	O
languages	O
can	O
avoid	O
this	O
issue	O
.	O
</s>
<s>
All	O
three	O
problems	O
could	O
be	O
alleviated	O
by	O
altering	O
the	O
underlying	O
programming	O
language	O
to	O
make	O
factories	O
first-class	O
class	O
members	O
(	O
see	O
also	O
Virtual	B-Application
class	I-Application
)	O
.	O
</s>
