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. // Report or Product
  30. public class Report
  31. {
  32. public string ReportType;
  33. public string Header;
  34. public string Footer;
  35. public string Content;
  36. }
  37. // Report Builder - Builder is responsible for defining
  38. // the construction process for individual parts. Builder
  39. // has those individual processes to initialize and
  40. // configure the report.
  41. public abstract class ReportBuilder
  42. {
  43. public Report report;
  44. public void CreateReport()
  45. {
  46. report = new Report();
  47. }
  48. public abstract void SetReportType();
  49. public abstract void SetHeader();
  50. public abstract void SetFooter();
  51. public abstract void SetContent();
  52. public Report DispatchReport()
  53. {
  54. return report;
  55. }
  56. }
  57. // PDF Report class
  58. public class PDFBuilder : ReportBuilder
  59. {
  60. public override void SetReportType()
  61. {
  62. report.ReportType = "PDF";
  63. }
  64. public override void SetHeader()
  65. {
  66. report.Header = "PDF Header";
  67. }
  68. public override void SetFooter()
  69. {
  70. report.Footer = "PDF Footer";
  71. }
  72. public override void SetContent()
  73. {
  74. report.Content = "PDF Content";
  75. }
  76. }
  77. // Excel Report class
  78. public class ExcelBuilder : ReportBuilder
  79. {
  80. public override void SetReportType()
  81. {
  82. report.ReportType = "Excel";
  83. }
  84. public override void SetHeader()
  85. {
  86. report.Header = "Excel Header";
  87. }
  88. public override void SetFooter()
  89. {
  90. report.Footer = "Excel Footer";
  91. }
  92. public override void SetContent()
  93. {
  94. report.Content = "Excel Content";
  95. }
  96. }
  97. ///
  98. /// Director takes those individual processes from the builder
  99. /// and defines the sequence to build the report.
  100. ///
  101. public class Director
  102. {
  103. public Report GenerateReport(ReportBuilder reportBuilder)
  104. {
  105. reportBuilder.CreateReport();
  106. reportBuilder.SetReportType();
  107. reportBuilder.SetHeader();
  108. reportBuilder.SetContent();
  109. reportBuilder.SetFooter();
  110. return reportBuilder.DispatchReport();
  111. }
  112. }
  113. }