My Blog

Program to an Interface, not an Implementation

In the legendry book by GOF named “Design Patterns Elements of Reusable Software Architectures”, this phrase is used at quite a places, and somehow it is quite misinterpreted while implementing in languages like C#, since C# has an explicit construct named Interface.

So, what does the above phrase mean?

First let us dissect this a bit, and come to some common definition. In OOPS world, when we say interface or type, we mean capabilities or behaviors which an object can support. So, if a class defines two methods and one property, it is expected that when the corresponding methods or properties are called on that classes instance, the object will behave in that intended fashion, by invoking the desired behavior. This set of capabilities or behaviors is known as interface or type. Hence, class defines the implementation, and the interface or type defines the set of requests to which the object can respond. So, an object can have many types or interfaces, and objects of different classes can have the same interface or type.

So, now we know that interface or type in OOPS parlance, is a notional thing, meaning set of capabilities or behaviors, and implementation is the way in which we define and implement them. Then how come we ever define an OOPS interface? Well, from a more realistic standpoint, whatever type, be it a class, interface or struct, we define with any set of methods or properties, not only is an implementation, but also defines the interface as well, as those methods are the behaviors or capabilities which that object posses. Since in the earlier languages like C or C++, there was no way of explicitly defining the interface only, .NET included the keyword interface, which is realistically a pure abstract class, and which only contains the method/property signatures rather than the implementation. But, this also does not mean that a class can’t be used instead for an interface, it certainly be can.

So what does it mean to say that program to an interface, rather than implementation? As we now know, interface means set of capabilities, we should only be using the type which defines the capabilities, be it an abstract class, a concrete super class or in interface. So, program to a super type rather than the child type, is what this sentence mean. And, this brings in the whole benefits of polymorphism into play. Just by following this rule, you get two very important benefits as stated in the GOF book:

1.       Clients remain unaware of the specific types of objects they use, as long as the objects adhere to the interface that the clients expect.

2.       Clients remain unaware of the classes that implement these objects. Clients only know about the base/abstract classes or interfaces (C# interface) defining the interface.

And this greatly reduces the implementation dependencies.
Published Thursday, June 12, 2008 9:34 AM by sachinsrb
Filed under: ,
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker