Skip to main content

Builder Pattern

Builder Pattern

Gamma Categorization: Creational Design Patten
Summary: When piecewise object construction is complicated, provide an API for doing it succintly.

Problem examples

App needs to construct multiple type classes
These classes have complicated construction methods
Isolate construction process from application

Solution

Builder Interface or Abstract class
Create Builder classes that implement builder interface for each object type
Director class: uses builder class to create object

Sample Code

Problem: we need to construct a Sales Report, that must be built every day with the sales summary.

Code

So in order to build the report we need an abstract ReportBuilder class, with a simple constructor that initializes the report. The class contains methods for constructing the header, content and footer. The DispatchReport will be implemented for each report type. 

public abstract class ReportBuilder
{
    protected Report report;

    public void CreateReport() => report = new Report();
    public void SetHeader(string header) => report.Header = header;
    public void AppendContentLine(string contentLine) => report.Content.Add(contentLine);
    public void SetFooter(string footer) => report.Footer = footer;

    public abstract string  DispatchReport();

    public void Clear() => report = new Report();
}

The Report object containds a header, multiple content lines and a footer.

public class Report
{
    public string Header { getset; }
    public List<stringContent { getset; } = new List<string>();
    public string Footer { getset; }
}

As an example, we will implement a text report, that will print the report as a string.

public class TextReportBuilder : ReportBuilder
{
    public override string DispatchReport()
    {
        var hr = "-------------------------";

        var sb = new StringBuilder();

        sb.AppendLine(hr);
        sb.AppendLine(report.Header);
        sb.AppendLine(hr);

        foreach (var contentLine in report.Content)
        {
            sb.AppendLine(contentLine);
        }

        sb.AppendLine(hr);
        sb.AppendLine(report.Footer);
        sb.AppendLine(hr);

        return sb.ToString();
    }
}

Usage

var textReportBuilder = new TextReportBuilder();

textReportBuilder.CreateReport();
textReportBuilder.SetHeader($"Daily Report - {DateTime.UtcNow.ToString("dd/MM/yyyy")}");
textReportBuilder.AppendContentLine("14 Hot Meals");
textReportBuilder.AppendContentLine("22 Breakfasts");
textReportBuilder.SetFooter("Copyright - Cafe Shop");

System.Console.WriteLine(textReportBuilder.DispatchReport());

Output
-------------------------
Daily Report - 06/12/2019
-------------------------
14 Hot Meals
22 Breakfasts
-------------------------
Copyright - Cafe Shop
-------------------------


Note: The Builder Patter definition states that you must have a Director object that basicaly encapsulates what we did in the Usage section. It will depend on your context if it makes sense to use a Director class or if you need the flexibility to assemble the report yourself dynamically.

Next, you can proceed to extend the pattern in order chain the build operations using the Fluent Builder Pattern

Comments

Popular posts from this blog

XML Webservice (ASMX) - SOAP Request and Response Invocation logging

You are an integration developer. Eventualy you came into the state where there is nothing else you can debug, and you have to check which SOAP request it is built on the request, and which SOAP response you are getting from the server. C# XML Webservice (ASMX) - SOAP Request and Response Invocation logging In the legaccy .NET framework System.Web.Services , this means using soapExtensions to help you intersept the interaction with the webservice. This is done like so:  public class TraceExtension : SoapExtension     {         Stream oldStream;         Stream newStream;         string filename;         // Save the Stream representing the SOAP request or SOAP response into          // a local memory buffer.          public override Stream ChainStream(Stream stream)         {           ...

Abstract Factory Pattern

Abstract Factory Pattern  Gamma Categorization: Creational Design Patten Summary: When the object construction is complicated, needing multiple arguments, we should create a separate function (Factory Method) or class (Factory), which is responsible for the creation of the all object. Problem examples Suport of multiple databases Multiple data sources: Serial port, ethernet port, device driver Diferent report types Solution Abstract class Generalized interface A Factory creates instances of the concrete classes Sample Code The abstract factory public   interface   IPhotoFactory {      IAnaloguePhoto   CreateAnaloguePhoto ();      IDigitalPhoto   CreateDigitalPhoto (); } The abstract products public   interface   IAnaloguePhoto {      string   GetName (); } public   interface   IDigitalPhoto {      ...

SOLID (1/5) - Single Resposibility Principle

 SOLID (1/5) - Single Resposibility Principle The single-responsibility principle (SRP) is a computer-programming principle that states that every class in a computer program should have responsibility over a single part of that program's functionality, which it should encapsulate. All of that module, class or function's services should be narrowly aligned with that responsibility. In the following example we have a TodoList class which only handles it's own functionality logic, and then we have a Persistance class which handles the saving logic, hence keeping the concerns separeted. using   System ; using   System . Collections . Generic ; namespace   Journal {      public   class   TodoList     {          private   readonly   List < string >  _entries  =  new   List < string >();          private...