Wednesday 9 April 2014

Loosely Coupled Application (delegates in c# )



Short Introduction Loose and Tight Coupling
Loose Coupling means reducing dependencies of a class that use a different class directly. In tight coupling, classes and objects are dependent on one another. In general, tight coupling is usually bad because it reduces flexibility and re-usability of code.



Business layer Code
 namespace BusinessLayer
{
    public class Calc
    {
        //public delegate int CalculationHandler(int x, int y);
        public int Add(int a, int b)
        {
            return a + b;
        }

        public int Sub(int a, int b)
        {
            return a - b;
        }

        public int Mul(int a, int b)
        {
            return a * b;
        }
    }
}

UI Layer Code
   class Program
    {
        static void Main(string[] args)
        {
            Calc obj = new Calc();
            Console.WriteLine( obj.Add(10, 12));
            Console.WriteLine(obj.Sub(10, 12));
            Console.WriteLine(obj.Mul(10, 12));
            Console.Read();
        }
    }
Problem:
     If I've changed Business layer function name(Add() to AddNumber(), I should be change UI Layer also.

Loosely Coupled:

What is Delegate?
A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.
Delegates are used to pass methods as arguments to other methods. Event handlers are nothing more than methods that are invoked through delegates.
You create a custom method, and a class such as a windows control can call your method when a certain event occurs. The following example shows a delegate declaration:
public delegate int CalculationHandler(int x, int y);

Loosely Coupled Code

 namespace BusinessLayer
{
    public class Calc
    {
        public delegate int CalculationHandler(int x, int y);
        public CalculationHandler Calculator(int Choice)
        {
            CalculationHandler objCalc = null;
            if (Choice == 1)
                objCalc += Add;
            else if (Choice == 2)
                objCalc += Sub;
            else if (Choice == 3)
                objCalc += Mul;
            return objCalc;
        }


        private int Add(int a, int b)
        {
            return a + b;
        }

        private int Sub(int a, int b)
        {
            return a - b;
        }

        private int Mul(int a, int b)
        {
            return a * b;
        }
    }
}

UI Layer:
class Program
    {
        static void Main(string[] args)
        {
            Calc obj = new Calc();
            Console.WriteLine( obj.Calculator(1).Invoke(12,2));
            Console.WriteLine(obj.Calculator(2).Invoke(10, 12));
            Console.WriteLine(obj.Calculator(3).Invoke(10, 12));
            Console.Read();
        }
    }

If I've changed Business layer function name(Add() to AddNumber()), Here no need to change UI Layer. So it is calling Loosely Coupled.