Factory design pattern — Java
Tags: tutorial, programming, java, designpattern
Published: 2022-01-28 12:09:02

![design-pattern](https://cdn-images-1.medium.com/max/2000/1*cpmqYomwpJ_fAZ_p_sxGNQ.jpeg)

## Definition of the Factory pattern
>  In class-based programming, the [**factory method pattern](https://javarevisited.blogspot.com/2015/06/difference-between-dependency-injection.html)** 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 ](http://javarevisited.blogspot.sg/2011/12/factory-design-pattern-java-example.html#axzz51cvxH5kW)— 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.

## Where to use the Factory pattern

* When a class doesn’t know what sub-classes will be required to create

* When a class wants that its sub-classes specify the objects to be created.

* When the parent classes choose the creation of objects to its sub-classes.

## UML example

![](https://cdn-images-1.medium.com/max/3000/1*07jwCvFRdb0DivjOLo137g.png)

## Implementation of the Factory Pattern

We are going to create a Human [abstract class](https://www.java67.com/2017/08/difference-between-abstract-class-and-interface-in-java8.html) and then the different periods of age that will implement it, then we will create the factory that will generate it.

### The human abstract class
{% gist https://gist.github.com/ErwanLT/8fb7382663a4e3a0b28c52caf3e3f9db.js %}
### The implementations
{% gist https://gist.github.com/ErwanLT/7be632234fd513722e4121ebcc805c37.js %}{% gist https://gist.github.com/ErwanLT/1ae0ae5a8f342a75932e70abcd13b68c.js %}{% gist https://gist.github.com/ErwanLT/9d52eefcd90baf2740be7bfa5c4b2a36.js %}
Each implementation [overrides ](https://www.java67.com/2015/08/top-10-method-overloading-overriding-interview-questions-answers-java.html)the *getAge()* method to return a random int that will correspond to the age period.

### The factory

Now we can create the factory that will generate a Human based on the given information, here we will use an [enum ](https://javarevisited.blogspot.com/2011/08/enum-in-java-example-tutorial.html)to be sure that we don’t pass the nonexisting value to the factory
{% gist https://gist.github.com/ErwanLT/334c2d2e35dd3969462a49482960f31f.js %}{% gist https://gist.github.com/ErwanLT/df3197457d26715af9dccec8e2684e15.js %}
now we can generate our humans using the factory

### Usage
{% gist https://gist.github.com/ErwanLT/462cb252b4d34b68984f250ba013b94c.js %}
that will give an output like the following one :

    ==============================
    age : 8
    ==============================
    ==============================
    age : 49
    ==============================
    ==============================
    age : 99
    ==============================

Here are other **design pattern articles **you may like
[**7 Best Online Courses to learn Object-Oriented Design Pattern in Java**](https://medium.com/javarevisited/7-best-online-courses-to-learn-object-oriented-design-pattern-in-java-749b6399af59)
[**10 OOP Design Principles You Can Learn in 2021**](https://medium.com/javarevisited/10-oop-design-principles-you-can-learn-in-2020-f7370cccdd31)
[**7 Best books to learn Design Patterns for Java Programmers**](https://medium.com/javarevisited/7-best-books-to-learn-design-patterns-for-java-programmers-5627b93eefdb)