What does it mean to program to an interface?
Asked Answered
G

17

22

I keep hearing the statement on most programming related sites:

Program to an interface and not to an Implementation

However I don't understand the implications?
Examples would help.

EDIT: I have received a lot of good answers even so could you'll supplement it with some snippets of code for a better understanding of the subject. Thanks!

Gauleiter answered 11/9, 2009 at 22:25 Comment(0)
D
38

You are probably looking for something like this:

public static void main(String... args) {
  // do this - declare the variable to be of type Set, which is an interface
  Set buddies = new HashSet();

  // don't do this - you declare the variable to have a fixed type
  HashSet buddies2 = new HashSet();
}

Why is it considered good to do it the first way? Let's say later on you decide you need to use a different data structure, say a LinkedHashSet, in order to take advantage of the LinkedHashSet's functionality. The code has to be changed like so:

public static void main(String... args) {
  // do this - declare the variable to be of type Set, which is an interface
  Set buddies = new LinkedHashSet();  // <- change the constructor call

  // don't do this - you declare the variable to have a fixed type
  // this you have to change both the variable type and the constructor call
  // HashSet buddies2 = new HashSet();  // old version
  LinkedHashSet buddies2 = new LinkedHashSet();
 }

This doesn't seem so bad, right? But what if you wrote getters the same way?

public HashSet getBuddies() {
  return buddies;
}

This would have to be changed, too!

public LinkedHashSet getBuddies() {
  return buddies;
}

Hopefully you see, even with a small program like this you have far-reaching implications on what you declare the type of the variable to be. With objects going back and forth so much it definitely helps make the program easier to code and maintain if you just rely on a variable being declared as an interface, not as a specific implementation of that interface (in this case, declare it to be a Set, not a LinkedHashSet or whatever). It can be just this:

public Set getBuddies() {
  return buddies;
}

There's another benefit too, in that (well at least for me) the difference helps me design a program better. But hopefully my examples give you some idea... hope it helps.

Dishonor answered 11/9, 2009 at 22:42 Comment(6)
Wow! That's very well explained!Gauleiter
Just keep in mind that this principle goes for types that you create as well as built-in abstractions like Set. For example, if you have a "Foo" in your project. Don't just make a "class Foo," instead make "interface Foo" and then have some implementation like "class MyFoo implements Foo" You might even find that your project will have several classes that implement the "Foo" interface. By consistently writing the name of the interface instead of the implementation (except for constructor invocations) you can quickly change Foo implementations to fit your needs.Rebut
Be very careful about OVERusing this principle for you code. If every class has a corresponding interface that gets defined and there's always a 1-to-1 correspondence between thes classes and interfaces, then it just serves to obfuscate the code and make it harder to maintain. Knowing WHEN to introduce interfaces because you might in the future want to introduce a different implementation is key.Universally
I really don't like this standard example about declaring local variables. It's a very weak case for interfaces. Method parameters are a much stronger case - declare a HashSet parameter, and you force clients to use a HashSet. Declare a Set parameter, and they're free to use Collections.singleton(), Collections.emptySet(), or their own custom Set implementation.Sculpture
Michael - You're right, method parameters would make a better example. However, I purposely wanted to give something very simple for the questioner. I start with constructors and move to return types; it would just be another small step to illustrate parameters. I sort of thought he would be able to make that connection himself and so left it out due to brevity.Dishonor
In Swing it is common for interfaces to have an Adapter providing a dummy implementation. It is then easy to extend the adapter to override a single method instead of providing a full set of dummy methods everytime. Very nice for anonymous functions.Candicecandid
P
19

One day, a junior programmer was instructed by his boss to write an application to analyze business data and condense it all in pretty reports with metrics, graphs and all that stuff. The boss gave him an XML file with the remark "here's some example business data".

The programmer started coding. A few weeks later he felt that the metrics and graphs and stuff were pretty enough to satisfy the boss, and he presented his work. "That's great" said the boss, "but can it also show business data from this SQL database we have?".

The programmer went back to coding. There was code for reading business data from XML sprinkled throughout his application. He rewrote all those snippets, wrapping them with an "if" condition:

if (dataType == "XML")
{
   ... read a piece of XML data ...
}
else
{
   .. query something from the SQL database ...
}

