Decoupling through Delegates

Decoupling

Delegates also promotes decoupling (It is an Object oriented concept ,I will write about that later). You can learn about Delegates in my article:

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.     }  
  49. }  
This is class which is using the above class methods.
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         This is a class which is using these methods.  
  6.         Console.WriteLine("Enter the Option");  
  7.         Console.WriteLine("1.Addition");  
  8.         Console.WriteLine("2.Subtraction");  
  9.         Console.WriteLine("3.Division");  
  10.         Console.WriteLine("4.Multiplication");  
  11.         int option = int.Parse(Console.ReadLine());  
  12.         Calculator Cal_obj = new Calculator();  
  13.         int result = Cal_obj.GetOperation(option)  
  14.             .Invoke(10, 20);  
  15.         Console.WriteLine(result);  
  16.     }  
  17. }  

 

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.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now