Design Patterns for Beginners

Prasad Bylapudi
2 min readAug 10, 2023

A lot of times you heard this buzzword design pattern, but what is it, and why it’s so important to know even if you are a junior developer? let’s dive into it.

Design patterns are particular patterns that help in improving code quality. let’s see how. we don’t have to go through the all design pattern out there we’ll go through some important design patterns only.

Definition

A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.

Each pattern describes a problem that occurs multiple times in out environment.

Types of Design Patterns:

Creational:

how objects are created

Structural:

how objects relate to each other

behavioral:

how objects communicate with each other.

source from sudo code

1. Factory Method

The factory method is like encapsulation. Hiding the implementation details and just showing the functionality.

Source from Sudo Code.

Real use Cases where to use Factory Method

if you guys observe carefully we are using a lot of libraries and their functions without knowing what’s happening behind the scenes.

UI Frameworks,

Creation of different types of buttons or UI widgets.

2. Builder Pattern

The builder pattern is a creational design pattern that lets you construct complex objects step by step. It JavaScript, we can achieve this with method chaining.

class Product {
constructor() {
this.name = '';
this.price = 0;
this.category = '';
this.color = '';
}

// Additional methods and properties can be added here
}

class ProductBuilder {
constructor() {
this.product = new Product();
}

setName(name) {
this.product.name = name;
return this;
}

setPrice(price) {
this.product.price = price;
return this;
}

setCategory(category) {
this.product.category = category;
return this;
}

setColor(color) {
this.product.color = color;
return this;
}

build() {
return this.product;
}
}

// Usage
const product = new ProductBuilder()
.setName('Smartphone')
.setPrice(500)
.setCategory('Electronics')
.setColor('Black')
.build();

console.log(product);

--

--