When presented with the new iteration of the software, the boss replied: "That's great, but can it also report on business data from this web service?" Remembering all those tedious if statements he would have to rewrite AGAIN, the programmer became enraged. "First xml, then SQL, now web services! What is the REAL source of business data?"

The boss replied: "Anything that can provide it"

At that moment, the programmer was enlightened.

Patrickpatrilateral answered 11/9, 2009 at 23:51 Comment(3)
Wonderful!.. Surely written by an Enlightened person!Gauleiter
Er!..But could you enlighten me!...What did the programmer do?Gauleiter
He realized he could make most code agnostic about the type of data source by programming against a single abstraction of the data source; an interface! Adding support for new data source types then becomes a matter of creating a new implementation of the interface. The rest of the code doesn't have to be touched. This is the data mapper pattern.Patrickpatrilateral
B
8

An interface defines the methods an object is commited to respond.

When you code to the interface, you can change the underlying object and your code will still work ( because your code is agnostic of WHO do perform the job or HOW the job is performed ) You gain flexibility this way.

When you code to a particular implementation, if you need to change the underlying object your code will most likely break, because the new object may not respond to the same methods.

So to put a clear example:

If you need to hold a number of objects you might have decided to use a Vector.

If you need to access the first object of the Vector you could write:

 Vector items = new Vector(); 
 // fill it 
 Object first = items.firstElement();

So far so good.

