// Builder pattern example for creating a complex object step by step // This example may seem like a toy (it is) // but a real-life example (which I use in FRC) of a Builder pattern API // can be found here (note that it's in Java): // https://api.ctr-electronics.com/phoenix6/release/java/com/ctre/phoenix6/configs/Slot0Configs.html // (for the above example, take note of the `with*` methods such as `.withKV`) class Product { constructor() { this.parts = []; } addPart(part) { this.parts.push(part); } showParts() { console.log('Product parts:', this.parts.join(', ')); } } class ProductBuilder { constructor() { this.product = new Product(); } buildPartA() { this.product.addPart('Part A'); return this; } buildPartB() { this.product.addPart('Part B'); return this; } getProduct() { return this.product; } } // Usage const builder = new ProductBuilder(); const product = builder.buildPartA().buildPartB().getProduct(); product.showParts(); // Product parts: Part A, Part B