How to implement Factory Design Pattern using Java?
Tags: webdev, beginners, programming
Published: 2022-01-05 11:52:17


## Design Pattern


Hello👋 Its Rohan Kadam😊

Maybe you are a newbie to coding👩‍💻 or programming, maybe experienced👴 or maybe a FrontEnd Developer or BackEnd Developer you all have may have come across the word Design Pattern as a principle or as a concept. The design pattern is a way or approach to writing code or developing an application. In this article, we try to answer certain questions What is Design Pattern? Why implement a design pattern? How to implement a Design pattern (**Factory**)?

![How to Implement Factory Design Pattern?](https://cdn-images-1.medium.com/max/3840/1*cEm5ML8JhAEbawySBVGh_A.png)

### 🏭What is Design Pattern?

**Design patterns** are the solutions to commonly occurring problems in software design. They are like pre-fabricated blueprints that we can customize to solve a recurring design problem in your code.

They are not pieces of code or libraries that can be imported and used in the development of software or solving a particular problem. We can follow the pattern details and implement a solution that suits the realities of your own program.

We often confused ourself between design pattern and algorithms. While an algorithm always defines a clear set of actions that can achieve some goal, a pattern is a more upper-level description of a solution.

An analogy to an algorithm is a cooking dish: both have clear steps to achieve a goal. They’re not libraries or modules; they’re guidelines you integrate into the core of your designs, giving you a leg up in creating flexible and maintainable object-oriented systems.

### 🏭Why implement a Design Pattern?

We have encountered design patterns only in a nutshell, they’re general object-oriented solutions that you can use in your own designs. Crafted by experienced 👴object-oriented practitioners, design patterns can make your designs more flexible, more resilient to change, and easier to maintain. So, if design patterns still aren’t part of your development toolbelt, here are five reasons you should add them.

1 → Make our life easier by not reinventing the wheel

2 → Improve our object-oriented Skills

3 → Recognize Patterns in libraries and languages

4 → Use the power of a shared vocabulary

5 → Find Truth and beauty

### 🏭How to implement a Factory Design Pattern?

Before jumping directly into the implementation of the design pattern we need to answer certain questions such as What is a Factory design pattern? Why incorporate the design pattern? and finally How to implement a Factory Design Pattern?

**1 →What is Factory Design Pattern?**

The factory design pattern is part of the Creational Design Pattern Family, it let us create an interface over subclass and let subclass do business logic work. A real-life example will be, **Imagine you have run a big delivery business🛒, you got to deliver the package to a consumer now the question arises what should use you used delivery the particular car🚗 , truck 🚚or bicycle🛵**. **So to solution could be we give input as size and weight of a package based on that we decide which would be the best possible mode of transportation**. That's how the Factory pattern works in real life.

**Definition:** 
*The Factory pattern is a design pattern that ***defines an interface or abstract class for creating an object but let the subclasses decide which class to instantiate***.*

**2 → Why implement Factory Design Pattern?**

 1. Factory allows us to follow the Design Principle (**SOLID**).

 2. It fosters **loose-coupling** by eliminating the need to bind application-specific classes into the code.

 3. It allows us to introduce new code without affecting the existing code structure like **Plug and Play Model**

**3 → How to Implement Factory Design Pattern?**

In section, we try to implement Factory Design Pattern using Java. For example, we use the delivery manager example for selecting the mode of transportation.

![Delivery Manager Examples](https://cdn-images-1.medium.com/max/3840/1*N7qnpUqGydS6BC1x19XGJg.png)

**Step 1 → What input should the delivery manager give?**

As a Delivery Manager, he/she should give info about the package like **size** and **weight**, in our example, we are using **PackageInfo** to the job.

![image1](https://cdn-images-1.medium.com/max/2408/1*l3vjAzE3O7QUEvP3EzP97g.png)

**Step 2 → What will be various modes for making a delivery?**

The various modes of delivering packages are car🚗, bicycle🏍 and truck🚚. We have created three-class defining the modes for delivery. According to the definition of Factory Pattern, we need to **define an interface or abstract class in **our case we have created an **interface **named **PackageDelivery.**

![Package Delivery interface](https://cdn-images-1.medium.com/max/2412/1*XBTTzPLh-HaOBe4cW9d9xQ.png)

We have created subclasses that are implemented by the PackageDelivery interface.

**1 →Delivery by bicycle🏍**

![Delivery By Bicycle](https://cdn-images-1.medium.com/max/3364/1*OnHfj0euWnT44Ossx9NJbA.png)

**2 →Delivery by Car**🚗

![Delivery By Car](https://cdn-images-1.medium.com/max/3048/1*_LSj9cVD3AcGe85FCnMjZA.png)

**3 →Delivery by Truck**🚚

![Delivery By Truck](https://cdn-images-1.medium.com/max/3220/1*SkHlDVmDKsvNUpDtduuamg.png)

**Step 3→ What will be my Factory Class or Manager Class?**

In Example, a Delivery Manager will be a factory Class or manager class that is responsible for choosing the delivery method based on package size. According to the definition, we are **letting the subclasses decide which class to instantiate.**

![image2](https://cdn-images-1.medium.com/max/2820/1*_m88DzHukztmEedH3lzFpQ.png)

**Step 4→ How to test the Factory Design Pattern?**

To test our Factory Design Pattern we are giving 3 different packages with different 3 different Packages sizes like **SMALL, LARGE and MEDIUM**

![image2](https://cdn-images-1.medium.com/max/3524/1*rpLjOp8fAMn_oqXF60Cqbw.png)

**Step 5 →What will be the Output for Factory Pattern?**

Below console output helps to understand how the Manager(Factory Class) is selecting the mode of transportation based on Package Info

![image3](https://cdn-images-1.medium.com/max/2484/1*QNUM6dlmIMFXQ8ZVhliRGQ.png)

### 🏭Where to find the Codebase for Factory Design Pattern?

[**On Github](https://github.com/Rohan2596)**
[**GitHub - Rohan2596/Design-pattern-examples at pattern/factory**](https://github.com/Rohan2596/Design-pattern-examples/tree/pattern/factory)

### Conclusion:-

In the article, we tried to answer some questions related to Design Pattern and especially **Factory Design Pattern** how it helps developers around the world to write better code and build faster applications. Factory Design Pattern allows us to implement the **SOLID **principle effectively may it is **loose-coupling or Single-responsibility.**

Please do share and like💖 if you find the article useful. Follow me on medium [Rohan Ravindra Kadam](undefined) and on Twitter at [**rohankadam25](https://twitter.com/rohankadam25)**

### 📚Bibliography:-
[**5 reasons to finally learn design patterns**](https://www.oreilly.com/content/5-reasons-to-finally-learn-design-patterns/)
[**Factory Method**](https://refactoring.guru/design-patterns/factory-method)

![Thank You, Viewers — Rohan Kadam](https://cdn-images-1.medium.com/max/2800/1*mRwSuSvhuRcZKS3uElP6bg.png)