Core Java

Creational Design Patterns: Factory Pattern

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. 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 action by using the factory pattern for a Voucher problem.
Supposing we have a loyalty feature in our application which, 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 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 it 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 the 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.

On the next blog post we will have a look at the builder pattern.

Also I have compiled a cheat sheet containing a summary of the Creational Design Patterns.
Sign up in the link to receive it.

Published on Java Code Geeks with permission by Emmanouil Gkatziouras, partner at our JCG program. See the original article here: Creational Design Patterns: Factory Pattern

Opinions expressed by Java Code Geeks contributors are their own.

Emmanouil Gkatziouras

He is a versatile software engineer with experience in a wide variety of applications/services.He is enthusiastic about new projects, embracing new technologies, and getting to know people in the field of software.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button