Click here to Skip to main content
6,597,576 members and growing! (20,592 online)
Email Password   helpLost your password?
Development Lifecycle » Design and Architecture » General     Intermediate

Design Patterns - Builder Pattern

By Subhash Balachandran

Design Patterns - Builder Pattern
Windows, Visual Studio, Dev
Posted:23 Feb 2006
Updated:26 Jul 2006
Views:19,944
Bookmarked:15 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 2.07 Rating: 2.45 out of 5
2 votes, 28.6%
1
3 votes, 42.9%
2

3

4
2 votes, 28.6%
5
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

Sample Image - Builder_Pattern.jpg

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
{
    //Director Class

    public class StegoGenerator
    {
        public void GenerateStego(StegoLibrary objlibrary)
        {
            objlibrary.ValidateCover(); 
            objlibrary.Encode();
        }
        
    }

    //Builder absctract Class

    public abstract class StegoLibrary
    {
        
        public abstract void ValidateCover();
        public abstract void Encode();
        public abstract Stego GetStego();
                
    }

    //ConcreteBuilder absctract 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 propery of Stego object

            
        }

        public override void Encode()
        {
            
        }

        public override Stego GetStego()
        {
            objstego.FilePath = "C:\\Sample.bmp";
            return objstego;
        }

    }

    //ConcreteBuilder absctract 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 propery of Stego object

        }

        public override void Encode()
        {
            
        }

        public override Stego GetStego()
        {
            
            return objstego;
        }
    }


    //ConcreteBuilder absctract 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 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 !

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Subhash Balachandran


Member
"Just began to think in patterns" - Zuboss
Occupation: Web Developer
Location: Singapore Singapore

Other popular Design and Architecture articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
QuestionDatabase interaction Pinmembervishal tahiliani8:26 27 Oct '06  
GeneralRe: Database interaction PinmemberRanjan.D2:54 13 Mar '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Jul 2006
Editor:
Copyright 2006 by Subhash Balachandran
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project