Thursday, 17 December 2015

Delegate Part 3

Delegate Part 3

 Decoupling

Delegates also promotes decoupling(It is an Object oriented concept ,I will write about that later).
For Example you have written a class of Calculator ,in which you have written the methods of Addition,Subtraction,Multiplication and Division. Another class is using your these methods.You add some more methods in your class ,in order to use these methods you also need to change your class.
Delegates provides solution to this problem .
Lets  go through in a example


This is  a calculator class which takes inputs and perform Addition,Subtraction,Multiplication and Division operations and return result .

  1. namespace Delegates
  2. {
  3.     delegate int Mathoperation(int num1,int num2);     //Delegate declaration
  4.     class Calculator
  5.     {
  6.         public Mathoperation GetOperation(int option)
  7.         {
  8.             Mathoperation mathOperation = null;             //Currently, delegate object has no reference  
  9.             if (option == 1)
  10.             {
  11.                 mathOperation = Add;
  12.             }
  13.             else if (option == 2)
  14.             {
  15.                 mathOperation =Sub;
  16.             }
  17.             else if (option == 3)
  18.             {
  19.                 mathOperation = Div;
  20.             }
  21.             else if(option==4)
  22.             {
  23.                 mathOperation = Mul;


  24.             }
  25.             else
  26.             {
  27.                 Console.WriteLine("You choose the wrong option");
  28.             }


  29.             return mathOperation;
  30.         }
  31. //Mathematical Operations  
  32.       private int Add(int num1, int num2)
  33.         {
  34.             return num1 + num2;
  35.         }
  36.         private int Sub(int num1, int num2)
  37.         {
  38.             return num1-num2;
  39.         }
  40.         private int Div(int num1, int num2)
  41.         {
  42.             return num1 / num2;
  43.         }
  44.         private int Mul(int num1, int num2)
  45.         {
  46.             return num1 * num2;
  47.         }


  48.     }
}


This is class which is using the above class methods.

  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {


  1. This is a class which is using these methods.
  2. Console.WriteLine("Enter the Option");
  3.             Console.WriteLine("1.Addition");
  4.             Console.WriteLine("2.Subtraction");
  5.             Console.WriteLine("3.Division");
  6.            Console.WriteLine("4.Multiplication");
  7.            int option = int.Parse(Console.ReadLine());
  8.            Calculator Cal_obj=new Calculator();
  9.            int result = Cal_obj.GetOperation(option).Invoke(10, 20);
  10.             Console.WriteLine(result);
  11. }
  12. }
Now whatever changes you made in your Calculator class,It would not affect your Program Class which is using the methods of this class.
This is called decoupling .And Object Oriented Method encourages decoupling .





No comments:

Post a Comment