Author - StudySection Post Views - 146 views
Builder Pattern

Builder Pattern in Python

Introduction

In the Python world, the Builder pattern is well-known. It’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.

Builder Method

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.
Using 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.

Example
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Any
class Software(ABC):
@property
@abstractmethod
def product(self) -> None:
pass
@abstractmethod
def functionality_a(self) -> None:
pass
@abstractmethod
def functionality_b(self) -> None:
pass
@abstractmethod
def functionality_c(self) -> None:
pass
class Builder(Software):
def __init__(self) -> None:
self.reset()
def reset(self) -> None:
self._product = Software_Product1()
@property
def product(self) -> Software_Product1:
product = self._product
self.reset()
return product
def functionality_a(self) -> None:
self._product.add("functionality_A1")
def functionality_b(self) -> None:
self._product.add("functionality_B1")
def functionality_c(self) -> None:
self._product.add("functionality_C1")
class Software_Product1():
def __init__(self) -> None:
self.parts = [] def add(self, part: Any) -> None:
self.parts.append(part)
def list_parts(self) -> None:
print(f"Software features: {', '.join(self.parts)}", end="")
class Manager:
def __init__(self) -> None:
self._builder = None
@property
def builder(self) -> Software:
return self._builder
@builder.setter
def builder(self, builder: Software) -> None:
self._builder = builder
def build_minimal_viable_product(self) -> None:
self.builder.functionality_a()
def build_full_featured_product(self) -> None:
self.builder.functionality_a()
self.builder.functionality_b()
self.builder.functionality_c()
if __name__ == "__main__":
manager = Manager()
builder = Builder()
manager.builder = builder
print("Standard basic Software product: ")
manager.build_minimal_viable_product()
builder.product.list_parts()
print("\n")
print("Standard full featured Software product: ")
manager.build_full_featured_product()
builder.product.list_parts()
print("\n")
print("Custom Software product: ")
builder.functionality_a()
builder.functionality_b()
builder.product.list_parts()

Output:
code23

Advantages:

  • 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.
  • Both the business logic and the complex construction code can be separated from one another.

Disadvantages:

  • It increases code complexity because the builder pattern necessitates the creation of several new classes.
  • The builder class is mutable, as per the developers.

People having good command over the French language can get a French certification from StudySection. StudySection offers both beginner level and expert level French Certification Exams to test the ability to communicate in the French language.

Leave a Reply

Your email address will not be published.