cult3

When should I code to an Interface?

Apr 02, 2014

Table of contents:

  1. What is an Interface?
  2. Isn’t that the same as an Abstract Class?
  3. When should you use Interfaces?
  4. Conclusion

The use of Interfaces in programming seems to be something that is often either confused or misinterpreted as being unnecessary. Whilst it is true that you aren’t required to use Interfaces in your code, an Interface provides a contract and ensures that the standard of development remains high.

In this article I’m going to look at the use of Interfaces when writing code, what Interfaces are used for and when you should use them too.

As with other previous articles, all code examples will be in PHP, but these principles apply to many different programming languages.

What is an Interface?

An Interface is simply a contract that states that any class that implements this interface will always have a defined set of methods available.

Following on from last week’s post on Abstract Classes, we might have the following Interface used on both the Twitter and Facebook child classes:

interface Shareable
{
    public function share();

    public function feedback();
}

The Twitter and Facebook child classes would then implement the Interface like this:

class Twitter extends AbstractSocialShare implements Shareable
{
    public function share()
    {
        return $this->tweet;
    }

    public function feedback()
    {
        $this->favourite();
    }
}
class Facebook extends AbstractSocialShare implements Shareable
{
    public function share()
    {
        return $this->post;
    }

    public function feedback()
    {
        return $this->like();
    }
}

So as you can see, by implementing the Shareable interface we can ensure that all classes have the methods that we have defined in the contract.

Isn’t that the same as an Abstract Class?

You might be thinking that an Interface looks a lot like an Abstract Class only without the full method being implemented. In many ways Interfaces and Abstract Classes share the same characteristics, however they do serve different purposes.

Generally speaking, when you use an Abstract Class you are saying that the child class is just a specific instance of a generic object. In the example above we are saying Twitter is an instance of a social sharing site.

When you use an Interface you are saying that this object is capable of doing something. So once again in the example above, by using the Shareable Interface we are saying Twitter is capable of sharing.

Another way in which Abstract Classes and Interfaces differ is the way in which they are implemented. When you extend an Abstract Class, you can’t then extend another class because you are already defining what the child class is an instance of. When implementing an Interface you are free to implement as many Interfaces as you like. So for example, our Twitter class might also have Interfaces such as Friendable, Conntectable and Authenticable depending on how the class should be implemented.

When should you use Interfaces?

You should use an Interface when you will have many child classes that should all be capable of doing certain actions. As I mentioned above, an Interface is a contract, and so by defining and implementing a contract we are free to switch out specific implementations because we can be confident that the method we require will be available on any of the objects that we want to use.

For example, suppose we have an social reader application that allows users to share an article they have just read with their followers on other social sites. We could implement this functionality like this:

class Broadcast
{
    public function __construct(Shareable $site)
    {
        $this->site = $site;
    }

    public function send()
    {
        $this->site->share();
    }
}

Now whatever we pass in to the Broadcast class we can be confident that the object will have the share method available.

Conclusion

Interfaces aren’t a prerequisite for writing code that works, but they are incredibly important when you come to write code as part of a bigger application.

The problem with application development is, things will change as the application evolves over time. Interfaces provide a contract that state that the current object is capable of doing a certain thing. When you start building big complex application these contracts become incredibly important to the architectural integrity of your application.

Interfaces are also important when you are working as part of a team of developers. An Interface provides a public API to your code so when someone else’s code consumes your code they don’t need to care about the implementation details, they only care about the public interface.

Using interfaces in your code can seem unnecessary at first because your code will work without them, but using interfaces is a big step towards being a better developer.

Philip Brown

@philipbrown

© Yellow Flag Ltd 2024.