SIGN UP MEMBER LOGIN:    
ARTICLE

Builder Pattern

Posted by Abhishek Bhat Articles | Design & Architecture January 31, 2011
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.
Reader Level:

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 :

using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace DesignPatterns
{
    // Client invoking class
    public class Client
    {
        public static void Main()
        {
            // Create a PDF report
            ReportBuilder pdfBuilder = new PDFBuilder();
            Director dir = new Director();
            Report pdfReport = dir.GenerateReport(pdfBuilder);
            // Print content
            Console.WriteLine(pdfReport.Header);
            Console.WriteLine(pdfReport.Content);
            Console.WriteLine(pdfReport.Footer);
            // Create a Excel report
            ReportBuilder excelBuilder = new ExcelBuilder();
            Report excelReport = dir.GenerateReport(excelBuilder);
            // Print content
            Console.WriteLine(excelReport.Header);
            Console.WriteLine(excelReport.Content);
            Console.WriteLine(excelReport.Footer);
            Console.ReadLine();
        }
    }
 
    // Report or Product
    public class Report
    {
        public string ReportType;
        public string Header;
        public string Footer;
        public string Content;
    }
 
    // Report Builder - Builder is responsible for defining
    // the construction process for individual parts. Builder
    // has those individual processes to initialize and
    // configure the report.
    public abstract class ReportBuilder
    {
        public Report report;
        public void CreateReport()
        {
            report = new Report();
        }
        public abstract void SetReportType();
        public abstract void SetHeader();
        public abstract void SetFooter();
        public abstract void SetContent();
        public Report DispatchReport()
        {
            return report;
        }
    }
 
    // PDF Report class
    public class PDFBuilder : ReportBuilder
    {
        public override void SetReportType()
        {
            report.ReportType = "PDF";
        }
        public override void SetHeader()
        {
            report.Header = "PDF Header";
        }
        public override void SetFooter()
        {
            report.Footer = "PDF Footer";
        }
        public override void SetContent()
        {
            report.Content = "PDF Content";
        }
    }
 
    // Excel Report class
    public class ExcelBuilder : ReportBuilder
    {
        public override void SetReportType()
        {
            report.ReportType = "Excel";
        }
        public override void SetHeader()
        {
            report.Header = "Excel Header";
        }
        public override void SetFooter()
        {
            report.Footer = "Excel Footer";
        }
        public override void SetContent()
        {
            report.Content = "Excel Content";
        }
    }
 
    ///
    /// Director takes those individual processes from the builder
    /// and defines the sequence to build the report.
    ///
    public class Director
    {
        public Report GenerateReport(ReportBuilder reportBuilder)
        {
            reportBuilder.CreateReport();
            reportBuilder.SetReportType();
            reportBuilder.SetHeader();
            reportBuilder.SetContent();
            reportBuilder.SetFooter();
            return reportBuilder.DispatchReport();
        }
    }
}

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks a lot Mahesh..I am reading your articles since long time and am a great fan of yours..

Posted by Abhishek Bhat Jan 31, 2011

Keep up the good work Abhishek.

Posted by Mahesh Chand Jan 31, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor