.NET Why Code to Interface?

You may have seen programming design patterns and practices encourage coding to interfaces. And you may have also seen examples as to how to achieve this. The question is, why? Why should a programmer make this his second nature.

The following example would hopefully, shed some light into this, and hopefully help you get into the habit of good coding habits.

Let’s just say we take the “Game of Thrones”. In the book and TV series you would have noticed the numerous characters involved. I begin with creating two classes, representing two types of characters, one for a Knight and another for a Lord.

    class Knight
    {
        // TODO 
    }

    class Lord
    {
        // TODO
    }

As you can see, they do absolutely nothing at the moment. But one of the main functionality for my program is to retrieve character’s attributes. I have the method for this ready, the idea of this method is to retrieve the attributes from a particular character and return them as a string array.

        public string[] GetCharacterAttributes()
        {
            // TODO
        }

I could instantiate my required object from the concrete classes before as below.

        public string[] GetCharacterAttributes()
        {
            var character = new Knight();
            // Retrieve the attributes from Knight
        }

The above would be ok and have absolutely no problems. However, what if I needed to get attributes for a Lord character type as well? I could pass them in as parameters, as below.

        public string[] GetCharacterAttributes(Knight character)
        {
            //  Retrieve Knight attributes.
        }

        public string[] GetCharacterAttributes(Lord character)
        {
            //  Retrieve Lord attributes.
        }

But this would make the code messy and complex. Alternatively, I could do something like the below instead.

        public string[] GetKnightAttributes()
        {
            var character = new Knight();
            // Retrieve the attributes from Knight
        }

        public string[] GetLordAttributes()
        {
            var character = new Lord();
            // Retrieve the attributes from Lord            
        }

The above will work, the code will not be messy, and there would not be any complications. However, I am repeating the code, and I have to do it for each of my concrete class or character. And for those of you who have read the “A Song of Ice and Fire” books would know that this will get insanely hard as more and more characters are introduced.

This is where coding to an interface becomes valuable. I first create an interface called ICharacter, and then use that interface on my concrete character classes as below.

    // Interface for Game of Thrones Characters
    public interface ICharacter
    {
        string Name { get; set; }
    }

    // Implementation of Interface on Knight concrete class.
    class Knight : ICharacter
    {
        public string Name { get; set; }
    }

    // Implementation of Interface for Lord concrete class.
    class Lord : ICharacter
    {
        public string Name { get; set; }
    }

Now let’s go back to our method of retrieving character attributes. I can now have just the one method, with no issues. All I’m doing now, is passing in the interface as the method parameter.

        public string[] GetCharacterAttributes(ICharacter character)
        {
            // character passed in can be a Knight or a Lord
            // or it can be any other new characters that may get
            // created using the ICharacter interface.
        }

As you can see from the above, by coding to an interface, I do not have to deal with messy or complex code, and I do not need to repeat my code. Please note, there are ways in improving this through, Dependency Injection, but that will be a another post. I hope this helps, thank you.


Follow

Get every new post delivered to your Inbox.