commit c509e6667059b7e1f1d8789240f82cce153eebfb Author: thangtd90 Date: Wed Apr 20 20:01:31 2016 +0700 Init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..090a1f0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +.DS_Store diff --git a/1-single-responsibility-principle.php b/1-single-responsibility-principle.php new file mode 100644 index 0000000..32100f2 --- /dev/null +++ b/1-single-responsibility-principle.php @@ -0,0 +1,63 @@ + $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()); + } +} diff --git a/2-open-closed-principle.php b/2-open-closed-principle.php new file mode 100644 index 0000000..ae558b9 --- /dev/null +++ b/2-open-closed-principle.php @@ -0,0 +1,62 @@ +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(); + } +} diff --git a/3-liskov-substitution-principle.php b/3-liskov-substitution-principle.php new file mode 100644 index 0000000..640666d --- /dev/null +++ b/3-liskov-substitution-principle.php @@ -0,0 +1,66 @@ +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 + } +} diff --git a/4-interface-segregation-principle.php b/4-interface-segregation-principle.php new file mode 100644 index 0000000..a1f3c66 --- /dev/null +++ b/4-interface-segregation-principle.php @@ -0,0 +1,94 @@ +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(); + } +} diff --git a/5-dependency-inversion-principle.php b/5-dependency-inversion-principle.php new file mode 100644 index 0000000..2ea7db2 --- /dev/null +++ b/5-dependency-inversion-principle.php @@ -0,0 +1,48 @@ +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; + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..58f1c7c --- /dev/null +++ b/readme.md @@ -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)