Later you decided that because for "some" reason you need to change the implementation ( let's say the Vector creates a bottleneck due to excessive synchronization)

You realize you need to use an ArrayList instad.

Well, you code will break ...

ArrayList items = new ArrayList();
// fill it  
Object first = items.firstElement(); // compile time error. 

You can't. This line and all those line who use the firstElement() method would break.

If you need specific behavior and you definitely need this method, it might be ok ( although you won't be able to change the implementation ) But if what you need is to simply retrieve the first element ( that is , there is nothing special with the Vector other that it has the firstElement() method ) then using the interface rather than the implementation would give you the flexibility to change.

 List items = new Vector();
 // fill it 
 Object first = items.get( 0 ); //

In this form you are not coding to the get method of Vector, but to the get method of List.

It does not matter how do the underlying object performs the method, as long as it respond to the contract of "get the 0th element of the collection"

This way you may later change it to any other implementation:

 List items = new ArrayList(); // Or LinkedList or any other who implements List
 // fill it 
 Object first = items.get( 0 ); // Doesn't break

This sample might look naive, but is the base on which OO technology is based ( even on those language which are not statically typed like Python, Ruby, Smalltalk, Objective-C etc )

A more complex example is the way JDBC works. You can change the driver, but most of your call will work the same way. For instance you could use the standard driver for oracle databases or you could use one more sophisticated like the ones Weblogic or Webpshere provide . Of course it isn't magical you still have to test your product before, but at least you don't have stuff like:

 statement.executeOracle9iSomething();

vs

statement.executeOracle11gSomething();

Something similar happens with Java Swing.

Additional reading:

Design Principles from Design Patterns

Effective Java Item: Refer to objects by their interfaces

( Buying this book the one of the best things you could do in life - and read if of course - )

Bondholder answered 11/9, 2009 at 23:7 Comment(2)
Fantastic! The books seem amazing too! Regarding the code, we have changed a line of code in the program from List items = new Vector(); to List items = new ArrayList(); so eventually we had to change some code in the client area so might as well change the remaining lines of code related to Vector that are going to break. I still dont understand the significance.Gauleiter
It sounds like you have code which is expecting to use a Vector, which is exactly what is meant by "programming to an implementation". Your other code now expects to use Vector. If, instead, you had your code expect the data structure to be a List (in other words, you declared items to be a List, so don't treat it like a specific subclass of List, and you thus "program to an interface"), then your other code wouldn't need modification later when you change the actual type by changing "new Vector()" to "new ArrayList()".Dishonor
S
7

My initial read of that statement is very different than any answer I've read yet. I agree with all the people that say using interface types for your method params, etc are very important, but that's not what this statement means to me.

My take is that it's telling you to write code that only depends on what the interface (in this case, I'm using "interface" to mean exposed methods of either a class or interface type) you're using says it does in the documentation. This is the opposite of writing code that depends on the implementation details of the functions you're calling. You should treat all function calls as black boxes (you can make exceptions to this if both functions are methods of the same class, but ideally it is maintained at all times).

Example: suppose there is a Screen class that has Draw(image) and Clear() methods on it. The documentation says something like "the draw method draws the specified image on the screen" and "the clear method clears the screen". If you wanted to display images sequentially, the correct way to do so would be to repeatedly call Clear() followed by Draw(). That would be coding to the interface. If you're coding to the implementation, you might do something like only calling the Draw() method because you know from looking at the implementation of Draw() that it internally calls Clear() before doing any drawing. This is bad because you're now dependent on implementation details that you can't know from looking at the exposed interface.

I look forward to seeing if anyone else shares this interpretation of the phrase in the OP's question, or if I'm entirely off base...

Sanguine answered 11/9, 2009 at 22:59 Comment(1)
That's my understanding too - programming to an interface means to program to the documentation, the specification, or the protocol. So if you're programming to an interface, a discussion about "does X work" isn't "I tried it and it works/doesn't work", it's more along the lines of "I read the documentation, and it permits X/forbids X/doesn't mention X/is ambiguous about X".Bastion
L
5

It's a way to separate responsibilities / dependancies between modules. By defining a particular Interface (an API), you ensure that the modules on either side of the interface won't "bother" one another.

For example, say module 1 will take care of displaying bank account info for a particular user, and module2 will fetch bank account info from "whatever" back-end is used.

By defining a few types and functions, along with the associated parameters, for example a structure defining a bank transaction, and a few methods (functions) like GetLastTransactions(AccountNumber, NbTransactionsWanted, ArrayToReturnTheseRec) and GetBalance(AccountNumer), the Module1 will be able to get the needed info, and not worry about how this info is stored or calculated or whatever. Conversely, the Module2 will just respond to the methods call by providing the info as per the defined interface, but won't worry about where this info is to be displayed, printed or whatever...

When a module is changed, the implementation of the interface may vary, but as long as the interface remains the same, the modules using the API may at worst need to be recompiled/rebuilt, but they do not need to have their logic modified in anyway.

That's the idea of an API.

Lucilla answered 11/9, 2009 at 22:29 Comment(0)
B
4

At its core, this statement is really about dependencies. If I code my class Foo to an implementation (Bar instead of IBar) then Foo is now dependent on Bar. But if I code my class Foo to an interface (IBar instead of Bar) then the implementation can vary and Foo is no longer dependent on a specific implementation. This approach gives a flexible, loosely-coupled code base that is more easily reused, refactored and unit tested.

Biblioclast answered 11/9, 2009 at 22:32 Comment(0)
Q
4

Take a red 2x4 Lego block and attach it to a blue 2x4 Lego block so one sits atop the other. Now remove the blue block and replace it with a yellow 2x4 Lego block. Notice that the red block did not have to change even though the "implementation" of the attached block varied.

Now go get some other kind of block that does not share the Lego "interface". Try to attach it to the red 2x4 Lego. To make this happen, you will need to change either the Lego or the other block, perhaps by cutting away some plastic or adding new plastic or glue. Notice that by varying the "implementation" you are forced to change it or the client.

Being able to let implementations vary without changing the client or the server - that is what it means to program to interfaces.

Quadrennium answered 11/9, 2009 at 22:44 Comment(1)
I like this analogy!Grating
L
3

An interface is like a contract between you and the person who made the interface that your code will carry out what they request. Furthermore, you want to code things in such a way that your solution can solve the problem many times over. Think code re-use. When you are coding to an implementation, you are thinking purely of the instance of a problem that you are trying to solve. So when under this influence, your solutions will be less generic and more focused. That will make writing a general solution that abides by an interface much more challenging.

Larrigan answered 11/9, 2009 at 22:27 Comment(1)
This answer is neither a statement for or against this methodology, FYI.Larrigan
S
3

Look, I didn't realize this was for Java, and my code is based on C#, but I believe it provides the point.

Every car have doors.

But not every door act the same, like in UK the taxi doors are backwards. One universal fact is that they "Open" and "Close".

interface IDoor
{
    void Open();
    void Close();
}

class BackwardDoor : IDoor
{
    public void Open()
    {
       // code to make the door open the "wrong way".
    }

    public void Close()
    {
       // code to make the door close properly.
    }
}

class RegularDoor : IDoor
{
    public void Open()
    {
        // code to make the door open the "proper way"
    }

    public void Close()
    {
        // code to make the door close properly.
    }
}

class RedUkTaxiDoor : BackwardDoor
{
    public Color Color
    {
        get
        {
            return Color.Red;
        }
    }
}

If you are a car door repairer, you dont care how the door looks, or if it opens one way or the other way. Your only requirement is that the door acts like a door, such as IDoor.

class DoorRepairer
{
    public void Repair(IDoor door)
    {
        door.Open();
        // Do stuff inside the car.
        door.Close();
    }
}

The Repairer can handle RedUkTaxiDoor, RegularDoor and BackwardDoor. And any other type of doors, such as truck doors, limousine doors.

DoorRepairer repairer = new DoorRepairer();

repairer.Repair( new RegularDoor() );
repairer.Repair( new BackwardDoor() );
repairer.Repair( new RedUkTaxiDoor() );

Apply this for lists, you have LinkedList, Stack, Queue, the normal List, and if you want your own, MyList. They all implement the IList interface, which requires them to implement Add and Remove. So if your class add or remove items in any given list...

class ListAdder
{
    public void PopulateWithSomething(IList list)
    {
         list.Add("one");
         list.Add("two");
    }
}

Stack stack = new Stack();
Queue queue = new Queue();

ListAdder la = new ListAdder()
la.PopulateWithSomething(stack);
la.PopulateWithSomething(queue);
Sardonic answered 12/9, 2009 at 3:14 Comment(2)
No problem! regarding the language Java or C# as long as we get the understanding. Thanks!Gauleiter
In Java the convention is to use Door and not IDoor for the interface.Candicecandid
O
2

In addition to the other answers, I add more:

You program to an interface because it's easier to handle. The interface encapsulates the behavior of the underlying class. This way, the class is a blackbox. Your whole real life is programming to an interface. When you use a tv, a car, a stereo, you are acting on its interface, not on its implementation details, and you assume that if implementation changes (e.g. diesel engine or gas) the interface remains the same. Programming to an interface allows you to preserve your behavior when non-disruptive details are changed, optimized, or fixed. This simplifies also the task of documenting, learning, and using.

Also, programming to an interface allows you to delineate what is the behavior of your code before even writing it. You expect a class to do something. You can test this something even before you write the actual code that does it. When your interface is clean and done, and you like interacting with it, you can write the actual code that does things.

Oft answered 11/9, 2009 at 23:0 Comment(1)
As an example I had seen a class which had several private methods, and a couple of public methods which were internally calling these private methods. In this case we are exposing only a subset of the API. Is this good programming practice?Gauleiter
S
2

Allen Holub wrote a great article for JavaWorld in 2003 on this topic called Why extends is evil. His take on the "program to the interface" statement, as you can gather from his title, is that you should happily implement interfaces, but very rarely use the extends keyword to subclass. He points to, among other things, what is known as the fragile base-class problem. From Wikipedia:

a fundamental architectural problem of object-oriented programming systems where base classes (superclasses) are considered "fragile" because seemingly safe modifications to a base class, when inherited by the derived classes, may cause the derived classes to malfunction. The programmer cannot determine whether a base class change is safe simply by examining in isolation the methods of the base class.

Solis answered 12/9, 2009 at 4:13 Comment(0)
H
2

"Program to an interface" can be more flexible.

For example, we are writing a class Printer which provides print service. currently there are 2 class (Cat and Dog) need to be printed. So we write code like below

class Printer
{
    public void PrintCat(Cat cat)
    {
        ...
    }

    public void PrintDog(Dog dog)
    {
        ...
    }
    ...
}

How about if there is a new class Bird also needs this print service? We have to change Printer class to add a new method PrintBird(). In real case, when we develop Printer class, we may have no idea about who will use it. So how to write Printer? Program to an interface can help, see below code

class Printer
{
    public void Print(Printable p)
    {
        Bitmap bitmap = p.GetBitmap();

        // print bitmap ...
    }
}

With this new Printer, everything can be printed as long as it implements Interface Printable. Here method GetBitmap() is just a example. The key thing is to expose an Interface not a implementation.

Hope it's helpful.

Helfrich answered 28/11, 2012 at 9:38 Comment(2)
I can't even understand this answer... what is the point of it? And are you seriously saying that it's OK to write code that exposes 100 points of potential refactoring when you could (fairly easily) expose just one?Churinga
thanks @Andrew, I have edited my post, hope this time it's better :). and also, weiji's example is good, my previous opinion was wrong, to keep code clear and refactor places less is always a good idea.Helfrich
A
1

Essentially, interfaces are the slightly more concrete representation of general concepts of interoperation - they provide the specification for what all the various options you might care to "plug in" for a particular function should do similarly so that code which uses them won't be dependent on one particular option.

For instance, many DB libraries act as interfaces in that they can operate with many different actual DBs (MSSQL, MySQL, PostgreSQL, SQLite, etc.) without the code that uses the DB library having to change at all.

Overall, it allows you to create code that's more flexible - giving your clients more options on how they use it, and also potentially allowing you to more easily reuse code in multiple places instead of having to write new specialized code.

Artina answered 11/9, 2009 at 22:31 Comment(0)
J
1

By programming to an interface, you are more likely to apply the low coupling / high cohesion principle. By programming to an interface, you can easily switch the implementation of that interface (the specific class).

Judy answered 11/9, 2009 at 22:33 Comment(0)
K
1

It means that your variables, properties, parameters and return types should have an interface type instead of a concrete implementation.

Which means you use IEnumerable<T> Foo(IList mylist) instead of ArrayList Foo(ArrayList myList) for example.

Use the implementation only when constructing the object:

IList list = new ArrayList();

If you have done this you can later change the object type maybe you want to use LinkedList instead of ArrayList later on, this is no problem since everywhere else you refer to it as just "IList"

Karns answered 11/9, 2009 at 22:44 Comment(0)
C
0

It's basically where you make a method/interface like this: create( 'apple' ) where the method create(param) comes from an abstract class/interface fruit that is later implemented by concrete classes. This is different than subclassing. You are creating a contract that classes must fulfill. This also reduces coupling and making things more flexible where each concrete class implements it differently.

The client code remains unaware of the specific types of objects used and remains unaware of the classes that implement these objects. Client code only knows about the interface create(param) and it uses it to make fruit objects. It's like saying, "I don't care how you get it or make it I, just want you to give it to me."

An analogy to this is a set of on and off buttons. That is an interface on() and off(). You can use these buttons on several devices, a TV, radio, light. They all handle them differently but we don't care about that, all we care about is to turn it on or turn it off.

Coxcombry answered 12/2, 2015 at 4:5 Comment(0)
P
0

Coding to an interface is a philosophy, rather than specific language constructs or design patterns - it instructs you what is the correct order of steps to follow in order to create better software systems (e.g. more resilient, more testable, more scalable, more extendible, and other nice traits).

What it actually means is:

===

Before jumping to implementations and coding (the HOW) - think of the WHAT:

  • What black boxes should make up your system,
  • What is each box' responsibility,
  • What are the ways each "client" (that is, one of those other boxes, 3rd party "boxes", or even humans) should communicate with it (the API of each box).

After you figure the above, go ahead and implement those boxes (the HOW).

Thinking first of what a box' is and what its API, leads the developer to distil the box' responsibility, and to mark for himself and future developers the difference between what is its exposed details ("API") and it's hidden details ("implementation details"), which is a very important differentiation to have.

One immediate and easily noticeable gain is the team can then change and improve implementations without affecting the general architecture. It also makes the system MUCH more testable (it goes well with the TDD approach).

===
Beyond the traits I've mentioned above, you also save A LOT OF TIME going this direction.

Micro Services and DDD, when done right, are great examples of "Coding to an interface", however the concept wins in every pattern from monoliths to "serverless", from BE to FE, from OOP to functional, etc....

I strongly recommend this approach for Software Engineering (and I basically believe it makes total sense in other fields as well).

Pratfall answered 25/5, 2022 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.