2015-08-13 23:54:40 +02:00
---
layout: pattern
title: Abstract Factory
folder: abstract-factory
2015-08-15 18:03:05 +02:00
permalink: /patterns/abstract-factory/
2016-08-30 15:10:34 +02:00
pumlid: PSZB3OD034NHLa81Czwd6sCC39gVxEUWT1_ssLmTtQLqgR5fM7sTmFGtaV6TZu8prd0r6HtQaMKqAZLk1XjT_E6qgPUZfyc0MdTgx0-8LuUn8ErFXdr98NypXxKyezKV
2015-08-20 21:40:07 +02:00
categories: Creational
2016-08-20 13:17:53 +05:30
tags:
2015-09-22 18:25:56 +05:30
- Java
- Gang Of Four
2015-12-28 15:52:44 +02:00
- Difficulty-Intermediate
2015-08-13 23:54:40 +02:00
---
2016-01-03 21:14:30 +01:00
## Also known as
Kit
2015-11-04 21:13:32 +02:00
2016-01-03 21:14:30 +01:00
## Intent
Provide an interface for creating families of related or dependent
2015-08-13 23:54:40 +02:00
objects without specifying their concrete classes.

2016-01-03 21:14:30 +01:00
## Applicability
Use the Abstract Factory pattern when
2015-08-13 23:54:40 +02:00
* a system should be independent of how its products are created, composed and represented
* a system should be configured with one of multiple families of products
* a family of related product objects is designed to be used together, and you need to enforce this constraint
* you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations
2017-05-07 13:28:54 +05:30
* the lifetime of the dependency is conceptually shorter than the lifetime of the consumer.
* you need a run-time value to construct a particular dependency
* you want to decide which product to call from a family at runtime.
* you need to supply one or more parameters only known at run-time before you can resolve a dependency.
## Use Cases:
* Selecting to call the appropriate implementation of FileSystemAcmeService or DatabaseAcmeService or NetworkAcmeService at runtime.
* Unit test case writing becomes much easier
## Consequences:
* Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.
2015-08-13 23:54:40 +02:00
2016-01-03 21:14:30 +01:00
## Real world examples
2015-08-13 23:54:40 +02:00
2015-08-15 18:03:05 +02:00
* [javax.xml.parsers.DocumentBuilderFactory ](http://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilderFactory.html )
2016-08-20 13:17:53 +05:30
* [javax.xml.transform.TransformerFactory ](http://docs.oracle.com/javase/8/docs/api/javax/xml/transform/TransformerFactory.html#newInstance-- )
* [javax.xml.xpath.XPathFactory ](http://docs.oracle.com/javase/8/docs/api/javax/xml/xpath/XPathFactory.html#newInstance-- )
2015-09-22 18:25:56 +05:30
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 )