![]() |
Development Lifecycle »
Design and Architecture »
General
Intermediate
Design Patterns - Builder PatternBy Subhash BalachandranDesign Patterns - Builder Pattern |
Windows, Visual Studio, Dev
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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

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.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.
using <CODE>System;namespace DESIGN_PATTERNS.BuilderPattern{ //Director Classpublic class StegoGenerator{publicvoid GenerateStego(StegoLibrary objlibrary) { objlibrary.ValidateCover(); objlibrary.Encode(); } } //Builder absctract Classpublic abstract class StegoLibrary{publicabstract void ValidateCover();publicabstract void Encode();publicabstract Stego GetStego(); } //ConcreteBuilder absctract Classpublic class BMPStego :StegoLibrary{privateStego objstego = new Stego() ;publicBMPStego(string l_text, string l_password) { }publicoverride void ValidateCover() { //Validation of cover file to check if the file is BMP goes here //if invalid cover file found populate the error propery of Stego object }publicoverride void Encode() { }publicoverride Stego GetStego() { objstego.FilePath = "C:\\Sample.bmp"; return objstego; } } //ConcreteBuilder absctract Classpublic class JPEGStego :StegoLibrary{privateStego objstego = new Stego() ;publicJPEGStego(string l_text, string l_password) { }publicoverride void ValidateCover() { //Validation of cover file to check if the file is JPEG goes here //if invalid cover file found populate the error propery of Stego object }publicoverride void Encode() { }publicoverride Stego GetStego() { return objstego; } } //ConcreteBuilder absctract Classpublic class AVIStego :StegoLibrary{privateStego objstego = new Stego() ;publicAVIStego(string l_text, string l_password) { }publicoverride void ValidateCover() { //Validation of cover file to check if the file is AVI goes here }publicoverride void Encode() { }publicoverride Stego GetStego() { return objstego; } } //Product-Its is the stego filepublic class Stego{privatestring _stegofilepath;privatestring _error;publicstring FilePath { get { return _stegofilepath; } set { _stegofilepath = value; } }publicstring Error { get { return _error; } set { _error = value; } } } }
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.
<code> using System; using DESIGN_PATTERNS.BuilderPattern;Thinking in Patterns can evolve solid solution !namespace DESIGN_PATTERNS{classStart {[STAThread]staticvoidMain(string[] args) { BuilderFactoryDemo(); Console.ReadLine(); }privatestatic 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); } } }
| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 26 Jul 2006 Editor: |
Copyright 2006 by Subhash Balachandran Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |