Understanding Factory Method with a Factory Design Pattern Case Study
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 design method, when should you use it, and its pros and cons.
We also use a real-life factory design pattern case study to understand how factory method makes it easier to develop complex software while keeping the complexity under control.
What is Factory Design Pattern?
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. 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 dedicated 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 resources for your project.
- Allows object creation at run time.
- Avoids tight coupling.
- Removes redundant code.
- Follows single responsibility and Open/Close principles.
- 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 dedicated 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 resources 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.