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)?
🏭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?
Factory allows us to follow the Design Principle (SOLID).
It fosters loose-coupling by eliminating the need to bind application-specific classes into the code.
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.
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.
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.
We have created subclasses that are implemented by the PackageDelivery interface.
1 →Delivery by bicycle🏍
2 →Delivery by Car🚗
3 →Delivery by Truck🚚
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.
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
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
🏭Where to find the Codebase for Factory Design Pattern?
**On Github**
GitHub - Rohan2596/Design-pattern-examples at 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 and on Twitter at **rohankadam25**
Top comments (0)