Click here to Skip to main content
Click here to Skip to main content

Design Patterns - Builder Pattern

, 26 Jul 2006
Rate this:
Please Sign up or sign in to vote.
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

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!

License

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

About the Author

Subhash Balachandran
Web Developer
Singapore Singapore
"Just began to think in patterns" - Zuboss

Comments and Discussions

 
QuestionDatabase interaction Pinmembervishal tahiliani27-Oct-06 7:26 
GeneralRe: Database interaction PinmemberRanjan.D13-Mar-08 1:54 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 26 Jul 2006
Article Copyright 2006 by Subhash Balachandran
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid