1
0
mirror of https://github.com/jupeter/clean-code-php.git synced 2025-09-25 21:49:04 +02:00

Merge pull request #42 from peter-gribanov/isp

Interface Segregation Principle (ISP)
This commit is contained in:
Tomáš Votruba
2017-09-05 18:36:38 +02:00
committed by GitHub

View File

@@ -1362,6 +1362,7 @@ renderLargeRectangles($shapes);
**[⬆ back to top](#table-of-contents)** **[⬆ back to top](#table-of-contents)**
### Interface Segregation Principle (ISP) ### Interface Segregation Principle (ISP)
ISP states that "Clients should not be forced to depend upon interfaces that ISP states that "Clients should not be forced to depend upon interfaces that
they do not use." they do not use."
@@ -1371,97 +1372,84 @@ huge amounts of options is beneficial, because most of the time they won't need
all of the settings. Making them optional helps prevent having a "fat interface". all of the settings. Making them optional helps prevent having a "fat interface".
**Bad:** **Bad:**
```php ```php
interface WorkerInterface { interface Employee
{
public function work(); public function work();
public function eat(); public function eat();
} }
class Worker implements WorkerInterface { class Human implements Employee
public function work() { {
public function work()
{
// ....working // ....working
} }
public function eat() {
public function eat()
{
// ...... eating in lunch break // ...... eating in lunch break
} }
} }
class SuperWorker implements WorkerInterface { class Robot implements Employee
public function work() { {
public function work()
{
//.... working much more //.... working much more
} }
public function eat() { public function eat()
//.... eating in lunch break {
} //.... robot can't eating, but it must implement this method
}
class Manager {
/** @var WorkerInterface $worker **/
private $worker;
public function setWorker(WorkerInterface $worker) {
$this->worker = $worker;
}
public function manage() {
$this->worker->work();
} }
} }
``` ```
**Good:** **Good:**
```php
interface WorkerInterface extends FeedableInterface, WorkableInterface {
}
interface WorkableInterface { Not every worker is an employee, but every employee is an worker.
```php
interface Workable
{
public function work(); public function work();
} }
interface FeedableInterface { interface Feedable
{
public function eat(); public function eat();
} }
class Worker implements WorkableInterface, FeedableInterface { interface Employee extends Feedable, Workable
public function work() { {
}
class Human implements Employee
{
public function work()
{
// ....working // ....working
} }
public function eat() { public function eat()
{
//.... eating in lunch break //.... eating in lunch break
} }
} }
class Robot implements WorkableInterface { // robot can only work
public function work() { class Robot implements Workable
{
public function work()
{
// ....working // ....working
} }
} }
class SuperWorker implements WorkerInterface {
public function work() {
//.... working much more
}
public function eat() {
//.... eating in lunch break
}
}
class Manager {
/** @var $worker WorkableInterface **/
private $worker;
public function setWorker(WorkableInterface $w) {
$this->worker = $w;
}
public function manage() {
$this->worker->work();
}
}
``` ```
**[⬆ back to top](#table-of-contents)** **[⬆ back to top](#table-of-contents)**
### Dependency Inversion Principle (DIP) ### Dependency Inversion Principle (DIP)