DEV Community

JMarathi
JMarathi

Posted on

What is Factory Design Patterns in Java

One of the goals of software development is to design products in such a way that the software can incorporate changes easily. The de facto standard in today's date is to follow the classic design patterns. Whatever modifications are needed can be done in a shorter span of time than it really takes. If a software development team writes code that needs a lot of modifications to add simple extensions to the product, they are not following the best practices. Design Patterns help solve a lot of ambiguities by providing an approach to write extensible code that does not need many changes while incorporating some new additions to it. The experts in the software industry follow standard design patterns or come up with their own to make their overall process efficiency. In this article, we are going to see one of those classic design patterns called the factory design pattern.

Factory Design Patterns in Java

Factory Design Patterns in Java. We have used simple factory design pattern examples in java for demonstrational purposes.

What kind of problem does the factory design pattern solve? Why use the factory design pattern in Java?

We know what you are thinking - Why use factory design pattern in Java? For a start, classes in java or any other object-oriented programming language are generally written in a way that leads to coupling problems, unless design patterns are used. Subclasses get highly dependent on each other when the code gets larger and larger and that is where the real problem begins. If that class needs some extra methods or maybe a new feature class is to be added, figuring out the modifications to be made to that class will take a lot of time because chances are high that making a simple change to a class may have rippling effects, as they might need changes as well (even the main class may not look the same again after making changes). This cannot go on forever and we need some elegant way to avoid such a situation.

Long story short – Why use a factory design pattern?

Answer: Following the factory design pattern can help solve the dependency issues.

Take a look at the code below, the DrinkCafe class has a createDrink method that does all sorts of stuff from choosing a drink to creating one and then finally returning it.

Read: Factory Design Patterns in Java

Top comments (0)