This commit is contained in:
thangtd90 2016-04-20 20:01:31 +07:00
commit c509e66670
7 changed files with 353 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea
.DS_Store

View File

@ -0,0 +1,63 @@
<?php
// Single Responsibility Principle Violation
class Report
{
public function getTitle()
{
return 'Report Title';
}
public function getDate()
{
return '2016-04-21';
}
public function getContents()
{
return [
'title' => $this->getTitle(),
'date' => $this->getDate(),
];
}
public function formatJson()
{
return json_encode($this->getContents());
}
}
// Refactored
class Report
{
public function getTitle()
{
return 'Report Title';
}
public function getDate()
{
return '2016-04-21';
}
public function getContents()
{
return [
'title' => $this->getTitle(),
'date' => $this->getDate(),
];
}
}
interface ReportFormattable
{
public function format(Report $report);
}
class JsonReportFormatter implements ReportFormattable
{
public function format(Report $report)
{
return json_encode($report->getContents());
}
}

View File

@ -0,0 +1,62 @@
<?php
// Open Closed Principle Violation
class Programmer
{
public function code()
{
return 'coding';
}
}
class Tester
{
public function test()
{
return 'testing';
}
}
class ProjectManagement
{
public function process($member)
{
if ($member instanceof Programmer) {
$member->code();
} elseif ($member instanceof Tester) {
$member->test();
};
throw new Exception('Invalid input member');
}
}
// Refactored
interface Workable
{
public function work();
}
class Programmer implements Workable
{
public function work()
{
return 'coding';
}
}
class Tester implements Workable
{
public function work()
{
return 'testing';
}
}
class ProjectManagement
{
public function process(Workable $member)
{
return $member->work();
}
}

View File

@ -0,0 +1,66 @@
<?php
// Liskov Substitution Principle Violation
// The Rectangle - Square problem
class Rectangle
{
protected $width;
protected $height;
public function setHeight($height)
{
$this->height = $height;
}
public function getHeight()
{
return $this->height;
}
public function setWidth($width)
{
$this->width = $width;
}
public function getWidth()
{
return $this->width;
}
public function area()
{
return $this->height * $this->width;
}
}
class Square extends Rectangle
{
public function setHeight($value)
{
$this->width = $value;
$this->height = $value;
}
public function setWidth($value)
{
$this->width = $value;
$this->height = $value;
}
}
class RectangleTest
{
private $rectangle;
public function __construct(Rectangle $rectangle)
{
$this->rectangle = $rectangle;
}
public function testArea()
{
$this->rectangle->setHeight(2);
$this->rectangle->setWidth(3);
// Expect rectangle's area to be 6
}
}

View File

@ -0,0 +1,94 @@
<?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';
}
}
class Tester implements Workable
{
public function test()
{
return 'testing in test server';
}
}
class ProjectManagement
{
public function processCode(Codeable $member)
{
$member->code();
}
}

View File

@ -0,0 +1,48 @@
<?php
// Dependency Inversion Principle Violation
class Mailer
{
}
class SendWelcomeMessage
{
private $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
}
// Refactored
interface Mailer
{
public function send();
}
class SmtpMailer implements Mailer
{
public function send()
{
}
}
class SendGridMailer implements Mailer
{
public function send()
{
}
}
class SendWelcomeMessage
{
private $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
}

18
readme.md Normal file
View File

@ -0,0 +1,18 @@
## SOLID Principles Examples in PHP
### Single Responsibility Principle
A class should have one, and only one, reason to change. [Example](./1-single-responsibility-principle.php)
### Open Closed Principle
A class should be open for extension, but closed for modification. [Example](./2-open-closed-principle.php)
### Liskov Substitution Principle
Derived classes must be substitutable for their base classes. [Example](./3-liskov-substitution-principle.php)
### Interface Segregation Principle
Many client-specific interfaces are better than one general-purpose interface. [Example](./4-interface-segregation-principle.php)
### Dependency Inversion Principle
Depend upon abstractions. Do not depend upon concretions. [Example](./5-dependency-inversion-principle.php)
#### For more information about *Object Oriented Design Principles*, you can refer [this slide](https://viblo.asia/thangtd90/posts/pVYRPJPmG4ng)