48

Creational Design Patterns: Factory Pattern

 5 years ago
source link: https://www.tuicool.com/articles/hit/nABb2um
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.

Free Resource

Join the DZone community and get the full member experience.

Verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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 .

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK