Non-technical explanation of interfaces

April 28, 2014 - Programming

I came up with this analogy for interfaces at my previous job at Avid Nano to explain to my non-programmer boss what it was all about. I was pretty happy with what I came up with so here it is:

Every day objects in life have certain properties to them which are common with other objects. In our example we’ll take a normal every day situation – turning things with your hand – and apply this to a programming context.

The action of turning things with your hand is independent of what you are turning however, the resulting effect can be drastically different. You apply the same motion to a door handle and a tap, yet the resultant effects have no relation to each other – opening the door and water coming out of the tap respectively.

So in this respect, what do the handle and the tap have in common? They are both turn-able, and when they are turned, they do something. And the hand can take something which is turn-able and turn it.

In coding terms, our interface is turn-able – which in C# we write as ITurnable:

public interface ITurnable
{
    void Turn();
}

Let’s break down the above code:

  • public – the access modifier which determines what other parts of your program can see this block of code
  • interface – marks this block of code as an interface
  • ITurnable – the name of our interface. In C# the standard is to prefix the names of interfaces with I
  • void Turn() – any class implementing this interface must define a public method called Turn which accepts no parameters and returns no value (i.e. void)

Now let us implement the interface in our two classes:

public class Tap : ITurnable
{
    public void Turn()
    {
        Console.WriteLine("Water comes out of the tap");
    }
}

public class DoorHandle : ITurnable
{
    public void Turn()
    {
        Console.WriteLine("The door opens");
    }
}

Dissecting again:

  • class – tells us that the following code block is a class – picture this as your real-world item
  • : ITurnable – this means that the class implements the interface ITurnable i.e. “Door Handles are Turnable”
  • public void Turn() – this is fulfilling the contract that the interface dictates – implementing a public method called Turn which has no input parameters and does not return anything. For this example, both of the methods defined just print to the console a short message.

Now we need to consume our interface with a hand:

public class Hand
{
    public void TurnSomething(ITurnable turnable)
    {
        turnable.Turn();
    }
}

Most of this we’ve seen before but we have a couple of new things

  • TurnSomething(ITurnable turnable) – here we are defining a method which consumes an ITurnable, we’ll see how to call this method later
  • turnable.Turn()– because we’ve supplied an ITurnable to this method, we can call the ITurnable.Turn() method without knowing what calling it will do.

You can think of consuming an interface in a method as being blindfolded – you can feel that something is turnable in your hand without knowing what the end result of turning it will be.

Finally, we have a piece of code putting together all of the above:

Hand myHand = new Hand();
DoorHandle aDoorHandle = new DoorHandle();
Tap aTap = new Tap();

myHand.TurnSomething(aDoorHandle); // prints "The door opens"
myHand.TurnSomething(aTap); // prints "Water comes out of the tap"

› tags: c# /