This is me…

NestJs - Builder pattern designed to provide a flexible solution to various complex object

20 min read0 comments


What?

The builder pattern is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming. It allows the creation of different representations of an object by isolating the construction code from the actual representation.

The builder pattern is typically used when:
  1. The creation of an object is complex and requires multiple steps.
  2. You want to keep the creation logic separate from the representation of the object.
  3. You need fine-grained control over the construction process.


Let's create a builder pattern in TypeScript for constructing a `Computer`object:


    import { Injectable } from "@nestjs/common";

    export class Computer {
    
        private _cpu: string;
        private _ram: string;
        private _ssd: string;
    
        constructor(builder: ComputerBuilder){
            this._cpu = builder.Cpu
            this._ram = builder.Ram
            this._ssd = builder.Ssd
        }
    
        getRam(): string {
            return this._ram;
        }
    
        getSsd(): string {
            return this._ssd
        }
    
        getCpu(): string {
            return this._cpu;
        }
    }

    @Injectable()
    export class ComputerBuilder {
        private _cpu: string;
        private _ram: string;
        private _ssd: string;
    
        set Cpu(value: string){
            this._cpu = value;
        }
    
        get Cpu(): string {
            return this._cpu;
        }
    
        set Ram(value: string){
            this._ram = value;
        }
    
        get Ram(): string {
            return this._ram;
        }
    
        set Ssd(value: string){
            this._ssd = value;
        }
    
        get Ssd(): string {
            return this._ssd;
        }
    
        setCpu(value): ComputerBuilder {
            this._cpu = value;
            return this;
        }
    
        setRam(value): ComputerBuilder {
            this._ram = value;
            return this;
        }
    
        setSsd(value): ComputerBuilder {
            this._ssd = value;
            return this;
        }
    
        build(): Computer {
            return new Computer(this)
        }
    
    }

Explanation

  1. `Computer` class:
    • It's the product we want to create.
    • It accepts a `ComputerBuilder` object and initializes its properties based on the builder's properties.
  2. `ComputerBuilder` class:
    • It encapsulates the construction logic.
    • Mandatory parameters (like CPU and HDD in this example) are set via the constructor.
    • Optional parameters (like RAM) are set using chained methods.
    • The `build()` method is responsible for constructing the `Computer` object.
  3. Using the builder pattern:
    • The builder pattern promotes the fluent API. So, after setting mandatory properties using the constructor, we can set optional properties using chained methods.
    • Once all desired properties are set, we can create the `Computer` object using the `build()` method.

This is a simple representation of the builder pattern. Depending on your needs, you can extend it with more properties, methods, or even additional validation logic.



Next

Adding OpenAPI to NestJS App | Time Saving