C# - Builder Design Pattern

 

Builder Design Pattern is one of the Creational Pattern in Gang Of Four (GOF) Design Patterns. As any design pattern is used for reducing the complexity of the code, increasing the reusability of code...etc Builder design pattern is used to hide the complexity while creating the object at client side. For example if we have ten methods in a class, to call these methods at client side we have to use this  class object ten times which makes client side code cluttered. By using Builder design Pattern we can avoid using object several times at client side by placing all these methods in a particular class. Today we discuss about Builder Design Pattern in C# with simple example.

 

For example we have Payment gateway application with two different type credit cards Maestro and VISA. Both these cards has same type of methods like GetCreditInfo() and Pay() methods. Lets implement the Builder Pattern for this payment gateway.

 

The main components of Builder Pattern are Builder interface, Concrete Builder classes and Director class.

 

Builder Interface - Interface which has common methods GetCreditInfo() and Pay()  for Concrete Builder classes(ICreditCard)

 

Concrete Builder Classes - Actual classes which implements Builder Interface(Maestro.cs, VISA.cs)

 

Director - Class through which client calls the main classes methods(CreditCard.cs)

 

Lets implement the Builder Pattern with above components.

 

Open Microsoft Visual Studio => Create Console Application and name it as CSharpBuilderDesignPattern.

 

Create interface ICreditCard.cs as shown below.

 

namespace CSharpBuilderDesignPattern

{

    interface ICreditCard

    {

        void GetCreditInfo();

        void Pay();

    }

}

 

Implement above interface through Concrete Builder classes Maestro and VISA as shown below.

 

using System;

 

namespace CSharpBuilderDesignPattern

{

    class Maestro : ICreditCard

    {

 

        #region ICreditCard Members

 

        public void GetCreditInfo()

        {

            Console.WriteLine("Maestro GetCreditInfo()");

        }

 

        public void Pay()

        {

            Console.WriteLine("Maestro Pay() method");

        }

 

        #endregion

    }

}

 

 

using System;

 

namespace CSharpBuilderDesignPattern

{

    class VISA : ICreditCard

    {

        #region ICreditCard Members

 

        public void GetCreditInfo()

        {

            Console.WriteLine("VISA GetCreditInfo()");

        }

 

        public void Pay()

        {

            Console.WriteLine("VISA Pay() method");

        }

 

        #endregion

    }

}

 

Now create Director class CreditCard.cs through which client calls all these methods as shown below.

 

namespace CSharpBuilderDesignPattern

{

    class CreditCard

    {

        public void Execute(ICreditCard creditCard)

        {

            creditCard.GetCreditInfo();

            creditCard.Pay();

        }

    }

}

 

Let's call above classes at client side as shown below.

 

using System;

 

namespace CSharpBuilderDesignPattern

{

    class Program

    {

        static void Main(string[] args)

        {

            CreditCard objCC = new CreditCard();

            ICreditCard iCreditCard = null;

 

            iCreditCard = new Maestro();

            objCC.Execute(iCreditCard);

 

            iCreditCard = new VISA();

            objCC.Execute(iCreditCard);

 

            Console.ReadLine();

        }

    }

}

 

As shown above we restricted the Maestro and VISA classes objects usage through Builder Pattern.  Run the application and the output is as shown below.

 

 

                                                                                                CSharpBuilderDesignPattern.zip (26.26 kb)