{"id":5379,"date":"2021-12-22T04:37:34","date_gmt":"2021-12-22T04:37:34","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=5379"},"modified":"2021-12-22T05:16:18","modified_gmt":"2021-12-22T05:16:18","slug":"factory-method-in-python","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/factory-method-in-python\/","title":{"rendered":"Factory Method in Python"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>Design patterns are the tried and tested software design templates that are used to solve problems in <a href=\"https:\/\/studysection.com\/blog\/seven-principles-of-software-testing\/\">software<\/a> development efficiently. Software developers practice these design patterns to make their code more scalable and maintainable. There are the following three categories of design patterns:<\/p>\n<ul>\n<li>Creational Patterns<\/li>\n<li>Structural Patterns<\/li>\n<li>Behavioral Patterns<\/li>\n<\/ul>\n<p>Creational Patterns provide a mechanism to create classes and objects in an efficient way to increase the code reusability.<br \/>\nStructural patterns are followed to arrange classes and objects to form a large, flexible structure.<br \/>\nBehavioral Patterns algorithmically explain the communication patterns between objects.<\/p>\n<h3>Factory Method<\/h3>\n<p>The factory method is a creational design pattern that provides a common interface for creating objects. It enhances the loose coupling by creating an abstract function (a factory method) that will be used to create different types of objects depending upon the input we give. Instead of writing code to create objects of each and every class we have, now we just call factory methods that will take care of creating objects of the required class.<\/p>\n<h3>Example<\/h3>\n<p>In the following example, the function &#8220;shape_factory_method&#8221; is used to create objects of different shapes. We can extend our code to any type of shape by only adding a class for that shape without tempering the existing code. Here, the factory method &#8221;<br \/>\n<code>class Circle:<br \/>\n   def area(radius):<br \/>\n       output = 3.14 * pow(radius, 2)<br \/>\n       return(\"Area of circle =\" + str(output))<br \/>\nclass Sphere:<br \/>\n   def area(radius):<br \/>\n       output = 4* 3.14 * pow(radius, 2)<br \/>\n       return(\"Area of sphere =\" + str(output))<br \/>\nclass Square:<br \/>\n   def area(side):<br \/>\n       output = side * side<br \/>\n       return(\"Area of square =\" + str(output))<br \/>\nclass Rectangle:<br \/>\n   def area(length,width):<br \/>\n       output = length * width<br \/>\n       return(\"Area of rectangle =\" + str(output))<br \/>\ndef shape_factory_method(shape = \"circle\"):<br \/>\n   shape = str(shape).lower()<br \/>\n   shape_obj = {<br \/>\n       \"circle\":Circle,<br \/>\n       \"sphere\":Sphere,<br \/>\n       \"square\":Square,<br \/>\n       \"rectangle\":Rectangle<br \/>\n   }<br \/>\n   return shape_obj[shape]\nif __name__ == \"__main__\":<br \/>\n   shape_object = shape_factory_method(\"sphere\")<br \/>\n   result = shape_object.area(6)<br \/>\n   print(result)<br \/>\n   shape_object = shape_factory_method(\"square\")<br \/>\n   result = shape_object.area(100)<br \/>\n   print(result)<\/code><\/p>\n<p><strong>Advantages:<\/strong><\/p>\n<ul>\n<li>It increases flexibility and reusability by which we can extend our code by just adding its class without tempering the entire code.<\/li>\n<li>Enhances loose coupling of code.<\/li>\n<\/ul>\n<p><strong>Disadvantage:<\/strong><\/p>\n<ul>\n<li>In some situations where many classes are involved, it will suffer from less readability of code.<\/li>\n<\/ul>\n<p><small><em>StudySection gives an opportunity to beginners and experts in .NET framework to go through StudySection\u2019s <a href=\"https:\/\/www.studysection.com\/c-sharp-net-web-developer-advanced-diploma\">.NET Certification Exam<\/a> and get a .NET certification for enhancement of career in programming. If you have knowledge of the .NET framework then you can get a certificate through an online exam at StudySection.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Design patterns are the tried and tested software design templates that are used to solve problems in software development<\/p>\n","protected":false},"author":1,"featured_media":5380,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[33],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Factory Method in Python - StudySection Blog<\/title>\n<meta name=\"description\" content=\"The factory method is a creational design pattern that provides a common interface for creating objects. Enhances loose coupling of code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/factory-method-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Factory Method in Python - StudySection Blog\" \/>\n<meta property=\"og:description\" content=\"The factory method is a creational design pattern that provides a common interface for creating objects. Enhances loose coupling of code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/factory-method-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-22T04:37:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-22T05:16:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/12\/Python.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/factory-method-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/factory-method-in-python\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Factory Method in Python\",\"datePublished\":\"2021-12-22T04:37:34+00:00\",\"dateModified\":\"2021-12-22T05:16:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/factory-method-in-python\/\"},\"wordCount\":315,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"Python\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/factory-method-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/factory-method-in-python\/\",\"url\":\"https:\/\/studysection.com\/blog\/factory-method-in-python\/\",\"name\":\"Factory Method in Python - StudySection Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2021-12-22T04:37:34+00:00\",\"dateModified\":\"2021-12-22T05:16:18+00:00\",\"description\":\"The factory method is a creational design pattern that provides a common interface for creating objects. Enhances loose coupling of code.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/factory-method-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/factory-method-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/factory-method-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Factory Method in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Factory Method in Python - StudySection Blog","description":"The factory method is a creational design pattern that provides a common interface for creating objects. Enhances loose coupling of code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/factory-method-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Factory Method in Python - StudySection Blog","og_description":"The factory method is a creational design pattern that provides a common interface for creating objects. Enhances loose coupling of code.","og_url":"https:\/\/studysection.com\/blog\/factory-method-in-python\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2021-12-22T04:37:34+00:00","article_modified_time":"2021-12-22T05:16:18+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/12\/Python.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/factory-method-in-python\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/factory-method-in-python\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Factory Method in Python","datePublished":"2021-12-22T04:37:34+00:00","dateModified":"2021-12-22T05:16:18+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/factory-method-in-python\/"},"wordCount":315,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["Python"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/factory-method-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/factory-method-in-python\/","url":"https:\/\/studysection.com\/blog\/factory-method-in-python\/","name":"Factory Method in Python - StudySection Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2021-12-22T04:37:34+00:00","dateModified":"2021-12-22T05:16:18+00:00","description":"The factory method is a creational design pattern that provides a common interface for creating objects. Enhances loose coupling of code.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/factory-method-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/factory-method-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/factory-method-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Factory Method in Python"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":417,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5379"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=5379"}],"version-history":[{"count":2,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5379\/revisions"}],"predecessor-version":[{"id":5382,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5379\/revisions\/5382"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/5380"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=5379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=5379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=5379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}