1. What is the Factory Design Pattern?

Before delving into the concept of the Factory Design Pattern in Java, let’s take a look at this practical example. The emergence of fintech has been a powerful aid in developing systems related to Finance, Insurance, and Banking. I also have a few years of experience in Banking, so I will use a Banking service as an example.

Most banks will support four types of accounts as follows:

  • Checking Account
  • Savings Account
  • Money Market Account
  • Certificate of Deposit

The challenge is to find a suitable solution to create these types of Banking Accounts. The Factory Design Pattern is a promising candidate because its purpose is to manage the creation of objects as required.

2. Factory Design Pattern Diagram

Factory Design Pattern in Java

Now let’s explore the ways to implement the Factory design pattern together. From the class diagram, coders can easily see that the implementation for Factory is extremely straightforward. AccountFactory takes an AccountType as input and returns a specific BankAccount as output.

3. Factory Design Pattern Implementation

Based on the diagram above, let’s go through the implementation of this design pattern together.

First, we create a BankAccount, which is an abstract class that will contain information commonly used for specific Accounts.

public abstract class BankAccount{

    protected String bankName;

    protected String accountNumber;

    protected BigDecimal balance;

    protected abstract String withDraw();

    protected abstract String deposit();

}

CheckingAccount is a class that extends BankAccount. This class is responsible for implementing specific business logic for Checking accounts.

public class CheckingAccount extends BankAccount{

    @Override

    protected String withDraw(){

        return “Checking account withdraws”;

    }

    @Override

    protected String deposit(){

        return “Checking account deposits”;

    }

}

SavingAccount is a class that extends BankAccount. This class is responsible for implementing specific business logic for Saving accounts.

public class SavingsAccount extends BankAccount{

    @Override

    protected String withDraw(){

        return “Saving account withdraws”;

    }

    @Override

    protected String deposit(){

        return “Saving account deposits”;

    }

}

MoneyMarketAccount is a class that extends BankAccount. This class is responsible for implementing specific business logic for MoneyMarket accounts.

public class MoneyMarketAccount extends BankAccount{

    @Override

    protected String withDraw(){

        return “MoneyMarket account withdraws”;

    }

    @Override

    protected String deposit(){

        return “MoneyMarket account deposits”;

    }

}

CertificateDepositAccount is a class that extends BankAccount. This class is responsible for implementing specific business logic for Certificate Deposit accounts.

public class CertificateDepositAccount extends BankAccount{

    @Override

    protected String withDraw(){

        return “CertificateDeposit account withdraws”;

   }

 @Override

    protected String deposit(){

        return “CertificateDeposit account deposits”;

    }}

AccountType consists of fixed values that have been predefined in the banking industry domain; therefore, it is a constant, and we use Enum for this data type to ensure integrity.

public enum AccountType{

    CHECKING,

    SAVINGS,

    MONEY,

    CERTIFICATE

}

AccountFactory is a class that implements the Factory design pattern. It uses the accountType to create the corresponding type of BankAccount.

public class AccountFactory{

    public BankAccount createAccount(AccountType accountType){

        switch(accountType) {

            case CHECKING:

                return new CheckingAccount();

            case SAVINGS:

                return new SavingsAccount();

            case MONEY:

                return new MoneyMarketAccount();

            case CERTIFICATE:

                return new CertificateDepositAccount();

            default:

                throw new IllegalArgumentException(“Error”));

        }

    }

}

Below is how to use AccountFactory to create a CheckingAccount.

public class Execution{

    public static void main(String[] args){

        AccountFactory accountFactory = new AccountFactory();

        BankAccount account = accountFactory.createAccount(AccountType.CHECKING);

        account.withDraw();

        account.deposit();

    }

}

4. The Pros and Cons of Factory Design Pattern

The Pros of Factory

The first strength of the Factory pattern that we can see is that it centralizes the creation of objects in one place. This helps developers to easily read and maintain the code.

Secondly, this pattern minimizes the dependency between classes. This facilitates easy scaling. For example, in the future, there might be new types of accounts such as Credit, Debit, etc. At that time, we only need to create additional subclasses that extend BankAccount and add them to the factory method.

Thirdly, it can hide complex logic involved in creating objects.

The Cons of Factory

As you can see, the creation of objects in the method create(AccountType type) depends on the constructor of the sub-classes. Therefore, if the constructor has many parameters, the factory method can become complex.

The number of sub-classes may increase over time. For me, this is not really a drawback of the factory. However, this will lead to the create(AccountType type) method becoming increasingly bloated.

The Conclusion

I would like to conclude the article here. Through this article, I have listed what a Factory Design Pattern is and how to implement a Factory. I hope to receive contributions from everyone so that the programming community can continue to develop, and especially so that future articles can be better.

Related Articles on Design Patterns

contact

Java Development Services

To master the Factory Design Pattern in Java, it's beneficial to collaborate with seasoned professionals. Saigon Technology, with its expertise in Java development, provides robust solutions that can help streamline your software development process.
Check Out Our Solutions arrow-narrow-right.png
Content manager
Thanh (Bruce) Pham
CEO of Saigon Technology
A Member of Forbes Technology Council

Related articles

Java or .NET For Web Application Development
Industry

Java or .NET For Web Application Development

Java or .NET for your web development project? Saigon Technology shares its 12-year expertise. Starting a new IT project can be tough, especially for beginners.
15 Amazing Things You Can Do With JavaScript
Methodology

15 Amazing Things You Can Do With JavaScript

JavaScript lets developers create interactive websites and apps. Use it for server-side programming, games, and art projects.
[Design Pattern] Lesson 03: Using Singleton in Java
Technologies

[Design Pattern] Lesson 03: Using Singleton in Java

The article is part of a series on design patterns. This series starts with a familiar design pattern, the Singleton, and aims to increase your knowledge about it.
calendar 10 Apr 2024
[Design Pattern] Lesson 05: Builder Design Pattern in Java
Technologies

[Design Pattern] Lesson 05: Builder Design Pattern in Java

Following the Creational patterns, the Builder is an extremely useful design pattern for creating objects. So, what is a Builder and how do you implement one? Through this article, we hope to provide everyone with additional knowledge about the Builder pattern.
calendar 28 Aug 2024
The Essential Guide to Clean Code Practices for JavaScript, React, and Node.js Projects
Technologies

The Essential Guide to Clean Code Practices for JavaScript, React, and Node.js Projects

Explore essential clean code practices for JavaScript, React, and Node.js. Learn best practices, tools, and techniques to create maintainable and efficient code.
calendar 02 Jul 2024
[Design Pattern] Lesson 06: Prototype Design Pattern in Java
Technologies

[Design Pattern] Lesson 06: Prototype Design Pattern in Java

As one of the popular patterns in the Creational group, the Prototype pattern is highly effective for creating complex objects. Design a Prototype pattern in Java.
calendar 04 Sep 2024

Want to stay updated on industry trends for your project?

We're here to support you. Reach out to us now.
Contact Message Box
Back2Top