01/09/2017 - PHP
An interface should never become a general purpose interface. A class specific interface is always better than one general purpose interface.
# VIOLATION
interface PlayerInterface
{
public function run();
public function jump();
public function rebound();
}
class Basketball implements PlayerInterface
{
public function run() {}
public function jump() {}
public function rebound() {}
}
class Football implements PlayerInterface
{
public function run() {}
public function jump() {}
public function rebound() {}
}
As you can see above, Football
class is forced to implement rebound
method which has nothing to do with it. This is a violation.
# REFACTORED
interface PlayerInterface
{
public function run();
public function jump();
}
interface BasketballPlayerInterface
{
public function rebound();
}
class Basketball implements PlayerInterface, BasketballPlayerInterface
{
public function run() {}
public function jump() {}
public function rebound() {}
}
class Football implements PlayerInterface
{
public function run() {}
public function jump() {}
}
As you can see above, we have moved rebound
method into BasketballPlayerInterface
interface and let only Basketball
class implement it. Our Football
class is not forced to implement irrelevant methods anymore.