A Few Good Reasons to use Interfaces

An interface acts as a mediator or an adapter allowing information to flow between two or more components. In Computing, interfaces are regarded as contracts or boundaries defining the points of interactions where classes can communicate with each other.

Interfaces are defined using the keyword interface and contain methods (always public) without a body.

Example of a simple interface.

<?php
interface MenuInterface
{
    public function getPrice()
}

A Few Real-Life Examples of Interfaces

  • Divorce Lawyers keeping things civil as you and your formerly beloved go to war.
  • The local clergy interceding between you and your Deity of choice (no judgment what so ever, I’m not agnostic).
  • Hostage negotiators 👀( letting my imagination run a little wild here).
  • eBay acting as the auction host accepting multiple bids on for a pair of exclusive Air More Uptempo Supreme sneaker ( I can only apologize, my love of sneakers is getting in the way of good writing again).
  • The Adaptor you plug into the electrical mains when you travel 🔌🔋.

About Those Reasons

  • Abstraction – You only need to know the methods, you need to know, Capisce!
    Any changes to the inner workings of the other component are of no concern to you. (Almost sounds like a life lesson).
  • Consistency – Using interfaces enforces a list of methods required by on all class implementing that interface allowing us to keep the same methods of communication between multiple components sharing that same interface.
  • Allows us to reuse older classes and make them available for use without changing the original class.

Design Patterns of Note

Several Design Patterns use interfaces in their approaches. Some patterns to note include Adapter Pattern is particularly useful when you have an existing class that lacks methods you need and therefore requires adapting into the methods that’s required. Can’t take credit for this analogy but

It is like the problem of inserting a new three-prong electrical plug in an old two-prong wall outlet – some kind of adapter or intermediary is necessary.

-sourcemaking.com

Bridge Pattern is similar to Adapter except that it doesn’t apply to existing classes.

More design patterns available here.

To the code 👉

We take the earlier example and expand on it.

Create a MenuInterface class.

<?php   
interface MenuInterface 
{
    public function getPrice();
    public function isGlutenFree();
}

Create a Food class implementing MenuInterface with all the methods inherited from MenuInterface.

<?php
class Bread implements MenuInterface {
    private $rate = "$1.50";
    public function getPrice()
    {
        return $this->rate;
    }
    public function isGlutenFree()
    {
        return false;
    }
}

class Coffee implements MenuInterface {
    private $rate = "$1.00";
    public function getPrice()
    {
        return $this->rate;
    }
    public function isGlutenFree()
    {
        return true;
    }
}

Based on the code above we can now write a Waiter class that access all the price and weight details.

<?php
class Waiter {
    __construct (MenuInterface $item)
    {
        $this->foodItem = $item;
    }
    public function getPrice(MenuInterface $item)
    {
        return $foodItem->getPrice();
    }
    public function isGlutenFree(MenuInterface $item)
    {
        return $foodItem->getPrice();
    }
}

Interfaces allow users to reduce dependency between component as such if we change how the price for bread is calculated the waiter class can remain blissfully ignorant and carry on!

References and Resources

1
0

Related Posts