Decorator Pattern in C#

 

Decorator pattern is one of the Structural patterns. Decorator pattern is used to attaché the new functionality to existing object dynamically. Because of you decorating existing object with new functionality and making new object it is called Decorator pattern.

 

Decorator pattern in C# is very useful when you want to make new class with some extra functionality by add this new functionality to existing object. By using decorator pattern you can easily extend the existing object without using inheritance concept.

 

There are several players in decorator pattern. Those are Component( which is original class), Operation(original class method), IComponent(interface, all classes have to implement) and Decorator(new class which is deriving based on original class Component).

 

Here we discuss Decorator Pattern in C# with Bank example. For example we have existing Bank which IBank interface and provide one payment method as shown below.

 

interface IBank

{

       string PaymentOption();

}

 

class Bank:IBank

{

        #region IBank Members

 

         public string PaymentOption()

        {

                  return" Pay using Chq";

        }

 

        #end region

 }

 

And now we are going to decorate new payment methods based on existing functionality as shown below.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace DecoratorPatternExp

{

        interface IBank

       {

              string PaymentOption();

       }

 

      class Bank:IBank

     {

        #region IBank Members

 

         public string PaymentOption()

        {

          return " Pay using Chq";

        }

 

        #endregion

    }

 

   //Decorator A

   class BankA : IBank

  {

         IBank bnk;

 

          public BankA(IBank bank)

         {

              bnk = bank;

         }

 

        #region IBank Members

 

         public string PaymentOption()

        {

               string str = bnk.PaymentOption();

               return str + " Or using DD ";

        }

 

        #endregion

   }

 

   //Decorator B

   class BankB : IBank

   {

           IBank bnk;

 

         public BankB(IBank bank)

        {

            bnk = bank;

        }

 

        #region IBank Members

 

         public string PaymentOption()

        {

                string str = bnk.PaymentOption();

                return str + " Or using Credit Card ";

        }

 

        #endregion

    }

}

 

Here BankA and BankB provides two more payment methods. Now we are decorating the original Bank class with BankA and BankB classes functionality as show below.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace DecoratorPatternExp

{

    class Program

    {

           static void Display(string str, IBank bnk)

          {

                  Console.WriteLine( str + bnk.PaymentOption());  

          }

 

          static void Main(string[] args)

         {

             IBank bnk = newBank();

 

            Display(" Main bank has: ", bnk);

            Display(" BankA has: ", newBankA(bnk) );

            Display(" BankB has: ", newBankB(bnk));

 

            Console.ReadLine(); 

        }

    }

}

 

If you run the application, the output is as shown below. BankA providing payment options through Chque(which was provided by original Bank class) and through DD(which is provided by BankA class). BankB providing payment options through Chque(which was provided by original Bank class) and through Credit Card(which is provided by BankD class).

 

                                                                                                     DecoratorPatternExp.zip (23.79 kb)