SOLID/4-interface-segregation-principle.php

95 lines
1.3 KiB
PHP
Raw Normal View History

2016-04-20 13:01:31 +00:00
<?php
// Interface Segregation Principle Violation
interface Workable
{
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();
}
}