{"id":2936,"date":"2020-06-24T09:29:09","date_gmt":"2020-06-24T09:29:09","guid":{"rendered":"https:\/\/eluminoustechnologies.com\/blog\/?p=2936"},"modified":"2025-06-12T13:01:17","modified_gmt":"2025-06-12T13:01:17","slug":"case-study-factory-design-pattern","status":"publish","type":"post","link":"https:\/\/eluminoustechnologies.com\/blog\/case-study-factory-design-pattern\/","title":{"rendered":"Understanding Factory Method with a Factory Design Pattern Case Study"},"content":{"rendered":"<p>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\u00a0 method design, when should you use it, and its pros and cons.<\/p>\n<p>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.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"#\" data-href=\"https:\/\/eluminoustechnologies.com\/blog\/case-study-factory-design-pattern\/#what-is-factory-design-pattern\" >What is Factory Design Pattern?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"#\" data-href=\"https:\/\/eluminoustechnologies.com\/blog\/case-study-factory-design-pattern\/#factory-design-pattern-example\" >Factory Design Pattern Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"#\" data-href=\"https:\/\/eluminoustechnologies.com\/blog\/case-study-factory-design-pattern\/#when-to-use-factory-design-pattern\" >When to Use Factory Design Pattern?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"#\" data-href=\"https:\/\/eluminoustechnologies.com\/blog\/case-study-factory-design-pattern\/#what-are-the-pros-and-cons-of-factory-design-pattern\" >What are the Pros and Cons of Factory Design Pattern?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"#\" data-href=\"https:\/\/eluminoustechnologies.com\/blog\/case-study-factory-design-pattern\/#summing-it-up\" >Summing It Up<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"what-is-factory-design-pattern\"><\/span>What is Factory Design Pattern?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The factory method ranks among the most used design patterns in software development. As a creational design pattern, it\u2019s 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.<\/p>\n<p>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.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"factory-design-pattern-example\"><\/span>Factory Design Pattern Example<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3>Problem<\/h3>\n<p>To understand how factory design pattern works, let us look at a sample case study. Let\u2019s assume that you hire an <u>IT business solution provider<\/u> to develop an app that allows users to rent vehicles. In the initial days of your operation, you might start with only car rentals.<\/p>\n<p>This can lead to a situation where your entire application is built around car rentals with the majority of the code in the \u2018Car\u2019 class. Now, if you expand your business and decide to offer bike rentals as well, a lot of changes will be required in the \u2018Car\u2019 class as that houses the entire login.<\/p>\n<p>It\u2019s obvious what will happen to your app when you decide to offer additional vehicle types to your users \u2013 its underlying code will become too complex, unwieldy, and messy.<\/p>\n<h3>Solution<\/h3>\n<p>This is where the factory method comes to your rescue. It lets you replace the object creation (using the new operator) with a <em>factory<\/em> method. Yes, the objects are still created using the new operator but in this case, it\u2019s called from within the factory method.<\/p>\n<p><img decoding=\"async\" class=\"wp-image-2939 aligncenter lazyload\" data-src=\"https:\/\/b4130876.smushcdn.com\/4130876\/wp-content\/uploads\/2020\/06\/factory-design-patter-test-cases.png-1.jpg?lossy=2&strip=1&webp=1\" alt=\"factory design patter test cases.png\" width=\"647\" height=\"392\" title=\"\" data-srcset=\"https:\/\/b4130876.smushcdn.com\/4130876\/wp-content\/uploads\/2020\/06\/factory-design-patter-test-cases.png-1.jpg?lossy=2&amp;strip=1&amp;webp=1 647w, https:\/\/b4130876.smushcdn.com\/4130876\/wp-content\/uploads\/2020\/06\/factory-design-patter-test-cases.png-1.jpg?size=128x78&amp;lossy=2&amp;strip=1&amp;webp=1 128w, https:\/\/b4130876.smushcdn.com\/4130876\/wp-content\/uploads\/2020\/06\/factory-design-patter-test-cases.png-1.jpg?size=256x155&amp;lossy=2&amp;strip=1&amp;webp=1 256w, https:\/\/b4130876.smushcdn.com\/4130876\/wp-content\/uploads\/2020\/06\/factory-design-patter-test-cases.png-1.jpg?size=384x233&amp;lossy=2&amp;strip=1&amp;webp=1 384w, https:\/\/b4130876.smushcdn.com\/4130876\/wp-content\/uploads\/2020\/06\/factory-design-patter-test-cases.png-1.jpg?size=512x310&amp;lossy=2&amp;strip=1&amp;webp=1 512w\" data-sizes=\"auto\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 647px; --smush-placeholder-aspect-ratio: 647\/392;\" data-original-sizes=\"(max-width: 647px) 100vw, 647px\"><\/p>\n<p>As this UML diagram shows, among the main factory design pattern advantages, is that both the \u2018Car\u2019 and \u2018Bike\u2019 classes implement a \u2018Rent\u2019 interface, which in turn declares the method <em>getVehicleDetails<\/em>(). This enables each class to implement this method differently.<\/p>\n<h3>For instance, the \u2018Car\u2019 class will have details such as<\/h3>\n<ul>\n<li>auto gear, non-auto gear<\/li>\n<li>sedan, hatchback, SUV etc.<\/li>\n<\/ul>\n<h3>Similarly, the \u2018Bike\u2019 class will have details such as,<\/h3>\n<ul>\n<li>gear, non-gear<\/li>\n<li>two-stroke engine, four-stroke engine etc.<\/li>\n<\/ul>\n<p>The factory method in CarRent class will return the objects for cars and the factory method in BikeRent class will return objects for bikes.<\/p>\n<p>In this example of factory pattern design, the code using the factory method is known as the client code. The client code doesn\u2019t 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 <em>getVehicleDetails <\/em>method. \u00a0In other words, the client does not know how the method works.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"when-to-use-factory-design-pattern\"><\/span>When to Use Factory Design Pattern?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Your <u><a href=\"https:\/\/eluminoustechnologies.com\/front-end-development\/\" target=\"_blank\" rel=\"noopener\">Front-end development<\/a> team<\/u>\u00a0should use the factory method if,<\/p>\n<ul>\n<li>the exact types and dependencies of the objects that your code will work with are yet to be decided.<\/li>\n<li>you want to offer users of your framework or library a way to extend its internal components<\/li>\n<li>you prefer reusing existing objects and saving system resources rather than rebuilding objects every time.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"what-are-the-pros-and-cons-of-factory-design-pattern\"><\/span>What are the Pros and Cons of Factory Design Pattern?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>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 <u>hire dedicated developer<\/u> for your project.<\/p>\n<h3>Pros:<\/h3>\n<ul>\n<li>Allows object creation at run time.<\/li>\n<li>Avoids tight coupling.<\/li>\n<li>Removes redundant code.<\/li>\n<li>Follows single responsibility and Open\/Close principles.<\/li>\n<\/ul>\n<h3>Cons:<\/h3>\n<ul>\n<li>Code can become overly complicated if too many sub-classes and interfaces are added.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"summing-it-up\"><\/span>Summing It Up<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>It\u2019s 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.<\/p>\n<p>As an\u00a0<u>IT business solution<\/u><u>\u00a0provider<\/u> for close to two decades, we\u2019ve used factory design pattern to create thousands of such applications. Our <a href=\"https:\/\/eluminoustechnologies.com\/front-end-development\/\" target=\"_blank\" rel=\"noopener\"><u>Front-end development <\/u><\/a><u>team<\/u> studies your current and future business requirements in detail before committing to the development process, resulting in software that\u2019s scalable, maintainable, and cost-efficient. For details on how to <u>hire dedicated developer<\/u> to create an app, get in touch with us now.<\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":7,"featured_media":2937,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[91,155],"tags":[312,329,326,328,325,324,327],"class_list":["post-2936","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mobile-apps","category-web-development","tag-app-development-company","tag-case-study-for-factory-design-pattern","tag-dedicated-development-team","tag-factory-design-pattern","tag-hire-dedicated-resources","tag-it-business-solution-provider","tag-software-design-patterns"],"acf":[],"_links":{"self":[{"href":"https:\/\/eluminoustechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/2936","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eluminoustechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eluminoustechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eluminoustechnologies.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/eluminoustechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=2936"}],"version-history":[{"count":0,"href":"https:\/\/eluminoustechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/2936\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eluminoustechnologies.com\/blog\/wp-json\/wp\/v2\/media\/2937"}],"wp:attachment":[{"href":"https:\/\/eluminoustechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=2936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eluminoustechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=2936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eluminoustechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=2936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}