SOLID/4-interface-segregation-principle.php

96 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2016-04-20 13:01:31 +00:00
<?php
// Interface Segregation Principle Violation
interface Workable
{
2018-01-19 14:03:24 +00:00
public function canCode();
2016-04-20 13:01:31 +00:00
public function code();
public function test();
}
class Programmer implements Workable
{
public function canCode()
{
return true;
}
public function code()
{
return 'coding';
}
public function test()
{
return 'testing in localhost';
}
}
class Tester implements Workable
{
public function canCode()
{
return false;
}
public function code()
{
throw new Exception('Opps! I can not code');
}
public function test()
{
return 'testing in test server';
}
}
class ProjectManagement
{
public function processCode(Workable $member)
{
if ($member->canCode()) {
$member->code();
}
}
}
// Refactored
interface Codeable
{
public function code();
}
interface Testable
{
public function test();
}
class Programmer implements Codeable, Testable
{
public function code()
{
return 'coding';
}
public function test()
{
return 'testing in localhost';
}
}
2016-09-22 10:05:02 +00:00
class Tester implements Testable
2016-04-20 13:01:31 +00:00
{
public function test()
{
return 'testing in test server';
}
}
class ProjectManagement
{
public function processCode(Codeable $member)
{
$member->code();
}
}