Wednesday 6 June 2012

What the HELL is an Interface

     As .Net folks, we have never used an Interface to solve a particular problem. We have lot of definition memorized that pops up when we were asked to define the INTERFACE. 
   Most of the useless definitions are Jargon's such as “Blueprint of your class” or “Detailed plan of your class or class without implementation.
We always forget the definition or it does not gives a better feeling that we understood what it actually mean
I know where it is used, how to use it, but somehow in back of my mind, am not sure about the actual definition what it means
Until I learned in the book Design Patterns by Erich Gamma, there he taught me EXACTLY what an interface is.
                                     INTERFACE IS A TYPE
As string and integer are data types, INTERFACE IS WAY TO DEFINE YOUR OWN TYPE.
Any variable defined as String can hold alphanumeric values, any variable that is defined as integer can hold numeric values, same way any variable defined as specific Interface can hold object value of an implemented class.
 Just remember that INTERFACE IS A TYPE (Say that, three times to yourself), you can see the example so you never have to learn the definition or meaning of it again.
Next time when you come across Interface you know exactly where it is used, what is it purpose and where else you can use the interface.


I will take the same car example which is used worldwide for explaining Interface, but will add different connotation to it so that you can understand exactly what it means. Stick till the end, you will have better understanding

    interface ICar
    {
        public void MoveSteering();
        public void ApplyBrakes(int limit);
    }


You need a UI to move two cars from parking lot to lobby. When the “Move Ford” Button is clicked the ford car is parked in Lobby and When the “Move Hyundai” is clicked the Hyundai is parked in lobby.





   
    public class Ford:ICar
    {
        public void MoveSteering()
        {
            //Implementation Here
        }

        public void ApplyBrakes(int limit)
        {
            //Implementation Here
        }
    }


    public class Hyundai:ICar
    {
       public void MoveSteering()
        {
            //Implementation Here
        }

       public void ApplyBrakes(int limit)
        {
            //Implementation Here
        }
    }




Both Ford and Hyundai have implemented Interface, meaning they have to add the methods that are defined in the interface and also add actual implementation to it.
private void btnFord_Click(object sender, EventArgs e)
        {
            MoveCarToDisplay("Ford");
            lblResult.Text = "Ford Car Parked in Lobby";
        }

        private void btnHyundai_Click(object sender, EventArgs e)
        {
            MoveCarToDisplay("Hyundai");
            lblResult.Text = "Hyundai Car Parked in Lobby";
        }




MoveCarToDisplay is called with the parameter Hyundai or Ford based upon the button clicked

private void MoveCarToDisplay(string carName)
        {
            ICar car = null;
            if (carName == "Ford")
            {
                car = new Ford();
            }
            else if (carName == "Hyundai")
            {
                car = new Hyundai();
            }

            car.MoveSteering();
            car.ApplyBrakes(100);
        }



Based on the parameter, it create an instance car, since interface is a type of car, it can hold Hyundai or Ford. Like integer can hold 1 or 2 or any number which is of type number.


Please try this code once. You can take the code and try in your system compile it and get a sample. Once you have done a sample you will have clear idea about interface and implementation. Next time, when you code you know where to introduce interface where you need not.

Build Bot using LUIS