Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Creational Design Patterns: Factory Pattern

DZone's Guide to

Creational Design Patterns: Factory Pattern

As we continue to explore creational design patterns, let's look at the commonly used Factory Pattern and learn how and when to apply it.

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

Previously, we had an introduction to the Creational Patterns and used the Abstract Factory Pattern in order to create a families of objects.

The next pattern is the Factory Pattern. The Factory Pattern is one of the most-used patterns when it comes to Java.

So what is the Factory Pattern all about? The Factory Pattern deals with creating objects without specifying the exact class or the object that will be created.

Let’s get into the action by using the factory pattern for a Voucher problem.

Supposing we have a loyalty feature in our application that, depending on the customer’s transactions, rewards the customer with some vouchers.

The voucher requires a voucher code, and since it will be displayed on the application, it requires a specially formatted HTML message.

The first step is to create the voucher interface.

package com.gkatzioura.design.creational.factory;
public interface Voucher {
    public String code();
    public String htmlMessage();
}


We will have two types of Vouchers for now — however, it will be easy to add more once the business people come up with more ideas.

The first one is the food Voucher.

package com.gkatzioura.design.creational.factory;
import java.util.UUID;
public class FoodVoucher implements Voucher {
    private UUID code;
    private static final String htmlMessage= "<html><body><h1>Food Voucher</h1></body></html>";

    public FoodVoucher() {
        code = UUID.randomUUID();
    }

    public String code() {
        return code.toString();
    }

    public String htmlMessage() {
        return htmlMessage;
    }
}


And the second one is the clothes voucher.

package com.gkatzioura.design.creational.factory;

import java.util.UUID;

public class ClothesVoucher implements Voucher {

    private UUID code;
    private static final String htmlMessage = "<html><body><h1>Clothes Voucher</h1></body></html>";

    public ClothesVoucher() {
        code = UUID.randomUUID();
    }

    public String code() {
        return code.toString();
    }

    public String htmlMessage() {
        return htmlMessage;
    }
}


The vouchers will be created based on a transaction-points system.

The food voucher will be created in cases of less than 30 transaction points. For transaction-points higher than 30 the clothes voucher shall be created.

So the next step is to create the mechanism that will create the vouchers based on transaction points.

The best candidate for this is the Factory Pattern.

package com.gkatzioura.design.creational.factory;

public class VoucherFactory {
    public Voucher create(Integer discountPoints) {
        if(discountPoints<=0) {
            throw new IllegalArgumentException("Invalid number of discount points!");
        }

        if(discountPoints<30) {
            return new FoodVoucher();
        } else {
            return new ClothesVoucher();
        }
    }
}


To sum up, by choosing the Factory Pattern:

  • We create the vouchers needed without exposing to the client any creation logic.
  • The client does not have to specify the exact class of the object that will be created.

You can find the source code on GitHub.

In the next blog post, we will have a look at the Builder Pattern.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java ,factory pattern ,creational patterns ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}