Factory Method Design Pattern
Tags: designpatterns, java, gangoffour, creational
Published: 2021-05-23 10:15:53

This is a **Creational** Design Pattern under **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.

## Explanation

Real world example

> 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

> It provides a way to delegate the instantiation logic to child classes.

Wikipedia says

> 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
public interface Blacksmith {
  Weapon manufactureWeapon(WeaponType weaponType);
}

public class ElfBlacksmith implements Blacksmith {
  public Weapon manufactureWeapon(WeaponType weaponType) {
    return ELFARSENAL.get(weaponType);
  }
}

public class OrcBlacksmith implements Blacksmith {
  public Weapon manufactureWeapon(WeaponType weaponType) {
    return ORCARSENAL.get(weaponType);
  }
}
```

When the customers come, the correct type of blacksmith is summoned and requested weapons are 
manufactured:

```java
var blacksmith = new ElfBlacksmith();
blacksmith.manufactureWeapon(WeaponType.SPEAR);
blacksmith.manufactureWeapon(WeaponType.AXE);
```

Program output:
```java
// Elven spear
// Elven axe
```

## Class diagram

![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/43rjfjyochyd9srtyff8.png "Factory Method pattern class diagram")

## Applicability

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.

## Real world examples

* [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--)

## Credits

* [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)
* [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)
* [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)

***

Code can be found [here](https://github.com/iluwatar/java-design-patterns/tree/master/factory-method)

Find out about more design patterns from [here](https://dev.to/ohbus/series/12844)

> Credit to Original Authors of this post:
- [Ilkka Seppälä](https://github.com/iluwatar)
- [Rodolfo Forte](https://github.com/Tschis)
- [Wes Gilleland](https://github.com/Deathnerd)
- [Sviataslau Shchaurouski](https://github.com/slawiko)
- [JackieNim](https://github.com/JackieNim)
- [Narendra Pathai](https://github.com/npathai)
- [Hemant Bothra](https://github.com/hbothra15)
- [Anurag Agarwal](https://github.com/anuragagarwal561994)