liveBook · Manning

Builder pattern

As explained in AI-Powered Developer: Build software with ChatGPT and Copilot

The Builder Pattern is a creational design pattern that constructs objects through a sequence of steps, often exposed through a fluent interface.

The Builder Pattern is a creational design pattern that constructs objects through step-by-step instructions exposed through a fluent API. It helps reduce duplicated construction logic in factories and makes object assembly easier to read. section 4.2.2 section 4.2.3

A builder stores construction values in local attributes, offers with_... methods that return self for chaining, and finishes with build(). AssetBuilder selects Hardware or Software from category, while FundingDetailsBuilder creates the related FundingDetails object. section 4.2.3

A practical builder-based construction flow is:

  • Create an AssetBuilder and assign values such as name, category, cost, useful life, status, salvage value, and purchase date. section 4.2.3
  • Chain the with_... methods, which return the builder itself so successive configuration calls remain readable. section 4.2.3
  • Call build() to return the appropriate Asset subclass based on category, then configure FundingDetailsBuilder for depreciation and department allocations. section 4.2.3
  • Move depreciation-strategy name mapping into the funding-details builder, returning the corresponding strategy object or raising ValueError for an unknown name. section 4.2.3

Constructing an asset

An AssetFactory can chain AssetBuilder calls for the asset type and values, call build(), chain FundingDetailsBuilder calls for depreciation settings, build the funding details, attach them to the asset, and return it. This reduces the updated factory method to a small orchestration role. section 4.2.3

# Create a class called AssetBuilder
# It should use the Builder pattern to build an Asset
# Create another class called FundingDetailsBuilder
# It should use the Builder pattern to build a FundingDetails
# The AssetBuilder should have an embedded FundingDetailsBuilder
# When the category is "hardware" the AssetBuilder should create a Hardware object
# When the category is "software" the AssetBuilder should create a Software object
# When depreciation_strategy is equal to "straight_line" it should return a new instance of StraightLineDepreciationStrategy
# When depreciation_strategy is equal to "double_declining_balance" it should return a new instance of DoubleDecliningDepreciationStrategy
# When depreciation_strategy is equal to "none" it should return a new instance of NoDepreciationStrategy
# When depreciation_strategy is equal to "declining_balance" it should return a new instance of DecliningBalanceDepreciationStrategy
# The variables will need to be held in local variables and then passed to the Asset and FundingDetails objects when they are created.
# The final method of the AssetBuilder should return an Asset and be called build().
# The final method of the FundingDetailsBuilder should return a FundingDetails and be called build().

Use the builder specification as a starting prompt for generating AssetBuilder and FundingDetailsBuilder implementations, including subclass selection and depreciation-strategy mapping. section 4.2.3

How AI-Powered Developer: Build software with ChatGPT and Copilot covers it

How deep it goes
Explains fluent setters, local state, polymorphic build methods, nested builders, and strategy mapping, without broader pattern theory.
Examples
Uses AssetBuilder, FundingDetailsBuilder, Hardware, Software, AssetFactory, and depreciation-strategy selection as construction examples.
What it assumes
Assumes familiarity with factories, Asset subclasses, FundingDetails, fluent methods, and depreciation-strategy objects.
What you can do after
Build it via GitHub Copilot by specifying fluent builders, subclass selection, strategy mapping, and factory integration.
How it connects
Connects object construction with the Factory Pattern, polymorphism, the Adapter Pattern, and the Depreciation Strategy pattern.

Also explained in

Compare how 6 books explain builder pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Developer: Build software with ChatGPT and Copilot. The section links point to the parts of the book it draws on.