0

In the Head First Design Patterns book, the author often says that one should program to interface rather than implementation?

What does that mean?

1

4 Answers 4

5

Let's illustrate it with the following code:

namespace ExperimentConsoleApp
{
    class Program
    {
        static void Main()
        {
            ILogger loggerA = new DatabaseLogger();

            ILogger loggerB = new FileLogger();

            loggerA.Log("My message");
            loggerB.Log("My message");
        }
    }

    public interface ILogger
    {
        void Log(string message);
    }

    public class DatabaseLogger : ILogger
    {
        public void Log(string message)
        {
            // Log to database
        }
    }

    public class FileLogger : ILogger
    {
        public void Log(string message)
        {
            // Log to File
        }
    }

}

Suppose you are the Logger developer and the application developer needs a Logger from you. You give the Application developer your ILogger interface and you say to him he can use but he doesn't have to worry about the implementation details.

After that you start developing a FileLogger and Databaselogger and you make sure they follow the interface that you gave to the Application developer.

The Application developer is now developing against an interface, not an implementation. He doesn't know or care how the class is implemented. He only knows the interface. This promotes less coupling in code and it gives you the ability to (trough configuration files for example) easily switch to another implementation.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I understand. We should change the interface later on if we feel we something new feature to add? Will that cause some problems?
Most of the time adding a method to an interface shouldn't give any problems. Changing an existing method signature will force your clients to change how they use your interface.
1

Worry more about what a class does rather than how it does it. The latter should be an implementation detail, encapsulated away from clients of your class.

If you start with an interface, you're free to inject in a new implementation later without affecting clients. They only use references of the interface type.

3 Comments

Worry more about what a class does rather than how it does it. This statement may suit a user. But what should a "developer" make out of this?
EH! do you mean that first we should design the interface and then write the code according to the interface?
Exactly, consider the programming interface as a contract that your code must follow and not the other way. When you code you should stipulate what is the main purpose of some module, you build an interface basing that purpose and finally you implement the interface
0

It means that when working with a class, you should only program against the public interface and not make assumptions about how it was implemented, as it may change.

Normally this translates to using interfaces/abstract classes as variable types instead of concrete ones, allowing one to swap implementations if needed.

In the .NET world one example is the use of the IEnumerable/IEnumerator interfaces - these allow you to iterate over a collection without worrying how the collection was implemented.

2 Comments

Please read my comments on the other answer and confirm whether I am making sense or not.
@AnishaKaul - It is not as clear cut as that, but more often than not, that is indeed what it boils down to.
0

It is all about coupling. Low coupling is a very important property* of software architecture. The less you need to know about your dependency the better.

Coupling can be measured by the number of assumptions you have to make in order to interact/use your dependency (paraphrasing M Fowler here).

So when using more generic types we are more loosely coupled. We are for example de-coupled from a particular implementation strategy of a collection: linked list, double linked list, arrays, trees, etc. Or from the classic OO school: "what exact shape it is: rectangle, circle, triangle", when we just want to dependent on a shape (in old school OO we apply polymorphism here)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.