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:

  • When you have different type of things to create: Imagine that you have a program that needs to create different kind of shapes, like circle, triangle and square. Factory pattern will help you to create there shapes without knowing the exact details of each of them.
  • When we want to organize how objects are made: Consider creating an object involves a bunch of steps or rules. A factory help you keep all these steps in one place, making code easier to maintain.
  • When you want to let subclass take decision for what to create: Say if you have a basic class like Currency and you want a specific subclass(like USB, GSD, INR) to decide how they’re made, a factory pattern is suitable here. It let’s each subclass have its own way of creating itself.

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

...