Understanding the Factory Method in Java Design Patterns

Let’s consider the above-mentioned diagram for implementing a factory design pattern. Here is a Factory Method pattern to convert currency values between Indian Rupees (INR), US Dollars (USD), and Great British Pounds (GBP).

Step 1: Define the Product(Currency):

This is an interface or an abstract class that declares the method(s) that need to be implemented by the concrete products. In the example provided, Currency is the product interface with the method getSymbol().

Java




public interface Currency {
    public String getSymbol();
}


Step 2: Create Concrete Products (INR, USD, GBP):

These are the actual implementations of the Currency interface. Each concrete product provides its own implementation of the getSymbol() method. In the example, INR, USD, and GBP are concrete implementations.

Java




public class INR implements Currency {
    @Override public String getSymbol() { return "₹"; }
}


Java




public class GBP implements Currency {
    @Override public String getSymbol() { return "£"; }
}


Java




public class USD implements Currency {
    @Override public String getSymbol() { return "$"; }
}


Step 3: Define the Factory (CurrencyFactory):

This is an interface or an abstract class that declares the method for creating objects. In the example, CurrencyFactory is the factory interface with the method createCurrency().

Java




public interface CurrencyFactory {
    Currency createCurrency();
}


Step 4: Create Concrete Factories (INRFactory, USDFactory, GBPFactory):

These are the actual implementations of the factory interface. Each concrete factory is responsible for creating an instance of a specific concrete product. In the example, INRFactory, USDFactory, and GBPFactory are concrete factory implementations.

Java




public class INRFactory implements CurrencyFactory {
    @Override public Currency createCurrency()
    {
        return new INR();
    }
}


Java




public class USDFactory implements CurrencyFactory {
    @Override public Currency createCurrency()
    {
        return new USD();
    }
}


Java




public static class GBPFactory
        implements CurrencyFactory {
        @Override public Currency createCurrency()
        {
            return new GBP();
        }
    }


Code:

Java




import java.util.*;
 
     
public class Main {
//Currency is the product interface with method getSymbol.
    public interface Currency {
        public String getSymbol();
    }
// This interface represents the CurrencyFactory responsible for creating instances of Currency implementations.
    public interface CurrencyFactory {
         
        Currency createCurrency();
    }
// It provides an implementation for the getSymbol() method representing the Indian Rupee currency symbol.
    public static class INR implements Currency{
        @Override public String getSymbol() {
            return "₹";
        }
}
//createCurrency() method that creates and returns an instance of INR.
    public static class INRFactory
        implements CurrencyFactory {
        @Override public Currency createCurrency()
        {
            return new INR();
        }
    }
// It provides an implementation for the getSymbol() method representing the GBP currency symbol.
    public static class GBP implements Currency {
        @Override public String getSymbol() {
            return "£";
        }
    }
//createCurrency() method that creates and returns an instance of GBP.
    public static class GBPFactory
        implements CurrencyFactory {
        @Override public Currency createCurrency()
        {
            return new GBP();
        }
    }
// It provides an implementation for the getSymbol() method representing the USD
    public static class USD implements Currency {
        @Override public String getSymbol() {
            return "$";
        }
    }
//createCurrency() method that creates and returns an instance of USD.
    public static class USDFactory
        implements CurrencyFactory {
        @Override public Currency createCurrency()
        {
            return new USD();
        }
    }
     
    public static void main(String[] args) throws Exception
    {
         
        INRFactory objINRFactory = new INRFactory();
        Currency objINRCurrency = objINRFactory.createCurrency();
        
        GBPFactory objGBPFactory = new GBPFactory();
        Currency objGBPCurrency = objGBPFactory.createCurrency();
         
        USDFactory objUSDFactory = new USDFactory();
        Currency objUSDCurrency = objUSDFactory.createCurrency();
         
        System.out.println("Symbol of INR: "+objINRCurrency.getSymbol());
        System.out.println("Symbol of GBP: "+objGBPCurrency.getSymbol());
        System.out.println("Symbol of USD: "+objUSDCurrency.getSymbol());
    }
}


Output

Symbol of INR: ₹
Symbol of GBP: £
Symbol of USD: $


Below is the explanation of the above example:

  • We have defined a Currency interface with three concrete implementations (INR, USD, and GBP).
  • We also have a CurrencyFactory interface with three concrete factory implementations (INRFactory, USDFactory, and GBPFactory) responsible for creating instances of the respective currencies.
  • When we run the Main class, we get the symbols for INR, USD, and GBP as expected.
  • This demonstrates how the Factory Method pattern allows us to create objects without specifying the exact class at compile time.

Factory Method | Java Design Patterns

The factory design pattern is a creational design pattern. It states the best way to create an object without telling the exact class of object that will be created. It is used when we have a super-class with multiple sub-classes and based on input, we want to return one of the sub-class.

Important Topics for the Factory Method in Java Design Patterns

  • Understanding the Factory Method in Java Design Patterns
  • When to Use the Factory Method in Java Design Patterns
  • Important Points to Keep in Mind for the Factory Method in Java Design Patterns
  • Advantages of the Factory Design Pattern in Java Design Patterns
  • Disadvantages of the Factory Design Pattern in Java Design Patterns
  • Conclusion

Similar Reads

Understanding the Factory Method in Java Design Patterns

...

When to Use the Factory Method in Java Design Patterns

Let’s consider the above-mentioned diagram for implementing a factory design pattern. Here is a Factory Method pattern to convert currency values between Indian Rupees (INR), US Dollars (USD), and Great British Pounds (GBP)....

Important Points to Keep in Mind for the Factory Method in Java Design Patterns

...

Advantages of the Factory Design Pattern in Java Design Patterns

...

Disadvantages of the Factory Design Pattern in Java Design Patterns

...

Conclusion

...