Skip to main content

Command Palette

Search for a command to run...

Builder Design Pattern in TS

Published
•1 min read•View as Markdown
A

This blog is my knowledge dump. For more structured blogs visit: https://medium.com/@ArcadeBuilds

  • Creational pattern used for creating objects in TS

  • We define additional functions which we can method chain to the object

  • Eg. of use functions are like get, set methods

Full code

class Person {
  name: string;
  age: number;

  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }

  getName = (): string => {
    return this.name;
  };

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

  getAge = (): number => {
    return this.age;
  };

  setAge = (age: number) => {
    this.age = age;
  };
}

const person1 = new Person("Ben", 14);
console.log(person1);
person1.setAge(24);
console.log(person1);

More from this blog

A

Aashish's Blog

15 posts

This is my knowledge dump. For more structured blogs visit: https://medium.com/@ArcadeBuilds