Builder Pattern

Builder pattern falls under the Creational design pattern. The Builder pattern separates the construction phase of a complex object from its representation so that the same construction logic can be used for different objects and the output representation may be different. Using the same construction logic, we will get different object representations.

I have used the scenario for creation of reports as the example. We can create either PDF or Excel reports.

ReportBuilder class has the construction logic for building the individual parts of the Report. Director takes ReportBuilder as argument and then defines the sequence for building the report. The end result is the Report which can either be Excel or PDF.

The code snippet for this pattern is as seen below :

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Diagnostics;  
  4. namespace DesignPatterns  
  5. {  
  6.     // Client invoking class  
  7.     public class Client  
  8.     {  
  9.         public static void Main()  
  10.         {  
  11.             // Create a PDF report  
  12.             ReportBuilder pdfBuilder = new PDFBuilder();  
  13.             Director dir = new Director();  
  14.             Report pdfReport = dir.GenerateReport(pdfBuilder);  
  15.             // Print content  
  16.             Console.WriteLine(pdfReport.Header);  
  17.             Console.WriteLine(pdfReport.Content);  
  18.             Console.WriteLine(pdfReport.Footer);  
  19.             // Create a Excel report  
  20.             ReportBuilder excelBuilder = new ExcelBuilder();  
  21.             Report excelReport = dir.GenerateReport(excelBuilder);  
  22.             // Print content  
  23.             Console.WriteLine(excelReport.Header);  
  24.             Console.WriteLine(excelReport.Content);  
  25.             Console.WriteLine(excelReport.Footer);  
  26.             Console.ReadLine();  
  27.         }  
  28.     }  
  29.    
  30.     // Report or Product  
  31.     public class Report  
  32.     {  
  33.         public string ReportType;  
  34.         public string Header;  
  35.         public string Footer;  
  36.         public string Content;  
  37.     }  
  38.    
  39.     // Report Builder - Builder is responsible for defining  
  40.     // the construction process for individual parts. Builder  
  41.     // has those individual processes to initialize and  
  42.     // configure the report.  
  43.     public abstract class ReportBuilder  
  44.     {  
  45.         public Report report;  
  46.         public void CreateReport()  
  47.         {  
  48.             report = new Report();  
  49.         }  
  50.         public abstract void SetReportType();  
  51.         public abstract void SetHeader();  
  52.         public abstract void SetFooter();  
  53.         public abstract void SetContent();  
  54.         public Report DispatchReport()  
  55.         {  
  56.             return report;  
  57.         }  
  58.     }  
  59.    
  60.     // PDF Report class  
  61.     public class PDFBuilder : ReportBuilder  
  62.     {  
  63.         public override void SetReportType()  
  64.         {  
  65.             report.ReportType = "PDF";  
  66.         }  
  67.         public override void SetHeader()  
  68.         {  
  69.             report.Header = "PDF Header";  
  70.         }  
  71.         public override void SetFooter()  
  72.         {  
  73.             report.Footer = "PDF Footer";  
  74.         }  
  75.         public override void SetContent()  
  76.         {  
  77.             report.Content = "PDF Content";  
  78.         }  
  79.     }  
  80.    
  81.     // Excel Report class  
  82.     public class ExcelBuilder : ReportBuilder  
  83.     {  
  84.         public override void SetReportType()  
  85.         {  
  86.             report.ReportType = "Excel";  
  87.         }  
  88.         public override void SetHeader()  
  89.         {  
  90.             report.Header = "Excel Header";  
  91.         }  
  92.         public override void SetFooter()  
  93.         {  
  94.             report.Footer = "Excel Footer";  
  95.         }  
  96.         public override void SetContent()  
  97.         {  
  98.             report.Content = "Excel Content";  
  99.         }  
  100.     }  
  101.    
  102.     ///  
  103.     /// Director takes those individual processes from the builder  
  104.     /// and defines the sequence to build the report.  
  105.     ///  
  106.     public class Director  
  107.     {  
  108.         public Report GenerateReport(ReportBuilder reportBuilder)  
  109.         {  
  110.             reportBuilder.CreateReport();  
  111.             reportBuilder.SetReportType();  
  112.             reportBuilder.SetHeader();  
  113.             reportBuilder.SetContent();  
  114.             reportBuilder.SetFooter();  
  115.             return reportBuilder.DispatchReport();  
  116.         }  
  117.     }  
  118. }