Click here to Skip to main content
13,297,878 members (46,248 online)
Click here to Skip to main content
Add your own
alternative version


57 bookmarked
Posted 27 Jan 2010

When To Use Interfaces

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


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

    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.


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.


  • 27th January, 2010: Initial post


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


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.

You may also be interested in...


Comments and Discussions

QuestionHere is another practical example: Pin
Dietmar Schoder2-Aug-14 11:26
professionalDietmar Schoder2-Aug-14 11:26 
GeneralMy vote of 4 Pin
Arthanarieaswaran16-Oct-12 4:48
memberArthanarieaswaran16-Oct-12 4:48 
GeneralMy vote of 3 Pin
pradiprenushe27-Aug-12 2:41
memberpradiprenushe27-Aug-12 2:41 
GeneralMy vote of 4 Pin
adriancs4-Apr-11 17:46
memberadriancs4-Apr-11 17:46 
GeneralWhen using c#, I always use them together. Pin
Miller428-Jan-10 6:32
memberMiller428-Jan-10 6:32 
GeneralThank you! Pin
Dinesh Mani27-Jan-10 20:52
memberDinesh Mani27-Jan-10 20:52 
I have always felt uneasy when I'm asked about creating interfaces for internal classes and data structures. I have managed to give some reasons or the other but couldn't quote any references.

Going forward I can surely quote this article for my reference. Thank You!!!Thumbs Up | :thumbsup:
GeneralRe: Thank you! Pin
Fred Westrom5-Feb-10 7:03
memberFred Westrom5-Feb-10 7:03 
GeneralInterface Vs Abstract Class Pin
Bibhas Paul27-Jan-10 17:33
memberBibhas Paul27-Jan-10 17:33 
GeneralShort and to the point Pin
Doddy200527-Jan-10 12:57
memberDoddy200527-Jan-10 12:57 
GeneralRe: Short and to the point Pin
Fred Westrom5-Feb-10 7:06
memberFred Westrom5-Feb-10 7:06 
GeneralCOM - an interface based framework. Pin
yafan27-Jan-10 12:56
memberyafan27-Jan-10 12:56 
GeneralMaybe a more real world example Pin
hammerstein0527-Jan-10 10:54
memberhammerstein0527-Jan-10 10:54 

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

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

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