Java design pattern: Factory

In this article, we will look at another Java design pattern from the category of patterns designed for creating objects (creational patterns) – Factory

Read more – a series of design patterns:

What is the Factory design pattern?

The design pattern Factory is a software design pattern whose main goal is to provide a simple and flexible way to create class instances without having to directly specify specific classes in client code.

What problem does the Factory design pattern solve?

If we have classes that implement or are derived from a common interface, we want to be able to create instances of those classes without having to directly mention a specific class. This is useful if we don’t want the client code to be bound to a specific implementation, but want to retrieve an object based on some parameter or condition.

Example of Factory implementation in Java

Now we will show how to implement the Factory pattern in Java. This example illustrates how the Factory pattern allows you to create different types of objects (chocolates) using a single factory class without having to directly specify specific classes in client code.

In this example, we have created an abstract class Cokolada to represent chocolate.

package designpatterns;

// Abstraktna trieda reprezentujuca cokoladu
public abstract class Cokolada {
     public abstract void ochutnat ();

We then implemented two concrete classes, Milka and Lindt, which extend the abstract class Cokolada. Each of these classes defines its own implementation ochutnat() method.

package designpatterns;

// Konkretna implementacia cokolady znacky Milka
public abstract class Milka extends Cokolada { 
       public void ochutnat ()   { 
                 system.out.println("Milka: Sladká a mliečna chut");

// Konkretna implementacia cokolady znacky Lindt
public abstract class Lindt extends Cokolada { 
       public void ochutnat ()   { 
                 system.out.println("Lindt: Jemná a luxusná chut");

Next, we created the CokoladaFactory class, which is used to create instances of chocolate based on the brand. The vytvorCokoladu(String tag) method creates a chocolate according to the specified tag parameter. If the tag is Milka, an instance of the Milka class is created, if the tag is Lindt, an instance of the Lindt class is created. If an unknown tag is specified, an IllegalArgumentException is thrown.

package designpatterns;

// Factory trieda pre vyrobu cokolady
public class CokoladaFactory {
    public Cokolada vytvorCokoladu(String znacka) {
        if(znacka.equalsIgnoreCase("Milka")) {
            return new Milka();
        else if(znacka.equalsIgnoreCase("Lindt")) {
            return new Lindt();
        else {
            throw new IllegalArgumentException("Neznáma 
                značka čokolády: " + znacka);

We created a CokoladaFactory instance in the client code, and used it to create and taste Milka and Lindt chocolates.

import designpatterns.*;

// Pouzitie
public class Main {
    public static void main(String[] args) {
        CokoladaFactory factory = new CokoladaFactory();

        // Vytvorenie a ochutnanie cokolady znacky Milka
        Cokolada milka = factory.vytvorCokoladu("Milka");

        // Vytvorenie a ochutnanie cokolady znacky Lindt
        Cokolada lindt = factory.vytvorCokoladu("Lindt");

Output of the program
Output of the program

We have prepared the files with the above mentioned Java Factory example in the form of code that you can run directly in Java. Download the code here.

If you’re a Java developer looking for a job, read more about developer salary and check out our latest job offers.

About the author

Jozef Wagner

Java Developer Senior

Viac ako 10 rokov programujem v Jave, momentálne pracujem v msg life Slovakia ako Java programátor senior a pomáham zákazníkom implementovať ich požiadavky do poistného softvéru Life Factory. Vo voľnom čase si rád oddýchnem v lese, prípadne si zahrám nejakú dobrú počítačovú hru.

Let us know about you