{"id":5873,"date":"2022-07-01T04:29:52","date_gmt":"2022-07-01T04:29:52","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=5873"},"modified":"2022-07-01T04:29:52","modified_gmt":"2022-07-01T04:29:52","slug":"builder-pattern-in-python","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/builder-pattern-in-python\/","title":{"rendered":"Builder Pattern in Python"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>In the Python world, the Builder pattern is well-known. It&#8217;s very beneficial when you need to construct an object with a lot of configurable options. Builder is a creational design pattern that allows you to build complex objects step by step.<\/p>\n<h3>Builder Method<\/h3>\n<p>The Builder pattern is found in a class that includes a single construction function as well as numerous methods for configuring the final item. Builder approaches frequently enable chaining.<br \/>\nUsing the same construction code, we can easily create different types and representations of the object. It is primarily intended to increase the flexibility of solutions to various object creation problems in object-oriented programming.<\/p>\n<p><strong>Example<\/strong><br \/>\n<code>from __future__ import annotations<br \/>\nfrom abc import ABC, abstractmethod<br \/>\nfrom typing import Any<br \/>\nclass Software(ABC):<br \/>\n    @property<br \/>\n    @abstractmethod<br \/>\n    def product(self) -> None:<br \/>\n        pass<br \/>\n    @abstractmethod<br \/>\n    def functionality_a(self) -> None:<br \/>\n        pass<br \/>\n    @abstractmethod<br \/>\n    def functionality_b(self) -> None:<br \/>\n        pass<br \/>\n    @abstractmethod<br \/>\n    def functionality_c(self) -> None:<br \/>\n        pass<br \/>\nclass Builder(Software):<br \/>\n    def __init__(self) -> None:<br \/>\n        self.reset()<br \/>\n    def reset(self) -> None:<br \/>\n        self._product = Software_Product1()<br \/>\n    @property<br \/>\n    def product(self) -> Software_Product1:<br \/>\n        product = self._product<br \/>\n        self.reset()<br \/>\n        return product<br \/>\n    def functionality_a(self) -> None:<br \/>\n        self._product.add(\"functionality_A1\")<br \/>\n    def functionality_b(self) -> None:<br \/>\n        self._product.add(\"functionality_B1\")<br \/>\n    def functionality_c(self) -> None:<br \/>\n        self._product.add(\"functionality_C1\")<br \/>\nclass Software_Product1():<br \/>\n    def __init__(self) -> None:<br \/>\n        self.parts = []\n    def add(self, part: Any) -> None:<br \/>\n        self.parts.append(part)<br \/>\n    def list_parts(self) -> None:<br \/>\n        print(f\"Software features: {', '.join(self.parts)}\", end=\"\")<br \/>\nclass Manager:<br \/>\n    def __init__(self) -> None:<br \/>\n        self._builder = None<br \/>\n    @property<br \/>\n    def builder(self) -> Software:<br \/>\n        return self._builder<br \/>\n    @builder.setter<br \/>\n    def builder(self, builder: Software) -> None:<br \/>\n        self._builder = builder<br \/>\n    def build_minimal_viable_product(self) -> None:<br \/>\n        self.builder.functionality_a()<br \/>\n    def build_full_featured_product(self) -> None:<br \/>\n        self.builder.functionality_a()<br \/>\n        self.builder.functionality_b()<br \/>\n        self.builder.functionality_c()<br \/>\nif __name__ == \"__main__\":<br \/>\n    manager = Manager()<br \/>\n    builder = Builder()<br \/>\n    manager.builder = builder<br \/>\n    print(\"Standard basic Software product: \")<br \/>\n    manager.build_minimal_viable_product()<br \/>\n    builder.product.list_parts()<br \/>\n    print(\"\\n\")<br \/>\n    print(\"Standard full featured Software product: \")<br \/>\n    manager.build_full_featured_product()<br \/>\n    builder.product.list_parts()<br \/>\n    print(\"\\n\")<br \/>\n    print(\"Custom Software product: \")<br \/>\n    builder.functionality_a()<br \/>\n    builder.functionality_b()<br \/>\n    builder.product.list_parts()<\/code><\/p>\n<p><strong>Output:<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/07\/code23.png\" alt=\"code23\"\/><\/p>\n<h3>Advantages:<\/h3>\n<ul>\n<li>We can alter how objects are internally represented. While creating the different product representations, we may apply the same construction code for other representations as well.<\/li>\n<li>Both the business logic and the complex construction code can be separated from one another.<\/li>\n<\/ul>\n<h3>Disadvantages:<\/h3>\n<ul>\n<li>It increases code complexity because the builder pattern necessitates the creation of several new classes.<\/li>\n<li>The builder class is mutable, as per the developers.<\/li>\n<\/ul>\n<p><small><em>People having good command over the French language can get a French certification from StudySection. StudySection offers both beginner level and expert level <a href=\"https:\/\/www.studysection.com\/french-language-and-concepts-advanced\">French Certification Exams<\/a> to test the ability to communicate in the French language.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In the Python world, the Builder pattern is well-known. It&#8217;s very beneficial when you need to construct an object<\/p>\n","protected":false},"author":1,"featured_media":5874,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[262,33],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Builder Pattern in Python - StudySection Blog<\/title>\n<meta name=\"description\" content=\"The Builder pattern is well-known. It&#039;s very beneficial when you need to construct an object with a lot of configurable options.\" \/>\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\/builder-pattern-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Builder Pattern in Python - StudySection Blog\" \/>\n<meta property=\"og:description\" content=\"The Builder pattern is well-known. It&#039;s very beneficial when you need to construct an object with a lot of configurable options.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/builder-pattern-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=\"2022-07-01T04:29:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/07\/Builder-Pattern.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\/builder-pattern-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/builder-pattern-in-python\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Builder Pattern in Python\",\"datePublished\":\"2022-07-01T04:29:52+00:00\",\"dateModified\":\"2022-07-01T04:29:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/builder-pattern-in-python\/\"},\"wordCount\":216,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"development\",\"Python\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/builder-pattern-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/builder-pattern-in-python\/\",\"url\":\"https:\/\/studysection.com\/blog\/builder-pattern-in-python\/\",\"name\":\"Builder Pattern in Python - StudySection Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2022-07-01T04:29:52+00:00\",\"dateModified\":\"2022-07-01T04:29:52+00:00\",\"description\":\"The Builder pattern is well-known. It's very beneficial when you need to construct an object with a lot of configurable options.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/builder-pattern-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/builder-pattern-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/builder-pattern-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Builder Pattern 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":"Builder Pattern in Python - StudySection Blog","description":"The Builder pattern is well-known. It's very beneficial when you need to construct an object with a lot of configurable options.","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\/builder-pattern-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Builder Pattern in Python - StudySection Blog","og_description":"The Builder pattern is well-known. It's very beneficial when you need to construct an object with a lot of configurable options.","og_url":"https:\/\/studysection.com\/blog\/builder-pattern-in-python\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2022-07-01T04:29:52+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/07\/Builder-Pattern.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\/builder-pattern-in-python\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/builder-pattern-in-python\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Builder Pattern in Python","datePublished":"2022-07-01T04:29:52+00:00","dateModified":"2022-07-01T04:29:52+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/builder-pattern-in-python\/"},"wordCount":216,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["development","Python"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/builder-pattern-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/builder-pattern-in-python\/","url":"https:\/\/studysection.com\/blog\/builder-pattern-in-python\/","name":"Builder Pattern in Python - StudySection Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2022-07-01T04:29:52+00:00","dateModified":"2022-07-01T04:29:52+00:00","description":"The Builder pattern is well-known. It's very beneficial when you need to construct an object with a lot of configurable options.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/builder-pattern-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/builder-pattern-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/builder-pattern-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Builder Pattern 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":289,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5873"}],"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=5873"}],"version-history":[{"count":1,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5873\/revisions"}],"predecessor-version":[{"id":5876,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5873\/revisions\/5876"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/5874"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=5873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=5873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=5873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}