In this article we'll talk about the fourth of the SOLID principles. Wondering "what were those principles again?" Go ahead and read the summary article.
The Interface Segregation Principle states that no implementation of an interface should be forced to depend on methods it does not use. Have you ever had to implement methods of an interface that you didn't need? If so, you probably created empty methods in your implementation. That is an example of being forced to use an interface, and it violates the Interface Segregation Principle.
In practical terms, this principle asks for interfaces to be granular and focused. Sound familiar? Remember, all five SOLID principles are related, and by violating one you often violate the others. When the Interface Segregation Principle is violated, the Single Responsibility Principle is violated as well.
Instead of having a "bloated" interface containing methods that not all implementations need, it is preferable to have several smaller interfaces that can be implemented individually as needed. By splitting bloated interfaces into smaller, more focused contracts, consuming code can depend on a smaller interface without creating dependencies on parts of the application it will not use.
Interface Segregation Principle
This principle states that no implementation of an interface should be forced to depend on methods it does not use.
To illustrate this principle, let's consider an example session handling library. In fact, we'll look at PHP's own `SessionHandlerInterface`. Here are the methods defined by this interface, which has been included since PHP 5.4:
interface SessionHandlerInterface {
public function close();
public function destroy($sessionId);
public function gc($maxLiftetime);
public function open($savePath, $name);
public function read($sessionId);
public function write($sessionId, $sessionData);
}
Now that you are familiar with the methods defined by this interface, consider an implementation that uses Memcached. Will a Memcached implementation of this interface define functionality for each of these methods? Not only do we not need to implement all of these methods, we don't need half of them at all!
Since Memcached automatically expires the values it holds, we don't need to implement the interface's gc method, nor do we need to implement its open or close methods. So we are forced to define "stubs" for these methods in our implementation that are just empty methods. To fix this problem, let's start by defining a smaller, more focused interface for session garbage collection:
interface GarbageCollectorInterface {
public function gc($maxLifetime);
}
Now that we have a smaller interface, any consuming code can depend on this focused contract, which defines a very narrow set of functionality and does not create a dependency on the entire session handler.
To understand this principle better, let's reinforce our knowledge with another example. Imagine we have a Contact Eloquent class defined like this:
class Contact extends Eloquent {
public function getNameAttribute()
{
return $this->attributes['name'];
}
public function getEmailAttribute()
{
return $this->attributes['email'];
}
}
Now, suppose our application also employs a PasswordReminder class that is responsible for sending password reminder emails to users. PasswordReminder might be defined as follows:
class PasswordReminder {
public function remind(Contact $contact, $view)
{
// Şifre hatırlatıcı e-postası gönder...
}
}
As you may have noticed, our PasswordReminder class depends on the Contact class, which in turn depends on the Eloquent ORM. Coupling the password reminder system to a specific ORM implementation is neither desirable nor necessary. By breaking the dependency, we can freely change our back-end storage mechanism or ORM without affecting the password reminder component of our application. Once again, by violating one of the SOLID principles, we are giving the consuming class too much knowledge about the rest of the application.
To break the dependency, let's create a RemindableInterface. In fact, such an interface exists in Laravel and is implemented by default by the User model:
interface RemindableInterface {
public function getReminderEmail();
}
Once the interface has been created, we can implement it on our model:
class Contact extends Eloquent implements RemindableInterface {
public function getReminderEmail()
{
return $this->email;
}
}
Finally, we can depend on this smaller, more focused interface in the PasswordReminder:
class PasswordReminder {
public function remind(RemindableInterface $remindable, $view)
{
// Şifre hatırlatıcı e-postası gönder...
}
}
By making this simple change, we have removed unnecessary dependencies from the password reminder component and made it flexible enough to use a class from any ORM, as long as that class implements the new RemindableInterface. This is exactly how Laravel's password reminder component stays database and ORM agnostic!
Knowledge Is Power
Once again we have discovered the pitfalls of giving a class too much knowledge about application implementation details. By paying attention to how much knowledge we give a class, we can abide by all of the SOLID principles.