Skip to main content


CreationalGang of FourAbout 2 min

Also known as

  • Simple Factory
  • Static Factory Method


Providing a static method encapsulated in a class called the factory, to hide the implementation
logic and make client code focus on usage rather than initializing new objects.


Real-world example

Imagine an alchemist who is about to manufacture coins. The alchemist must be able to create both
gold and copper coins and switching between them must be possible without modifying the existing
source code. The factory pattern makes it possible by providing a static construction method which
can be called with relevant parameters.

Wikipedia says

Factory is an object for creating other objects – formally a factory is a function or method that
returns objects of a varying prototype or class.

Programmatic Example

We have an interface Coin and two implementations GoldCoin and CopperCoin.

public interface Coin {
  String getDescription();

public class GoldCoin implements Coin {

  static final String DESCRIPTION = "This is a gold coin.";

  public String getDescription() {
    return DESCRIPTION;

public class CopperCoin implements Coin {
  static final String DESCRIPTION = "This is a copper coin.";

  public String getDescription() {
    return DESCRIPTION;

Enumeration below represents types of coins that we support (GoldCoin and CopperCoin).

public enum CoinType {


  private final Supplier<Coin> constructor;

Then we have the static method getCoin to create coin objects encapsulated in the factory class

public class CoinFactory {

  public static Coin getCoin(CoinType type) {
    return type.getConstructor().get();

Now on the client code we can create different types of coins using the factory class."The alchemist begins his work.");
var coin1 = CoinFactory.getCoin(CoinType.COPPER);
var coin2 = CoinFactory.getCoin(CoinType.GOLD);;;

Program output:

The alchemist begins his work.
This is a copper coin.
This is a gold coin.

Class Diagram

alt text
Factory pattern class diagram


Use the factory pattern when you only care about the creation of an object, not how to create
and manage it.


  • 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.


  • The code becomes more complicated than it should be.

Known uses