Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

ts Builder pattern

// The Builder pattern is a design pattern lets you construct complex 
// objects step by step and it's used mainly in the object oriented 
// programming paradigm.

// And you can implement the Builder pattern in Typescript is 
// very easy and you can use the builder module.

class Builder {
  public build() {
    return new Product();
  }
}

// Then you make a product class :
class Product {
  constructor(private partA: string, private partB: string) {}
}

// Then you make a director class to build the builder:
class Director {
  public build(builder: Builder) {
    builder.buildPartA();
    builder.buildPartB();
  }
}

// Then you make a ConcreteBuilder that implements the Builder interface:
class ConcreteBuilder extends Builder {
  private product: Product;

  public buildPartA() {
    this.product.partA = 'Part A';
  }

  public buildPartB() {
    this.product.partB = 'Part B';
  }

  public getProduct() {
    const product = this.product;
    this.reset();
    return product;
  }

  public reset() {
    this.product = new Product('', '');
  }
}
const builder = new ConcreteBuilder();
const director = new Director();
director.build(builder);
// make a new product
const product = new Product('Part A', 'Part B');
// get the product
const newProduct = builder.getProduct();
Comment

PREVIOUS NEXT
Code Example
Typescript :: ts factory pattern 
Typescript :: custom portal react 
Typescript :: typescript readonly 
Typescript :: makestyles material ui typescript 
Typescript :: angular pass parameter to click function 
Typescript :: how to add command line arguments in vscode 
Typescript :: typescript cast string to number 
Typescript :: robux 
Typescript :: mongodb nest.js 
Typescript :: ERROR in The Angular Compiler requires TypeScript =3.4.0 and <3.6.0 but 4.1.5 was found instead. 
Typescript :: display moment in format dd/mm/yy only last two digits of year 
Typescript :: typescript vite static assets 
Typescript :: how to print brackets characters in c# 
Typescript :: sts is not opening in mac 
Typescript :: sum all elements using each_with_object ruby 
Typescript :: acceso a etiqueta o elemento # en agnular 
Typescript :: beyondcode/laravel-websockets 1.12.0 requires pusher/pusher-php-server ^3.0|^4.0|^5.0 - found pusher/pusher-php-server[dev-master 
Typescript :: read and write objects in cpp 
Typescript :: import fonts from angular.json file 
Typescript :: .htaccess Forcing scripts to display as source code 
Typescript :: create a 4x2 integer array and print its attributes 
Typescript :: js convert to typescript online 
Typescript :: TypeError: __cinit__() takes at least 2 positional arguments (0 given) 
Typescript :: deno allow net 
Typescript :: google clear list of accounts from chrome 
Typescript :: Powershell show inactive account in active directory 
Typescript :: .for each typescript 
Typescript :: how to let a textview take 75 percent of its parent width android xml 
Typescript :: java a program that converts letters to their corrosponding telephone digits 
Typescript :: how to select a column with brackets in jupyter notebook 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =