Builder Design Pattern in C#

Builder Design Pattern in C# with Examples

In this article, I am going to discuss the Builder Design Pattern in C# with examples. Please read our previous article where we discussed the Abstract Factory Design Pattern in C# with examples. The Builder Design Pattern falls under the category of the Creational Design Pattern. As part of this article, we are going to discuss the following pointers.

  1. What is the Builder Design Pattern?
  2. Understanding the Builder Design Pattern
  3. How to Implement the Builder Design Pattern in C#?
  4. Implementing the Bridge Design Pattern.
  5. When to use the Builder Design Pattern.
What is the Builder Design Pattern?

The Builder Design Pattern in C# helps us to separate the construction process of a very complex object from its representation so that the same construction process can create different representations.

The Builder Design Pattern is all about separating the construction process from its representation. When the construction process of your object is very complex then only you need to use to Builder Design Pattern. If this is not clear at the moment then don’t worry we will try to understand this with an example.

Understanding the Builder Design Pattern:

Let us understand the builder design pattern with an example. Suppose we want to develop an application for displaying the reports. The reports we need to display either in Excel or in the PDF format. That means, we have two types of representation of my reports. In order to understand this in better, please have a look at the following diagram.

Understanding the Builder Design Pattern in C#

As you can see, in the above image, we are generating the report either in Excel and PDF. Here, the construction process involves several steps such as Create a new report, setting report type, header, content, and footer. If you look at the final output we have one PDF representation and one Excel representation. Please have a look at the following diagram to understand the construction process and its representation.

Bilder Design Pattern Represenatation in C#

How to Implement the Builder Design Pattern in C#?

In order to separate the construction process from its representation, the builder pattern divides into four sections. They are as follows.

  1. Builder: The Builder is responsible for defining the abstract interface for creating the parts of a product.
  2. Concrete Builder: The Concrete Builder is responsible for constructs and assembles the individual parts of the product by implementing the Builder interface. It also defines and tracks the representation it creates.
  3. Director: The Director takes those individual processes from the Builder and defines the sequence to build the product.
  4. Product: Product is the final object which is produced from the coordination of Builder and Director.

Let us discuss the step by step procedure to implement the Builder Design Pattern in C#.

Step1: Create the Product

Create a class file with the name Report.cs and then copy and paste the following code in it.

using System;
namespace BuilderDesignPattern
{
    public class Report
    {
        public string ReportType { get; set; }
        public string ReportHeader { get; set; }
        public string ReportFooter { get; set; }
        public string ReportContent { get; set; }

        public void DisplayReport()
        {
            Console.WriteLine("Report Type :" + ReportType);
            Console.WriteLine("Header :" + ReportHeader);
            Console.WriteLine("Content :" + ReportContent);
            Console.WriteLine("Footer :" + ReportFooter);
        }
    }
}

Once we know the definition of the Product we are building, now we need to create the Builder.

Step2: Creating the Abstract Builder class.

Create a class file with the name ReportBuilder.cs and then copy and paste the following in it.

namespace BuilderDesignPattern
{
    public abstract class ReportBuilder
    {
        protected Report reportObject;

        public abstract void SetReportType();
        public abstract void SetReportHeader();
        public abstract void SetReportContent();
        public abstract void SetReportFooter();

        public void CreateNewReport()
        {
            reportObject = new Report();
        }

        public Report GetReport()
        {
            return reportObject;
        }
    }
}

Notice, here we have four abstract methods. So, each subclass of ReportBuilder will need to implement those methods in order to properly build a report. Now, we need to create a few concrete builder classes by implementing the above ReportBuilder interface.

Step3: Creating Concrete Builder classes.

In our example, we are dealing with two types of reports i.e. Excel and PDF. So, we need to create two concrete builder classes by implementing the ReportBuilder abstract class and providing implementation to the ReportBuilder abstract methods.

Create a class file with the name ExcelReport.cs and then copy and paste the following code in it.

namespace BuilderDesignPattern
{
    class ExcelReport : ReportBuilder
    {
        public override void SetReportContent()
        {
            reportObject.ReportContent = "Excel Content Section";
        }

        public override void SetReportFooter()
        {
            reportObject.ReportFooter = "Excel Footer";
        }

        public override void SetReportHeader()
        {
            reportObject.ReportHeader = "Excel Header";
        }

        public override void SetReportType()
        {
            reportObject.ReportType = "Excel";
        }
    }
}

Again create a class file with the name PDFReport.cs and then copy and paste the following code in it.

namespace BuilderDesignPattern
{
    public class PDFReport : ReportBuilder
    {
        public override void SetReportContent()
        {
            reportObject.ReportContent = "PDF Content Section";
        }

        public override void SetReportFooter()
        {
            reportObject.ReportFooter = "PDF Footer";
        }

        public override void SetReportHeader()
        {
            reportObject.ReportHeader = "PDF Header";
        }

        public override void SetReportType()
        {
            reportObject.ReportType = "PDF";
        }
    }
}

Once you have the required concrete builder classes, now we need to create the director. The director will execute the required steps to create a particular report.

Step4: Creating the Director

Please create a class file with the name ReportDirector.cs and then copy and paste the following code in it.

namespace BuilderDesignPattern
{
    public class ReportDirector
    {
        public Report MakeReport(ReportBuilder reportBuilder)
        {
            reportBuilder.CreateNewReport();
            reportBuilder.SetReportType();
            reportBuilder.SetReportHeader();
            reportBuilder.SetReportContent();
            reportBuilder.SetReportFooter();

            return reportBuilder.GetReport();
        }
    }
}

Once we have the Director and Concrete Builder, now we can use them in the Main method to create different types of Reports.

Step5: Modifying the Main Method.

Please modify the method as shown below.

namespace BuilderDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            // Client Code
            Report report;
            ReportDirector reportDirector = new ReportDirector();

            // Construct and display Reports
            PDFReport pdfReport = new PDFReport();
            report = reportDirector.MakeReport(pdfReport);
            report.DisplayReport();

            Console.WriteLine("-------------------");

            ExcelReport excelReport = new ExcelReport();
            report = reportDirector.MakeReport(excelReport);
            report.DisplayReport();
            
            Console.ReadKey();
        }
    }
}

Now run the application and you should get the output as shown below.

Bilder Design Pattern in C#

In the next article, I am going to discuss the Fluent Interface Design Pattern in C# with some examples. Here, in this article, I try to explain the Builder Design Pattern in C# step by step with some simple examples. I hope you understood the need and use of the Builder Design Pattern in C#.

Leave a Reply

Your email address will not be published. Required fields are marked *