Design Patterns - Builder Pattern






2.53/5 (9 votes)
Design Patterns - Builder Pattern

Introduction
Creational Pattern - Builder Pattern
Encapsulate the construction of a complex object, and the same construction will give a different representation each time. (E.g. the Steganographic algorithm construction is encapsulated such that the same library generates different file formats as output each time, based on the client request to the Director which directs the request of the client to the 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 System;
namespace DESIGN_PATTERNS.BuilderPattern
{
//Director Class
public class StegoGenerator
{
public void GenerateStego(StegoLibrary objlibrary)
{
objlibrary.ValidateCover();
objlibrary.Encode();
}
}
//Builder abstract Class
public abstract class StegoLibrary
{
public abstract void ValidateCover();
public abstract void Encode();
public abstract Stego GetStego();
}
//ConcreteBuilder abstract Class
public class BMPStego :StegoLibrary
{
private Stego objstego = new Stego() ;
public BMPStego(string l_text, string l_password)
{
}
public override void ValidateCover()
{
//Validation of cover file to check if the file is BMP goes here
//if invalid cover file found populate the error property of Stego object
}
public override void Encode()
{
}
public override Stego GetStego()
{
objstego.FilePath = "C:\\Sample.bmp";
return objstego;
}
}
//ConcreteBuilder abstract Class
public class JPEGStego :StegoLibrary
{
private Stego objstego = new Stego() ;
public JPEGStego(string l_text, string l_password)
{
}
public override void ValidateCover()
{
//Validation of cover file to check if the file is JPEG goes here
//if invalid cover file found populate the error property of Stego object
}
public override void Encode()
{
}
public override Stego GetStego()
{
return objstego;
}
}
//ConcreteBuilder abstract Class
public class AVIStego :StegoLibrary
{
private Stego objstego = new Stego() ;
public AVIStego(string l_text, string l_password)
{
}
public override void ValidateCover()
{
//Validation of cover file to check if the file is AVI goes here
}
public override void Encode()
{
}
public override Stego GetStego()
{
return objstego;
}
}
//Product-Its is the stego file
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 adds the info to the stego which can be either the error or the path name of the created steganographic image.
Source Code (Client)
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!