87 lines
3.4 KiB
Markdown
Raw Normal View History

---
layout: pattern
title: Factory Method
folder: factory-method
permalink: /patterns/factory-method/
categories: Creational
tags:
- Extensibility
- Gang Of Four
---
## Also known as
Virtual Constructor
## Intent
Define an interface for creating an object, but let subclasses
decide which class to instantiate. Factory Method lets a class defer
instantiation to subclasses.
2017-08-12 20:02:14 +03:00
## Explanation
Real world example
2017-08-12 21:44:21 +03:00
2017-08-12 20:02:14 +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.
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
2017-08-12 20:02:14 +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.
**Programmatic Example**
Taking our blacksmith example above. First of all we have a blacksmith interface and some implementations for it
```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) {
return ELFARSENAL.get(weaponType);
2017-08-12 20:02:14 +03:00
}
}
public class OrcBlacksmith implements Blacksmith {
public Weapon manufactureWeapon(WeaponType weaponType) {
return ORCARSENAL.get(weaponType);
2017-08-12 20:02:14 +03:00
}
}
```
Now as the customers come the correct type of blacksmith is summoned and requested weapons are manufactured
2017-08-12 21:44:21 +03:00
```java
var blacksmith = new ElfBlacksmith();
2017-08-12 20:02:14 +03:00
blacksmith.manufactureWeapon(WeaponType.SPEAR);
blacksmith.manufactureWeapon(WeaponType.AXE);
// Elvish weapons are created
```
## Class diagram
![alt text](./etc/factory-method.urm.png "Factory Method pattern class diagram")
## Applicability
Use the Factory Method pattern when
* a class can't anticipate the class of objects it must create
* a 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
2017-12-11 11:45:32 -03:00
## Real world examples
2016-07-04 21:52:39 +03:00
* [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
## Credits
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)