2015-08-13 23:54:40 +02:00
---
layout: pattern
title: Factory Method
folder: factory-method
2015-08-15 18:03:05 +02:00
permalink: /patterns/factory-method/
2015-08-20 21:40:07 +02:00
categories: Creational
2021-05-19 10:49:05 -06:00
language: en
2016-08-20 13:17:53 +05:30
tags:
2019-12-13 21:09:28 +02:00
- Extensibility
2021-06-24 15:57:20 +03:00
- Gang of Four
2015-08-13 23:54:40 +02:00
---
2016-01-03 21:14:30 +01:00
## Also known as
2020-08-29 20:46:40 +03:00
2016-01-03 21:14:30 +01:00
Virtual Constructor
2015-11-04 21:13:32 +02:00
2016-01-03 21:14:30 +01:00
## Intent
2020-08-29 20:46:40 +03:00
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Factory Method lets a class defer instantiation to subclasses.
2015-08-13 23:54:40 +02:00
2017-08-12 20:02:14 +03:00
## Explanation
2020-08-29 20:46:40 +03:00
2021-06-24 15:57:20 +03:00
Real-world example
2017-08-12 21:44:21 +03:00
2020-08-29 20:46:40 +03:00
> Blacksmith manufactures weapons. Elves require Elvish weapons and orcs require Orcish weapons.
> Depending on the customer at hand the right type of blacksmith is summoned.
2017-08-12 20:02:14 +03:00
In plain words
2017-08-12 21:44:21 +03:00
2017-08-12 20:02:14 +03:00
> It provides a way to delegate the instantiation logic to child classes.
Wikipedia says
2017-08-12 21:44:21 +03:00
2020-08-29 20:46:40 +03:00
> In class-based programming, the factory method pattern is a creational pattern that uses factory
> methods to deal with the problem of creating objects without having to specify the exact class of
> the object that will be created. This is done by creating objects by calling a factory method
> — either specified in an interface and implemented by child classes, or implemented in a base
> class and optionally overridden by derived classes—rather than by calling a constructor.
2017-08-12 20:02:14 +03:00
**Programmatic Example**
2021-06-24 15:57:20 +03:00
Taking our blacksmith example above. First of all, we have a `Blacksmith` interface and some
2020-08-29 20:46:40 +03:00
implementations for it:
2017-08-12 20:02:14 +03:00
2018-03-28 01:35:43 -04:00
```java
2017-08-12 20:02:14 +03:00
public interface Blacksmith {
Weapon manufactureWeapon(WeaponType weaponType);
}
public class ElfBlacksmith implements Blacksmith {
public Weapon manufactureWeapon(WeaponType weaponType) {
2019-10-30 11:57:24 +05:30
return ELFARSENAL.get(weaponType);
2017-08-12 20:02:14 +03:00
}
}
public class OrcBlacksmith implements Blacksmith {
public Weapon manufactureWeapon(WeaponType weaponType) {
2019-10-30 11:57:24 +05:30
return ORCARSENAL.get(weaponType);
2017-08-12 20:02:14 +03:00
}
}
```
2020-08-29 20:46:40 +03:00
When the customers come, the correct type of blacksmith is summoned and requested weapons are
manufactured:
2017-08-12 21:44:21 +03:00
2018-03-28 01:35:43 -04:00
```java
2021-06-24 15:57:20 +03:00
Blacksmith blacksmith = new OrcBlacksmith();
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
LOGGER.info("{} manufactured {}", blacksmith, weapon);
weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
LOGGER.info("{} manufactured {}", blacksmith, weapon);
blacksmith = new ElfBlacksmith();
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
LOGGER.info("{} manufactured {}", blacksmith, weapon);
weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
LOGGER.info("{} manufactured {}", blacksmith, weapon);
2020-08-29 20:46:40 +03:00
```
Program output:
2021-06-24 15:57:20 +03:00
```
The orc blacksmith manufactured an orcish spear
The orc blacksmith manufactured an orcish axe
The elf blacksmith manufactured an elven spear
The elf blacksmith manufactured an elven axe
2017-08-12 20:02:14 +03:00
```
2015-08-13 23:54:40 +02:00
2019-12-07 20:01:13 +02:00
## Class diagram
2020-08-29 20:46:40 +03:00
2019-12-07 20:01:13 +02:00

2016-01-03 21:14:30 +01:00
## Applicability
2015-08-13 23:54:40 +02:00
2020-08-29 20:46:40 +03:00
Use the Factory Method pattern when:
* Class cannot anticipate the class of objects it must create.
* Class wants its subclasses to specify the objects it creates.
* Classes delegate responsibility to one of several helper subclasses, and you want to localize the
knowledge of which helper subclass is the delegate.
2015-09-22 18:25:56 +05:30
2021-06-24 15:57:20 +03:00
## Known uses
2016-07-04 21:52:39 +03:00
2016-08-20 13:17:53 +05:30
* [java.util.Calendar ](http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getInstance-- )
* [java.util.ResourceBundle ](http://docs.oracle.com/javase/8/docs/api/java/util/ResourceBundle.html#getBundle-java.lang.String- )
* [java.text.NumberFormat ](http://docs.oracle.com/javase/8/docs/api/java/text/NumberFormat.html#getInstance-- )
* [java.nio.charset.Charset ](http://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html#forName-java.lang.String- )
* [java.net.URLStreamHandlerFactory ](http://docs.oracle.com/javase/8/docs/api/java/net/URLStreamHandlerFactory.html#createURLStreamHandler-java.lang.String- )
* [java.util.EnumSet ](https://docs.oracle.com/javase/8/docs/api/java/util/EnumSet.html#of-E- )
* [javax.xml.bind.JAXBContext ](https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/JAXBContext.html#createMarshaller-- )
2016-07-04 21:52:39 +03:00
2016-01-03 21:14:30 +01:00
## Credits
2015-09-22 18:25:56 +05:30
2020-07-06 13:31:07 +03:00
* [Design Patterns: Elements of Reusable Object-Oriented Software ](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59 )
2020-07-07 18:05:11 +03:00
* [Head First Design Patterns: A Brain-Friendly Guide ](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b )
2020-07-07 18:44:00 +03:00
* [Refactoring to Patterns ](https://www.amazon.com/gp/product/0321213351/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321213351&linkCode=as2&tag=javadesignpat-20&linkId=2a76fcb387234bc71b1c61150b3cc3a7 )