🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
This post shows how to use the Builder Pattern in TypeScript with an example.
The definition of the Builder Pattern is a separation of the construction of a complex object from its representation.
Problem Statement
Let's look at below code, there are many parameters in the constructor and it's a complex object.
export class House {
address: string;
floorNumber: number;
city: string;
isHavingParking: boolean;
isHavingGarden: boolean;
constructor(address: string, floorNumber: number, city: string, isHavingParking: boolean, isHavingGarden: boolean){
this.address = address;
this.floorNumber = floorNumber;
this.city = city;
this.isHavingParking = isHavingParking;
this.isHavingGarden = isHavingGarden;
}
}
function clientCode() {
const myHouse = new House("Ramesh Fadatare 4803111", 14, "Pune", true, true);
}
clientCode();
Let's solve the above problem using a builder pattern.
TypeScript Builder Pattern
Let's use Builder Pattern, the parameters in the constructor are reduced and served in a much more readable way, and thus there is no need to pass in null for optional parameters to the constructor.
Let’s create “builder.ts” file which contains the following code:
/**
* The definition of the Builder Pattern is a separation of the
* construction of a complex object from its representation.
*
*/
export class House {
address: string;
floorNumber: number;
city: string;
isHavingParking: boolean;
isHavingGarden: boolean;
constructor(houseBuilder: HouseBuilder) {
this.address = houseBuilder.address;
this.city = houseBuilder.city;
this.floorNumber = houseBuilder.floorNumber;
this.isHavingParking = houseBuilder.isHavingParking;
this.isHavingGarden = houseBuilder.isHavingGarden;
}
}
/**
* HouseBuilder implementation
*/
export class HouseBuilder {
private readonly _address: string;
private _floorNumber: number = 0;
private _isHavingParking: boolean = false;
private _city: string;
private _isHavingGarden: boolean = false;
constructor(address: string) {
this._address = address;
}
setFloor(floor: number) {
this._floorNumber = floor;
return this;
}
setCity(city: string){
this._city = city;
return this;
}
makeParking() {
this._isHavingParking = true;
return this;
}
makeGarden() {
this._isHavingGarden = true;
return this;
}
build() {
return new House(this);
}
get isHavingParking() {
return this._isHavingParking;
}
get isHavingGarden() {
return this._isHavingGarden;
}
get address() {
return this._address;
}
get floorNumber() {
return this._floorNumber;
}
get city(){
return this._city;
}
}
function clientCode() {
const myHouse = new HouseBuilder('Ramesh Fadatare, Street: Katraj')
.setCity("Pune")
.setFloor(5)
.makeGarden()
.makeParking()
.build();
console.log(myHouse);
}
clientCode();
Notice that we always return
this in every function (expect build). Well, this because we want to “chain” everything together, and then build and return the new house instance according to the builder state.
Run:
- Compile the above code using the TypeScript compiler.
- Above code is compiled to plan JavaScript code
- Run Javascript code using node
C:\typescript-design-patterns\builder> tsc .\builder.ts
C:\typescript-design-patterns\builder> node .\builder.js
Output:
House {
address: 'Ramesh Fadatare, Street: Katraj',
city: 'Pune',
floorNumber: 5,
isHavingParking: true,
isHavingGarden: true }
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment