0% found this document useful (0 votes)
3 views9 pages

Design Pattern

The document discusses three creational design patterns: Singleton, Factory, and Builder. The Singleton pattern ensures a single instance per service, ideal for managing shared resources, while the Factory pattern provides an interface for object creation, promoting loose coupling and flexibility. The Builder pattern constructs complex objects step by step, improving readability and supporting optional fields, but may introduce extra code complexity.

Uploaded by

Abhishek Yadav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Design Pattern

The document discusses three creational design patterns: Singleton, Factory, and Builder. The Singleton pattern ensures a single instance per service, ideal for managing shared resources, while the Factory pattern provides an interface for object creation, promoting loose coupling and flexibility. The Builder pattern constructs complex objects step by step, improving readability and supporting optional fields, but may introduce extra code complexity.

Uploaded by

Abhishek Yadav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Singleton Design Pattern

What is Singleton Design Pattern?


The Singleton Design Pattern is a creational design pattern that ensures.

Only one instance of a class is created in the entire application,

and that instance is globally accessible .


Singleton in microservices = one instance per service (per JVM)

It is NOT global across all microservices

Why use Singleton?


I use Singleton when you want exactly one shared object to coordinate actions or
manage resources, such as:

• Logging

• Configuration management

• Cache or connection pool

• Thread pool or scheduler

Advantages

1. Controlled access to a single instance

2. Saves memory and resources

3. Centralized state management

Disadvantages

1. Can cause tight coupling


2. Harder to unit test

3. Not suitable for distributed systems as a “global” instance

Real-World Singleton Example in a Microservice


Problem (Real Life)

Your microservice calls an external Auth / Payment API.

1. Token is expensive to generate

2. Token is valid for some time

3. You don’t want to call auth API for every request

Solution: Singleton Token Cache

Code Singleton Bean (Cache)


Factory Design Pattern
What is Factory Design Pattern?
Factory Design Pattern is a Creational Design Pattern that
provides an interface or method to create objects, but lets
subclasses or logic decide which object to create.

Why Use Factory Design Pattern?


We use the Factory Design Pattern to create objects without
exposing the creation logic to the client and to make the system
flexible, loosely coupled, and easy to extend.

Example
1. Avoid Tight Coupling
2. Improves Code Maintainability
3. Supports Runtime Decision Making

Advantages
1. Loose Coupling
o Client code depends on interface, not
implementation.
2. Easy to Add New Types
3. Single Responsibility
4. Better Maintainability
5. Supports Dependency Injection
Disadvantages
1. Extra Classes
o More files = more complexity.
2. Factory Can Become Complex
o If too many if-else or switch.
3. Overkill for Small Apps
o Not needed for very simple logic.

Real-World Example
Example: Notification System
You send notifications via:
• Email
• SMS
Builder Design Pattern
What is Builder Design Pattern?
The Builder Design Pattern is a creational design pattern that
is used to construct complex objects step by step.
It separates object construction from object representation,
allowing the same construction process to create different
representations.
Problem Without Builder Pattern
Why do we use Builder Pattern?
Object has many fields.
Many fields are optional.
Constructor becomes confusing & unreadable.
We want immutable objects.
We want clean & fluent code.

With Builder Pattern

Advantages of Builder Pattern


Avoids telescoping constructors
Improves code readability
Supports optional fields
Helps create immutable objects
Easy to extend
Clean in DTOs & API requests

Disadvantages of Builder Pattern


More code (extra Builder class)
Overkill for small/simple objects
Slight learning curve for beginners

Real-World Usage
Lombok @Builder
Spring Boot DTOs
Kafka Message Builders
REST API Request Objects
code
@Builder
public class UserDTO {
private String name;
private String email;
private int age;}
Thank You

You might also like