Home > SOLID > Interfaces & TDD

Interfaces & TDD

I was having a discussion with a friend and was trying to explain why do you need an interface ( separation ) and how it can help you think in terms of SOLID principle & Test Driven Development philosophy..

From the discussion I realized it is hard to explain things by just talking over it and thought I will write down into a blog post.So here it goes ….

If there is business requirement and the user stakeholder says that “I need a Car which I could drive” we generally think of implementing in code like this :-

Car code

But the problem is that if we need to test the drive method or hook it up to UI for mocking, we have to create Real Car object and real implementation of the Drive() method.

For some people it doesn’t sound too bad but just imagine if it was like creating a database connection/connecting to Active directory/calling a webservice etc.
and getting some data would require a lot of pluming/the whole infrastructure to be developed before we could even write a single line of test.

Now in terms of strict TDD you would start with the test code and as a result of the test code you could create the interface. But lets say you are not following a strict TDD principles but thinking of separation and some of the principles of “SOLID” like the “I” Interface segregation and “S”Single responsibility.

We would start with something like this :-

IDrive interface

With this interface we are separating the function from the main class so that in future we could plug-in a “Railway train” or something else and still can hook up the Drive() method and we are also applying the “D” and “L” principle of “SOLID”.

So lets re factor the Car class as

Re factored class

Now with the separation of function based on the interface we can create a Fake implementation and not concern at this stage about the actual implementation.

And the fake test would look like :-

And once we are happy with the test result we can implement the real class like this

And it’s integration test(with real object) would be

As you can see the implementation is almost the same as the fake one and you can control how the interface gets the Fake or the Real object.
( I smell some DI and IoC here 😉 )

Categories: SOLID Tags: , , ,
  1. Yogesh
    June 23, 2010 at 2:06 am

    Nice one and easy to understand as usual.

  2. Monk
    November 8, 2010 at 12:53 pm

    So in this way how would you prevent that the fake classes get compiled for a release version?

    • November 10, 2010 at 10:05 pm

      There are 2 main ways to do this
      1) Use IoC and depenedency Inversion principles and you will see a few post I have done on that..

      2) Use the factory pattern i.e. create an abstract factory which always create the FakeDrive as the default. Your test code will always get use the default factory (abstract class) to get the TestDrive and in your actual code use the Drive factory class which will inherit from the default factory class but override the method ..may be I will do another post on that 😉

  1. June 23, 2010 at 9:15 am
  2. July 30, 2010 at 4:58 am
  3. August 10, 2010 at 1:43 am
  4. November 12, 2010 at 9:42 am
  5. January 27, 2011 at 5:13 am
  6. February 16, 2011 at 8:16 pm
  7. July 19, 2011 at 9:10 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: