Example without using the Builder Design Pattern

Let us take an example without using the Builder Design Pattern:

Javascript




class User {                                    
  constructor(name , age , weight , address , gender) {                           
    this.name = name;
    this.age = age;
    this.weight = weight;
    this.address = address;
    this.gender = gender;
  }
  printUser() {
    return `User: { name: ${this.name}, age: ${this.age}, weight: ${this.weight}, address: ${this.address}, gender: ${this.gender} }`;
  }
}
  
const user  = new User('Abhishek' , 22 , 55 , 'India' , 'Male' );
console.log(user.printUser());


Explanation of the above example:

User Class Definition:

Javascript




class User {
  constructor(name, age, weight, address, gender) {
    this.name = name;
    this.age = age;
    this.weight = weight;
    this.address = address;
    this.gender = gender;
  }
  printUser() {
    return `User: { name: ${this.name}, age: ${this.age}, weight: ${this.weight}, address: ${this.address}, gender: ${this.gender} }`;
  }
}


  • The User class is defined with a constructor that takes parameters for the user’s name, age, weight, address, and gender. The constructor initializes the respective attributes.
  • The printUser method generates a formatted string representing the user’s information.

Creating a User Instance:

const user = new User('Abhishek', 22, 55, 'India', 'Male');

This line creates an instance of the User class by calling the constructor with the specified values for name, age, weight, address, and gender.

Displaying User Information:

console.log(user.printUser());

This line calls the printUser method on the user instance, which generates a string containing the user’s information and logs it to the console.

Output:

User: { name: Abhishek, age: 22, weight: 55, address: India, gender: Male }

Before we move to implementing Builder Method we have to analyze what is wrong with this and what issues are solved by implementing builder method, at first we see the above code we realized that this is a correct and easy way to create a new object but we have to provide all information during initialization. If we look closely at the line:

const user = new User(‘Abhishek’ , 22 , 55 , ‘India’ , ‘Male’ );

we see that properties of this user object can be confusing, like sometime we can mistakenly give age instead of weight and weight instead of age . As our code size grows, we will have to look at the class to figure out which properties we have to provide when initializing a user object.

For all these problems we have a design solution called Builder Method.

Builder Method | JavaScript Design Pattern

The Builder design pattern is a creational design pattern used to construct complex objects by separating the construction process from the actual representation. It’s especially useful when an object requires multiple steps or configurations to be created.

Important Topics for the Builder Design Pattern in JavaScript Design Pattern

  • Example without using the Builder Design Pattern
  • Implementation of the above Example using Builder Design Pattern
  • Advantages of the Builder Design Pattern:
  • Disadvantages of the Builder Design Pattern:
  • Where to Use the Builder Design Pattern:
  • Where Not to Use the Builder Design Pattern:

Similar Reads

Example without using the Builder Design Pattern

Let us take an example without using the Builder Design Pattern:...

Implementation of the above Example using Builder Design Pattern

...

Advantages of the Builder Design Pattern:

...

Disadvantages of the Builder Design Pattern:

Implementation user Class:...

Where to Use the Builder Design Pattern:

...

Where Not to Use the Builder Design Pattern:

...

Contact Us