Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
Title: Builder Pattern
Email: zuboss_82@yahoo.com
Environment: C#
Keywords: Design Pattern, Builder Pattern
Level: Intermediate
Description: Article on Builder Pattern
Section General Reading
SubSection Design & Stratergy

Introduction
Creational Pattern - Builder Pattern
Encapsulate the construction of complex object, and the same construction will give different representation each time. (Eg the Steganographic algorithm construction is encapsulated such the same library generates different file format as output each time, based on the client request to the Director which directs the request of the client to appropriate builder)
Reference: Design Patterns, Elements of Reusable object oriented software by GOF
I have implemented the Steganographic library using Builder pattern for a clear understanding. The library creates the Steganographic file from the cover file. The Cover file can be BMP, JPEG, and AVI so on. The library creates the stego file by encoding the text or the file into the image. The construction logic is not discussed but rather the structure for implementation.
Components
Director (StegoGenerator)
Constructs the stego object using the builder (StegoLibrary)
Builder (StegoLibrary)
Interface or an abstract class for generating the stego object
ConcreteBuilder (BMPStego, JPEGStego, AVIStego, MP3Stego)
Derived class of builder that overrides the method of StegoLibrary to generate the appropriate Library
Product (Stego)
Represents the complex object, constructed by builder, all the implementation varies based on the request by the client raised to the director.
Source
using <CODE>System;
namespace DESIGN_PATTERNS.BuilderPattern
{
public class StegoGenerator
{
public void GenerateStego(StegoLibrary objlibrary)
{
objlibrary.ValidateCover();
objlibrary.Encode();
}
}
public abstract class StegoLibrary
{
public abstract void ValidateCover();
public abstract void Encode();
public abstract Stego GetStego();
}
public class BMPStego :StegoLibrary
{
private Stego objstego = new Stego() ;
public BMPStego(string l_text, string l_password)
{
}
public override void ValidateCover()
{
}
public override void Encode()
{
}
public override Stego GetStego()
{
objstego.FilePath = "C:\\Sample.bmp";
return objstego;
}
}
public class JPEGStego :StegoLibrary
{
private Stego objstego = new Stego() ;
public JPEGStego(string l_text, string l_password)
{
}
public override void ValidateCover()
{
}
public override void Encode()
{
}
public override Stego GetStego()
{
return objstego;
}
}
public class AVIStego :StegoLibrary
{
private Stego objstego = new Stego() ;
public AVIStego(string l_text, string l_password)
{
}
public override void ValidateCover()
{
}
public override void Encode()
{
}
public override Stego GetStego()
{
return objstego;
}
}
public class Stego
{
private string _stegofilepath;
private string _error;
public string FilePath
{
get
{
return _stegofilepath;
}
set
{
_stegofilepath = value;
}
}
public string Error
{
get
{
return _error;
}
set
{
_error = value;
}
}
}
}
What Happens ?
The client creates the StegoGenerator object and configures it with the desired StegoLibrary.StegoGenerator notifies the StegoLibrary whenever a part of stego should be built.StegoLibrary handles request from the StegoGenerator and add the info to the stego which can be either the error or the path name of the created steganographic image.
Source Code(Client)
<code>
using System;
using DESIGN_PATTERNS.BuilderPattern;
namespace DESIGN_PATTERNS
{
class Start
{
[STAThread]
static void Main(string[] args)
{
BuilderFactoryDemo();
Console.ReadLine();
}
private static void BuilderFactoryDemo()
{
Stego stegoobject = null ;
StegoGenerator stegogenerator = new StegoGenerator();
StegoLibrary bmpstego = new BMPStego("Text to be encrypted","sample password");
stegogenerator.GenerateStego(bmpstego);
stegoobject = bmpstego.GetStego();
Console.WriteLine(stegoobject.FilePath);
}
}
}
Thinking in Patterns can evolve solid solution !