liveBook · Manning

Builder pattern

As explained in Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform

The builder pattern is a design pattern in which inputs determine which components are created and how a complex result is assembled.

The builder pattern assembles a configurable set of infrastructure resources. Inputs determine which components are created, while defaults provide a reusable starting configuration. section 3.5

Use it when one system needs meaningful combinations of related resources. A database module, for example, can create a private or public load balancer and add the corresponding server, network, and firewall configuration. section 3.5

A practical implementation separates resource customization from system composition:

  • Create factory modules for individual resources such as database servers, load balancers, and firewall rules. section 3.5
  • Initialize the builder with required values and default resources. section 3.5
  • Expose operations that enable or disable resource groups, such as internal or external load balancing. section 3.5
  • Choose the pattern by assessing reuse, update frequency, purpose, and whether multiple resources must be composed. section 3.6

Database module with selectable exposure

A DatabaseModule starts with a database server, then extends its resource collection with either an internal or external load balancer. The selected factory supplies the matching forwarding rule and configuration. section 3.5

Figure 3.9 A builder module for the database would include parameters to select the type of load balancer and firewall rules that the module must create.

Shows how builder parameters select the load balancer type and the firewall rules that must be created with it. section 3.5

How Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform covers it

How deep it goes
Explains definitions, factory composition, implementation operations, and pattern selection, but does not detail complete production deployment.
Examples
Uses database systems, private and public load balancers, forwarding rules, firewall rules, and a Python DatabaseModule implementation.
What it assumes
Assumes familiarity with infrastructure resources and modules while defining selection-based assembly directly.
What you can do after
Build it from scratch by composing factory modules into configurable infrastructure systems.
How it connects
Connects builder modules with factory, prototype, singleton, and composite patterns for infrastructure modularization.

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 Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform. The section links point to the parts of the book it draws on.