The intent of this pattern is to separate construction of an object from its representation, so the same construction process can create different representation.
Its a creational design pattern.
Separates presentation of an object from its construction process.
There are three sections of this pattern:
Requirement: Whenever we have to create same kind of products with something different representation, we need this pattern.
Use: We can create different products using same construction process.
Problem Solved: For creating products having different representation, no need to create whole separate process for each.
Sample Code:
Client Code:
In .Net core below given is the example of builder pattern that we have in Program.cs
Its a creational design pattern.
Separates presentation of an object from its construction process.
There are three sections of this pattern:
- Builder: Builder is responsible for defining construction process of each individual part of product. Builder has these small construction processes in it.
- Director: Director calls concrete builder of product as per client requirement.
- Product: Product is final product that have multiple forms depends on which builder created the product.
Requirement: Whenever we have to create same kind of products with something different representation, we need this pattern.
Use: We can create different products using same construction process.
Problem Solved: For creating products having different representation, no need to create whole separate process for each.
Sample Code:
public class ReportDirector
{
public
ReportDirector()
{
}
public
Report CreateReport(ReportBuilder
reportBuilder)
{
reportBuilder.SetReportType();
reportBuilder.SetReportTitle();
reportBuilder.SetReportHeader();
reportBuilder.SetReportFooter();
return
reportBuilder.GetReport();
}
}
public abstract class ReportBuilder
{
public
ReportBuilder()
{
}
public
abstract void
SetReportType();
public
abstract void
SetReportHeader();
public
abstract void
SetReportFooter();
public
abstract void
SetReportTitle();
public
abstract Report
GetReport();
}
public class ExcelBuilder
: ReportBuilder
{
Report
objExcelReport = new Report();
public
ExcelBuilder()
{
}
public
override void
SetReportType()
{
objExcelReport.ReportType = "Excel";
}
public
override void
SetReportHeader()
{
objExcelReport.ReportHeader = "Market Survey Report";
}
public
override void
SetReportFooter()
{
objExcelReport.ReportFooter = "End of Survey Report";
}
public
override void
SetReportTitle()
{
objExcelReport.ReportTitle = "Mediclaim Market Survey";
}
public
override Report
GetReport()
{
objExcelReport.Data = "Assign data";
return
objExcelReport;
}
}
public class PDFBuilder :
ReportBuilder
{
Report
objPdfReport = new Report();
public
PDFBuilder()
{
}
public
override void
SetReportType()
{
objPdfReport.ReportType = "PDF";
}
public
override void
SetReportHeader()
{
objPdfReport.ReportHeader = "Market Survey Report";
}
public
override void
SetReportFooter()
{
objPdfReport.ReportFooter = "End of Survey Report";
}
public
override void
SetReportTitle()
{
objPdfReport.ReportTitle = "Mediclaim Market Survey";
}
public
override Report
GetReport()
{
objPdfReport.Data = "Assign data";
return
objPdfReport;
}
}
public class Report
{
public
Report()
{
}
public
string ReportType
{
get;
set;
}
public
string ReportHeader
{
get;
set;
}
public
string ReportFooter
{
get;
set;
}
public
string ReportTitle
{
get;
set;
}
public
string Data
{
get;
set;
}
}
Client Code:
static
void Main(string[]
args)
{
ReportDirector
objReportDirector = new ReportDirector();
ReportBuilder
objReportBuilder;
//Generate
Excel Report
objReportBuilder = new ExcelBuilder();
Report
excelReport;
excelReport =
objReportDirector.CreateReport(objReportBuilder);
Console.WriteLine("Report : " + excelReport.ReportType);
//Generate
PDF Report
objReportBuilder = new PDFBuilder();
Report
pdfReport;
pdfReport =
objReportDirector.CreateReport(objReportBuilder);
Console.WriteLine("Report : " + pdfReport.ReportType);
Console.ReadLine();
}
}
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddRazorPages();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddResponseCompression();
builder.Services.AddResponseCaching();
builder.Services.AddOutputCache();
var app =
builder.Build();

No comments:
Post a Comment