Builder

Why?

The pattern organizes the object construction into a set of steps (such as buildWalls, buildDoor, etc.) To create an object, you will need to call several building steps in a builder class. The important part here is that you do not need to call all the steps. You can use only the steps necessary for producing a particular configuration of an object.

Code

class Bot{
  constructor(builder){
    this.name=builder.name;
    this.role=builder.role;
    this.relaxTime=builder.relaxTime;
    this.canSing=builder.canSing;
  }
  //rest of Bot methods
}
//dev
const Elsa = 
new BotBuilder('Elsa').makeDeveloper().setRelaxTime(100).makeItSing().build();


//testers
const Maddie =
new BotBuilder('Maddie').makeTester().setRelaxTime(200).makeItSing().build();


//Manager
const Caroline = new BotBuilder('Caroline').makeManager().setRelaxtime(50).build();

//DevOps
const Jim = new BotBuilder('Caroline').makeDevOps().setRelaxtime(110).build();
class BotBuilder{
  constructor(name){
    this.name=name;
  }
  makeManager(){
  this.role="manager";
    returnthis;
  }
  makeDeveloper(){
    this.role="developer";
    return this;
  }
  makeTester(){
    this.role="tester";
    return this;
  }
  makeDevOps(){
    this.role="devops";
    return this;
  }
  setRelaxTime(time=100){
    this.relaxTime=time;
    return this;
  }
  makeItSing(){
    this.canSing=true;
    return this;
  }
  build(){
    return new Bot(this)
  }

Code

class Burger {
  constructor(data) {
   _size = data.size || false;
   _tomato = data.tomato || false;
   _cheese = data.cheese || false;
   _pepperoni = data.pepperoni || false;
 }
};

class BuildBurger{
  constructor(size){
    this.size = size;
  }
  
  addCheese(){
    this.cheese = true;
    return this;
  }
  
  addTomato(){
    this.tomato = true;
    return this;
  }
  
  addPepperoni(){
    this.pepperoni = true;
    return this;
  }
  
  build(){
    return new Burger(this);
  }
}

let obj = (new BuildBurger(14)).addCheese().addTomato().build();

Code


class HomeSettings {
	constructor(settings) {
		this._roof = settings.roof;
		this._walls = settings.walls;
		this._windows = settings.windows;
		this._doors = settings.doors;
	}

	get roof() {
		return this._roof;
	}

	get walls() {
		return this._walls;
	}

	get windows() {
		return this._windows;
	}

	get doors() {
		return this._doors;
	}

	static getBuilder() {
		return new HomeSettingsBuilder();
	}
}
const homeSettings = HomeSettings.getBuilder()
	.withRoof(true)
	.withWalls(true)
	.withWindows(true)
	.withDoors(true)
	.build();
class HomeSettingsBuilder {
     withRoof(roof) {
	this._roof = roof;
	return this;
        }

    withWalls(walls) {
	this._walls = walls;
	return this;
	}

    withWindows(windows) {
	this._windows = windows;
	return this;
	}

    withDoors(doors) {	
	
        this._doors = doors;
	return this;
	}

    build() 
	return new HomeSettings(this);
   }  
 }

Thx

Made with Slides.com