Click here to Skip to main content
15,892,809 members
Articles / Programming Languages / C#

Design Patterns - Builder Pattern

Rate me:
Please Sign up or sign in to vote.
2.53/5 (9 votes)
26 Jul 2006CPOL1 min read 44K   19   2
Design Patterns - Builder Pattern
Sample Image - Builder_Pattern.jpg

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

C#
Director (StegoGenerator)

Constructs the stego object using the builder (StegoLibrary)

C#
Builder (StegoLibrary) 

Interface or an abstract class for generating the stego object

C#
ConcreteBuilder (BMPStego, JPEGStego, AVIStego, MP3Stego) 

Derived class of builder that overrides the method of StegoLibrary to generate the appropriate Library

C#
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

C#
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)

C#
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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Singapore Singapore
"Just began to think in patterns" - Zuboss

Comments and Discussions

 
QuestionDatabase interaction Pin
vishal tahiliani27-Oct-06 7:26
vishal tahiliani27-Oct-06 7:26 
GeneralRe: Database interaction Pin
Ranjan.D13-Mar-08 1:54
professionalRanjan.D13-Mar-08 1:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.