0% found this document useful (0 votes)
3 views

Design Pattern Lab_Exp-3

The document outlines a lab record for a Builder Design Pattern implementation in the context of the finance sector, authored by students from Pandit Deendayal Energy University. It explains the Builder pattern's purpose, components, and benefits, including flexibility, encapsulation, and improved readability. The provided source code demonstrates the application of the Builder pattern in creating transaction objects, showcasing its effectiveness in managing complex object construction.

Uploaded by

kaushiksubs000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Design Pattern Lab_Exp-3

The document outlines a lab record for a Builder Design Pattern implementation in the context of the finance sector, authored by students from Pandit Deendayal Energy University. It explains the Builder pattern's purpose, components, and benefits, including flexibility, encapsulation, and improved readability. The provided source code demonstrates the application of the Builder pattern in creating transaction objects, showcasing its effectiveness in managing complex object construction.

Uploaded by

kaushiksubs000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PANDIT DEENDAYAL ENERGY UNIVERSITY

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF TECHNOLOGY
SESSION 2023-24

DESIGN PATTERN LAB RECORD

NAME & ROLL NO : SUHANI PATEL (22BCP399)


MAHI SHAH (22BCP408)
KRISH MODI (22BCP426)
SEMESTER : 4

DIVISION : D6 (G-11)

DOMAIN : FINANCE SECTOR

COURSE CODE : 20CP210P

PROGRAM : B-TECH 2ND YEAR

LAB INSTRUCTOR : DR. NISHANT DOSHI


PRACTICAL – 3 BUILDER DESIGN
PATTERN

AIM : Write a program to implement the Builder Design Pattern

PREREQUISITE : Basics of Object Oriented Programming

In the context of the Builder design pattern, OOPS stands for "Object-Oriented
Programming Principles" rather than an error. The Builder pattern is indeed
rooted in these principles. It's a creational design pattern that separates the
construction of a complex object from its representation, allowing the same
construction process to create different representations.

OUTCOME: To impart knowledge of Design Pattern techniques

The Builder design pattern aims to separate the construction of a complex


object from its representation, allowing the same construction process to
create different representations. The primary outcome or benefits of using the
Builder pattern include: Separation of Concerns, Flexible Object Creation,
Complex Object Construction, Encapsulation of Construction Logic, Improved
Readability, Avoidance of Telescoping Constructors

PROBLEM STATEMENT :

In the financial sector, the investments and many other things depends on
fluctuating factors. There are different things which keep changing in the
finance sector and all are interconnected. In this, we have used builder pattern
as we have many fluctuating factors. Here, investment return rate, profit rate
and currency values all keep changing and are never stable.

THEORY AND SOLUTION :

Builder Design pattern: The Builder Design Pattern is a creational pattern that
addresses the construction of complex objects by separating the construction
process from the actual representation. The pattern involves several key
components: the Product, which is the complex object being built; the Builder,
an interface or abstract class defining construction steps; the ConcreteBuilder,
a class implementing the Builder interface to construct specific products; the
Director, an optional class that orchestrates the construction process; and the
Client, responsible for creating a director, a builder, and initiating the
construction process.
The Builder design pattern can be applied in the finance sector to simplify the
creation of complex objects and improve code readability and maintainability. In
the finance sector, where complex financial instruments and transactions are
often modeled, the Builder pattern can be beneficial for creating these intricate
objects.

SOURCE CODE :

public class transaction{


private final String accountNumber;
private final String transactionType;
private double amount;
private boolean success;
private double totalAmount;
public String getAccountNumber(){
return accountNumber;
}
public String getTransactionType(){
return transactionType;
}
public double getAmount(){
return amount;
}
public double getTotalAmount(){
return totalAmount;
}
public boolean isSucess(){
return success;
}
private transaction(transactionBuilder builder){
this.accountNumber=builder.accountNumber;
this.transactionType=builder.transactionType;
this.amount=builder.amount;
this.success=builder.success;
this.totalAmount= builder.totalAmount;
}
public static class transactionBuilder{
private final String accountNumber;
private final String transactionType;
private double amount;
private boolean success;
private double totalAmount;
public transactionBuilder(String accountNumber, String transactionType){
this.accountNumber=accountNumber;
this.transactionType=transactionType;
}

public transactionBuilder amount(double amount) {


this.amount = amount;
return this;
}
public transactionBuilder success(boolean success) {
this.success = success;
return this;
}
public transactionBuilder totalAmount(double totalAmount) {
this.totalAmount = totalAmount;
return this;
}
public transaction build(){
return new transaction(this);
}
}

public static void main(String[] args){


transaction t=new transaction.transactionBuilder("56124",
"Withdraw").amount(1000).success(true).totalAmount(120000).build();
System.out.println("Amount to be "+t.getTransactionType()+" is:
"+t.getAmount());
System.out.println("Was the "+t.getTransactionType()+" successful? :
"+t.isSucess());
System.out.println("Remaining amount in the account:
"+t.getTotalAmount());
}
}
OUTPUT:
Amount to be Withdraw is: 1000.0
Was the Withdraw successful? : true
Remaining amount in the account: 120000.0

OBSERVATION & LEARNING :

Flexibility: The Builder pattern offers a flexible way to construct objects with
varying configurations. It allows clients to construct objects step by step,
providing control over each aspect of the object's construction.

Encapsulation: The construction process is encapsulated within the builder


classes, abstracting away the details of object creation. This encapsulation
enhances code maintainability by hiding complex construction logic from client
code.

Readability: By using method chaining or fluent interfaces, the Builder pattern


improves code readability. It provides a clear and expressive way to specify
object construction parameters, leading to more understandable code.

Avoidance of Telescoping Constructors: The Builder pattern eliminates the


need for telescoping constructors, which can become unwieldy when dealing
with objects with many optional parameters. Instead, it offers a clean and
concise way to construct objects with only the desired parameters set.
CONCLUSION :

The Builder pattern is a valuable tool for building complex objects in a flexible
and maintainable manner. It promotes separation of concerns by separating
the construction process from the representation of the object. By
encapsulating the construction logic within builder classes and providing a
clear interface for object construction, it enhances code readability and
maintainability. Additionally, it offers flexibility in object creation, allowing
clients to construct objects with different configurations using the same
construction process. Overall, the Builder pattern is a powerful design pattern
that facilitates the creation of complex objects while adhering to object-
oriented principles.

You might also like