Open In App

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.

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().




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.






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




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




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().




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.




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




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




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

Code:




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:

When to Use the Factory Method in Java Design Patterns

This pattern takes whole and sole responsibility of the instantiation of a class from client program to the factory class. It help to provide an way to create a objects without specifying the exact class of that will be created.Some of the following point when to use Factory Design Pattern:

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

Factory design pattern provides a way to create objects without specifying their exact class at compile time. It involves defining an interface or abstract class for creating objects (the factory) and allowing subclasses to alter the type of objects that will be created. This pattern decouples object creation from the client code, promoting flexibility, maintainability, and scalability.


Article Tags :