Lädt...


🔧 Go Design Patterns #2: Builder


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Builder

The Builder Pattern is a creational design pattern that provides a flexible solution for constructing complex objects step by step. It allows you to create different representations of an object using the same construction process.

Problem Statement

  • When creating an object involves multiple steps or parameters, managing the construction process can become cumbersome.
  • You want to avoid a "telescoping constructor" problem (where constructors take many parameters) and ensure that your object is immutable after creation.

Benefits of the Builder Pattern

  • Flexibility: Easily construct different representations of an object.
  • Readability: Method chaining makes the construction process clear and concise.
  • Encapsulation: The construction logic is encapsulated within the builder, separating it from the client code.

Real-World Example

Consider building a computer. A computer can have various components like CPU, RAM, storage, etc.

Instead of requiring all these parameters in a constructor, the builder pattern allows you to set each component step by step.

Implementation

package main

import "fmt"

// Product
type Computer struct {
    CPU     string
    RAM     string
    Storage string
    OS      string
}

// Builder Interface
type ComputerBuilder interface {
    SetCPU(cpu string) ComputerBuilder
    SetRAM(ram string) ComputerBuilder
    SetStorage(storage string) ComputerBuilder
    SetOS(os string) ComputerBuilder
    Build() *Computer
}

// Concrete Builder
type PCBuilder struct {
    computer *Computer
}

func NewPCBuilder() *PCBuilder {
    return &PCBuilder{&Computer{}}
}

func (b *PCBuilder) SetCPU(cpu string) ComputerBuilder {
    b.computer.CPU = cpu
    return b
}

func (b *PCBuilder) SetRAM(ram string) ComputerBuilder {
    b.computer.RAM = ram
    return b
}

func (b *PCBuilder) SetStorage(storage string) ComputerBuilder {
    b.computer.Storage = storage
    return b
}

func (b *PCBuilder) SetOS(os string) ComputerBuilder {
    b.computer.OS = os
    return b
}

func (b *PCBuilder) Build() *Computer {
    return b.computer
}

// Client Code
func main() {
    builder := NewPCBuilder()

    computer := builder.
        SetCPU("Intel i7").
        SetRAM("16GB").
        SetStorage("1TB SSD").
        SetOS("Windows 7").
        Build()

    fmt.Printf("Computer built: %+v\n", computer)
}

Product (Computer): Represents the complex object that will be built with various attributes.

Builder Interface (ComputerBuilder): Defines methods for setting each component of the product.

Concrete Builder (PCBuilder): Implements the builder interface.

  • Provides methods to set CPU, RAM, Storage, and OS, returning the builder itself for method chaining.
  • The Build() method returns the final Computer object.

Client Code: Creates a new builder instance.

  • Configures the computer step by step using method chaining.
  • Calls Build() to retrieve the final product.
...

🔧 Design Patterns in Microservices. Chapter 1: Introduction to Microservices Design Patterns


📈 33.45 Punkte
🔧 Programmierung

🔧 DESIGN PATTERNS : A Deep Dive into Common Design Patterns


📈 33.45 Punkte
🔧 Programmierung

🔧 Design Patterns vs. Architectural Patterns: Stop the Confusion


📈 28.2 Punkte
🔧 Programmierung

🔧 CI/CD Software Design Patterns and Anti-Patterns


📈 28.2 Punkte
🔧 Programmierung

🔧 Design Patterns in JavaScript: Creational Patterns


📈 28.2 Punkte
🔧 Programmierung

🔧 JavaScript Design Patterns: Mastering Creational, Structural, And Behavioral Patterns For Cleaner Code


📈 28.2 Punkte
🔧 Programmierung

🔧 Getting Started with Object-Oriented Design (Part 2): Design Principles and Design Patterns


📈 27.21 Punkte
🔧 Programmierung

🔧 Go Design Patterns #2: Builder


📈 26.4 Punkte
🔧 Programmierung

🔧 Design Patterns - Builder


📈 26.4 Punkte
🔧 Programmierung

🐧 [$] Security patterns and anti-patterns in embedded development


📈 22.96 Punkte
🐧 Linux Tipps

🔧 Machine Learning Patterns and Anti-Patterns


📈 22.96 Punkte
🔧 Programmierung

🍏 Patterns 1.3 - Build patterns quickly and effortlessly with syntax coloring.


📈 22.96 Punkte
🍏 iOS / Mac OS

🔧 Continuous Integration Patterns and Anti-Patterns


📈 22.96 Punkte
🔧 Programmierung

🔧 Flow & Cadence Best Practices, Patterns, and Anti-Patterns


📈 22.96 Punkte
🔧 Programmierung

🔧 System design: Design Patterns in System Architecture


📈 21.97 Punkte
🔧 Programmierung

🔧 SOLID Design Principles and Design Patterns Crash Course


📈 21.97 Punkte
🔧 Programmierung

🔧 Top 6 System Design Patterns to Ace Every System Design Interview


📈 21.97 Punkte
🔧 Programmierung

🔧 Design Patterns Are A Better Way To Collaborate On Your Design System


📈 21.97 Punkte
🔧 Programmierung

🕵️ Medium CVE-2019-15534: Raml-module-builder project Raml-module-builder


📈 19.36 Punkte
🕵️ Sicherheitslücken

🔧 Builder.io vs Webflow: A Beginner's Guide to Choosing the Best Website Builder


📈 19.36 Punkte
🔧 Programmierung

🕵️ CVE-2024-34683 | SAP Document Builder up to 748 Builder Service unrestricted upload


📈 19.36 Punkte
🕵️ Sicherheitslücken

📰 Divi Builder: Tutorial für den WordPress Builder


📈 19.36 Punkte
🖥️ Betriebssysteme

📰 WordPress-Builder: Das sind die besten Page-Builder-Plugins


📈 19.36 Punkte
🖥️ Betriebssysteme

📰 Best ecommerce website builder and online store builder in 2021


📈 19.36 Punkte
📰 IT Nachrichten

🕵️ Low CVE-2020-13423: Form builder for magento 2 project Form builder for magento 2


📈 19.36 Punkte
🕵️ Sicherheitslücken

🕵️ Profile Builder and Profile Builder Pro < 3.1.1 - User Registration With Administrator Role


📈 19.36 Punkte
🕵️ Sicherheitslücken

🔧 Rails API + Cache + Design Patterns + RSpec


📈 16.72 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Design Patterns: A Blueprint for Efficient Coding


📈 16.72 Punkte
🔧 Programmierung

matomo