2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								layout: pattern
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								title: Factory
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								folder: factory
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								permalink: /patterns/factory/
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								categories: Creational
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								tags:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								 -  Gang of Four
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								## Also known as
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								*  Simple Factory 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								*  Static Factory Method 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								## Intent
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-06 19:46:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Providing a static method encapsulated in a class called factory, in order to hide the 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								implementation logic and makes client code focus on usage rather then initialization new objects.
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								## Explanation
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Real world example
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-06 19:46:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								>  Lets say we have a web application connected to SQLServer, but now we want to switch to Oracle. To 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								>  do so without modifying existing source code, we need to implements Simple Factory pattern, in 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								>  which a static method can be invoked to create connection to a given database.
  
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Wikipedia says
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-06 19:46:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								>  Factory is an object for creating other objects –   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								>  returns objects of a varying prototype or class.
  
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								**Programmatic Example**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-06 19:46:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								We have an interface `Car`  and two implementations `Ford`  and `Ferrari` .
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								```java
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								public interface Car {
							 
						 
					
						
							
								
									
										
										
										
											2020-09-06 19:46:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  String getDescription();
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								public class Ford implements Car {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  static final String DESCRIPTION = "This is Ford.";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  @Override 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  public String getDescription() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    return DESCRIPTION;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								public class Ferrari implements Car {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								   
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  static final String DESCRIPTION = "This is Ferrari.";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  @Override 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  public String getDescription() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    return DESCRIPTION;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-06 19:46:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Enumeration above represents types of cars that we support (`Ford`  and `Ferrari` ).
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								```java
							 
						 
					
						
							
								
									
										
										
										
											2020-09-04 21:21:51 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								public enum CarType {
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  
							 
						 
					
						
							
								
									
										
										
										
											2020-09-04 21:21:51 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  FORD(Ford::new), 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  FERRARI(Ferrari::new);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  private final Supplier< Car >  constructor; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  CarType(Supplier< Car >  constructor) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    this.constructor = constructor;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  public Supplier< Car >  getConstructor() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    return this.constructor;
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2020-09-04 21:21:51 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2020-09-06 19:46:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Then we have the static method `getCar`  to create car objects encapsulated in the factory class 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-30 15:26:01 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								`CarsFactory` . 
						 
					
						
							
								
									
										
										
										
											2020-09-04 21:21:51 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								```java
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								public class CarsFactory {
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  public static Car getCar(CarType type) {
							 
						 
					
						
							
								
									
										
										
										
											2020-09-04 21:21:51 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    return type.getConstructor().get();
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-04 21:21:51 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Now on the client code we can create different types of cars using the factory class.
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								```java
							 
						 
					
						
							
								
									
										
										
										
											2020-09-04 21:21:51 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								var car1 = CarsFactory.getCar(CarType.FORD);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								var car2 = CarsFactory.getCar(CarType.FERRARI);
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								LOGGER.info(car1.getDescription());
							 
						 
					
						
							
								
									
										
										
										
											2020-12-05 14:26:00 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								LOGGER.info(car2.getDescription());
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Program output:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								```java
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								This is Ford.
							 
						 
					
						
							
								
									
										
										
										
											2021-03-18 13:19:45 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								This is Ferrari.
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2020-09-06 19:46:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-05 18:39:28 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								## Class Diagram
  
						 
					
						
							
								
									
										
										
										
											2020-09-06 19:46:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-05 18:39:28 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-05 18:39:28 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								## Applicability
  
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-06 19:46:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Use the Simple Factory pattern when you only care about the creation of a object, not how to create 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								and manage it.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Pros
							 
						 
					
						
							
								
									
										
										
										
											2020-09-06 19:55:38 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-05 14:04:49 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  Allows keeping all objects creation in one place and avoid of spreading 'new' keyword across codebase. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								*  Allows to write loosely coupled code. Some of its main advantages include better testability, easy-to-understand code, swappable components, scalability and isolated features. 
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-06 19:46:13 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Cons
							 
						 
					
						
							
								
									
										
										
										
											2020-09-06 19:55:38 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-05 18:39:28 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  The code becomes more complicated than it should be.  
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-13 10:33:43 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								## Real world examples
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								*  [java.util.Calendar#getInstance() ](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getInstance-- ) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								*  [java.util.ResourceBundle#getBundle() ](https://docs.oracle.com/javase/8/docs/api/java/util/ResourceBundle.html#getBundle-java.lang.String- ) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								*  [java.text.NumberFormat#getInstance() ](https://docs.oracle.com/javase/8/docs/api/java/text/NumberFormat.html#getInstance-- ) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								*  [java.nio.charset.Charset#forName() ](https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html#forName-java.lang.String- ) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								*  [java.net.URLStreamHandlerFactory#createURLStreamHandler(String) ](https://docs.oracle.com/javase/8/docs/api/java/net/URLStreamHandlerFactory.html ) (Returns different singleton objects, depending on a protocol) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								*  [java.util.EnumSet#of() ](https://docs.oracle.com/javase/8/docs/api/java/util/EnumSet.html#of(E )) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								*  [javax.xml.bind.JAXBContext#createMarshaller() ](https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/JAXBContext.html#createMarshaller-- ) and other similar methods. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 22:08:54 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								## Related patterns
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-06 19:56:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  [Factory Method ](https://java-design-patterns.com/patterns/factory-method/ ) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								*  [Factory Kit ](https://java-design-patterns.com/patterns/factory-kit/ ) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								*  [Abstract Factory ](https://java-design-patterns.com/patterns/abstract-factory/ )