Click here to Skip to main content
Click here to Skip to main content

When To Use Interfaces

, 27 Jan 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
A suggestion for when to use or not use interfaces

Introduction

Frequently, I encounter practices in software development that defy logical explanation. One of the more common practices is the misuse of interfaces. In this article, I describe the basic purpose for the interface type construct and explore the question of when to use them.

I would like to preface this article by stating that there are exceptions to all of the cases that I will describe, and that some developers may disagree on some points. However, I believe that my methods result in code that is easier to follow, easier to maintain, and avoid increasing the required effort when doing so does not provide a measurable benefit.

What Is An Interface?

An interface is nothing more than an agreement of how two pieces of code will interact. One piece of code implements the interface and another uses the interface. The interface acts as a decoupling layer that enables the replacement of the object implementing the interface.

To illustrate this point, let’s examine a simple interface usage.

public interface IAdder
{
    int Add(int a, int b);
}

public class Example
{
    public IAdder Adder
    {
        get;
        set;
    }

    public void DoSomething()
    {
        var two = Adder.Add(1, 1);
    }
}

In the above example, DoSomething uses an adder to add two numbers, but the Example class has no knowledge of the implementation of the adder.

Actions/Services

Classes that provide an action or a service are very often candidates for interface abstraction. The classic example is a WCF service. WCF services define service contracts, and communication is performed via the interface without any client dependency on the service implementation.

Let’s look at a variation of the adder interface, and assume the same usage as above.

public interface IAdder
{
    double Add(double a, double b);
}

public class DoubleAdder : IAdder
{
    public double Add(double a, double b)
    {
        return a + b;
    }
}

public class IntAdder : IAdder
{
    public double Add(double a, double b)
    {
        return (int)a + (int)b;
    }
}

The usefulness of defining the interface is obvious when we have a variety of implementations of the action that can be used by a single unchanged piece of code, where the only requirement is that the code be provided with an instance of the desired implementation.

Data Structures

Classes that represent data are unlikely to be candidates for interface abstraction. The reasoning behind this is that the actual structure of the data may be as important to using the data as the members of the data structure. An example of this point is seen when using WCF data contracts, where the serialized form of the data is critical to being able to use the data.

Again, we will modify the adder interface to illustrate this point.

public class Number { /* ... */ }
    
public interface IAdder
{
    Number Add(Number a, Number b);
}

In this case, the Number is the data passed as arguments to the adder. In essence, the Number class is as much a part of the adder interface as the Add method itself. Although a number interface could be used, the benefit of doing so is unclear, and the ability to remotely access the adder may be compromised. Would there really ever be a need to replace the number with a different implementation of the underlying data?

Internal Details

Classes that are used internally to some activity but that are not exposed publicly from the assembly are unlikely to be candidates for interface abstraction. Although there are definitely exceptions to this rule, the classic case of an internal class does not benefit from interface abstraction.

The classic case is illustrated below in an implementation of the adder interface’s method.

public class DoubleAdder : IAdder
{
    public double Add(double a, double b)
    {
        return new MyCalculator().Add(a, b);
    }
}

internal class MyCalculator
{
    internal double Add(double a, double b)
    {
        return a + b;
    }
}

Here, we can see that the internal MyCalculator class is used to contain the logic, and the DoubleAdder class is simply an adapter to the internal calculator implementation. Adding an interface to the internal class would not provide any benefit, and would increase the necessary effort to maintain the class if the internal method signatures were to change.

History

  • 27th January, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Fred Westrom
Architect Medseek
United States United States
Hello, I'm Fred. I've been working with software since I was a kid, and the foreseeable future looks to hold more of the same.
 
When I was in 4th grade, I had a homework assignment where I was supposed to write out a list of prime numbers less than 100. Instead of writing it out by hand, I spent a couple of minutes writing a simple C program, and turned in a list of prime numbers less than 1,000,000.
 
Since then, I have enjoyed wasting countless hours (years) playing with various technologies, patterns, and code constructs, and occasionally learn something useful.

Comments and Discussions

 
QuestionHere is another practical example: PinprofessionalDietmar Schoder2-Aug-14 11:26 
GeneralMy vote of 4 PinmemberArthanarieaswaran16-Oct-12 4:48 
GeneralMy vote of 3 Pinmemberpradiprenushe27-Aug-12 2:41 
GeneralMy vote of 4 Pinmemberadriancs4-Apr-11 17:46 
GeneralWhen using c#, I always use them together. PinmemberMiller428-Jan-10 6:32 
GeneralThank you! PinmemberDinesh Mani27-Jan-10 20:52 
GeneralRe: Thank you! PinmemberFred Westrom5-Feb-10 7:03 
GeneralInterface Vs Abstract Class PinmemberBibhas Paul27-Jan-10 17:33 
GeneralShort and to the point PinmemberDoddy200527-Jan-10 12:57 
GeneralRe: Short and to the point PinmemberFred Westrom5-Feb-10 7:06 
GeneralCOM - an interface based framework. Pinmemberyafan27-Jan-10 12:56 
GeneralMaybe a more real world example Pinmemberhammerstein0527-Jan-10 10:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 27 Jan 2010
Article Copyright 2010 by Fred Westrom
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid