In our previous blog post, we looked at software design patterns and the various benefits of using them. In this blog post, we delve deeper into the factoryย method design, when should you use it, and its pros and cons.
We also use a real-life factory design pattern case study to understand how method design pattern makes it easier to develop complex software while keeping the complexity under control.
What is Factory Design Pattern?
The factory method ranks among the most used design patterns in software development. As a creational design pattern, itโs used to create objects but without providing a specific class type for it. Rather we only define the interface or an abstract class while letting a sub-class decide which class to instantiate and do a little modification in the Factory class. In other words, the sub-classes decide which type of object is to be used.
Use of factory pattern design makes your software design more flexible without adding too much complexity. While other design patterns need new classes, the factory method only needs a new operation.
Factory Design Pattern Example
Problem
To understand how factory design pattern works, let us look at a sample case study. Letโs assume that you hire an IT business solution provider to develop an app that allows users to rent vehicles. In the initial days of your operation, you might start with only car rentals.
This can lead to a situation where your entire application is built around car rentals with the majority of the code in the โCarโ class. Now, if you expand your business and decide to offer bike rentals as well, a lot of changes will be required in the โCarโ class as that houses the entire login.
Itโs obvious what will happen to your app when you decide to offer additional vehicle types to your users โ its underlying code will become too complex, unwieldy, and messy.
Solution
This is where the factory method comes to your rescue. It lets you replace the object creation (using the new operator) with a factory method. Yes, the objects are still created using the new operator but in this case, itโs called from within the factory method.
As this UML diagram shows, among the main factory design pattern advantages, is that both the โCarโ and โBikeโ classes implement a โRentโ interface, which in turn declares the method getVehicleDetails(). This enables each class to implement this method differently.
For instance, the โCarโ class will have details such as
- auto gear, non-auto gear
- sedan, hatchback, SUV etc.
Similarly, the โBikeโ class will have details such as,
- gear, non-gear
- two-stroke engine, four-stroke engine etc.
The factory method in CarRent class will return the objects for cars and the factory method in BikeRent class will return objects for bikes.
In this example of factory pattern design, the code using the factory method is known as the client code. The client code doesnโt see any difference in the actual products returned by sub-classes. Rather, it treats all objects similarly and the only thing it knows is that each of those objects will have the getVehicleDetails method. ย In other words, the client does not know how the method works.
When to Use Factory Design Pattern?
Your Front-end development teamย should use the factory method if,
- the exact types and dependencies of the objects that your code will work with are yet to be decided.
- you want to offer users of your framework or library a way to extend its internal components
- you prefer reusing existing objects and saving system resources rather than rebuilding objects every time.
What are the Pros and Cons of Factory Design Pattern?
The flexibility and modularity inherent to factory design pattern do not make it an automatic choice for your project. While those very reasons are why factory design pattern is so popular, you should carefully weigh the pros and cons of factory method before you hire dedicated developer for your project.
Pros:
- Allows object creation at run time.
- Avoids tight coupling.
- Removes redundant code.
- Follows single responsibility and Open/Close principles.
Cons:
- Code can become overly complicated if too many sub-classes and interfaces are added.
Summing It Up
Itโs easy to understand why factory method pattern is so often prescribed to solve problems during software development. By separating the object creation logic from the object usage logic, factory design pattern makes it possible to create different types of objects at run time. Apart from making your code leaner and your development faster, this attribute also makes it easier to add or change object creation, thus helping your application achieve scale quickly.
As anย IT business solutionย provider for close to two decades, weโve used factory design pattern to create thousands of such applications. Our Front-end development team studies your current and future business requirements in detail before committing to the development process, resulting in software thatโs scalable, maintainable, and cost-efficient. For details on how to hire dedicated developer to create an app, get in touch with us now.
Thank you for reading! If you liked this post, do subscribe to get crisp and informative blog posts delivered straight to your inbox every week. Our posts touch a variety of topics including web and mobile development, data analytics, business intelligence, and eCommerce.
Digital Marketing Manager
Responsible for developing and managing web presence, Sarah has been associated with eLuminous Technologies for 7+ years. Strategic and innovative with a passion for Content Marketing and enhancing brand awareness. Administered all business marketing operations and advertisement campaigns that eventually increased web traffic. She works under the motto โThink like a Publisher, not a Marketer.โ