@tusss/ood
    Preparing search index...

    Class Builder<TProduct>Abstract

    An abstract base class implementing the Builder design pattern.

    The Builder pattern is a creational design pattern that lets you construct complex objects step-by-step. The pattern allows you to produce different types and representations of an object using the same construction code.

    Subclasses should extend this class, define their concrete product property, and implement chainable methods using the register method.

    Builder with external input product

    interface User {
    name: string;
    age: number;
    }

    class UserBuilder extends Builder<User> {
    constructor(protected override product: User) {
    super();
    }

    setName(name: string) {
    return this.register((product) => {
    product.name = name;
    });
    }
    }

    const user = new UserBuilder({ name: '', age: 0 })
    .setName('John Doe')
    .build();

    Builder with fresh product

    interface UserUpdatable {
    name?: string;
    age?: string;
    }

    class UserUpdateBuilder extends Builder<UserUpdatable> {
    protected override product: UserUpdatable = {};

    constructor(product?: UserUpdatable) {
    super();

    this.product = product;
    }
    }

    const data = new UserUpdateBuilder().setName("John Doe").build();

    Type Parameters

    • TProduct

      The type of the product being built.

    Index

    Constructors

    Properties

    Accessors

    Methods

    Constructors

    Properties

    _isActive: boolean = false

    Internal flag to track if any builder mutations have been registered/executed.

    product: TProduct

    The product instance being constructed by this builder. Must be initialized by the subclass.

    Accessors

    • get isActive(): boolean

      Indicates whether the builder has performed/registered any modifications on the product.

      Returns boolean

    Methods

    • Registers and immediately executes a modification function on the product. Sets the builder's active state to true and returns the builder instance for method chaining.

      Parameters

      • fn: (product: TProduct) => void

        A callback function that receives the current product and performs modifications on it.

      Returns Builder<TProduct>

      The builder instance (this) to allow fluent method chaining.