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/
2016-08-30 15:10:34 +02:00
pumlid: NSZB3G8n30N0Lg20n7UwCOxPP9MVx6TMT0zdRgEvjoazYeRrMmMsFuYChtmqr7Y6gycQq8aiQr3hSJ7OwEGtfwBUZfas0shJQR3_G2yMBFkaeQYha4B-AeUDl6FqBm00
2015-08-20 21:40:07 +02:00
categories: Creational
2016-08-20 13:17:53 +05:30
tags:
2015-09-10 15:26:47 +05:30
- Java
- Difficulty-Beginner
2015-09-22 18:25:56 +05:30
- Gang Of Four
2015-08-13 23:54:40 +02:00
---
2016-01-03 21:14:30 +01:00
## Also known as
Virtual Constructor
2015-11-04 21:13:32 +02:00
2016-01-03 21:14:30 +01:00
## Intent
Define an interface for creating an object, but let subclasses
2015-08-13 23:54:40 +02:00
decide which class to instantiate. Factory Method lets a class defer
instantiation to subclasses.

2016-01-03 21:14:30 +01:00
## Applicability
Use the Factory Method pattern when
2015-08-13 23:54:40 +02:00
* a class can't anticipate the class of objects it must create
* a class wants its subclasses to specify the objects it creates
2015-08-15 18:03:05 +02:00
* 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
2016-07-04 21:52:39 +03:00
## Known uses
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
* [Design Patterns: Elements of Reusable Object-Oriented Software ](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612 )