From 0ffbd7388e25f8009e4ca379a828284518193694 Mon Sep 17 00:00:00 2001 From: Antonio Spinelli Date: Fri, 11 Mar 2022 10:43:17 -0300 Subject: [PATCH] Apply SOLID principle at Door code example Many examples on the internet of software engineering didn't apply the SOLID principle correctly. Considering the Doors could be made in many kinds of materials, it sounds more like an Abstract class. The changes there introduced will be explained over the SOLID principle: * A Measurable Interface is a behavior expected for all objects which have dimensions, fitting to the Interface Segregation Principle. * Abstract Door has dimensions and requires a material to be built. It is a requirement for the Single Responsibility Principle. * Wooden Door is the concrete implementation over Door abstraction where defines its material and dimensions. This is an example of the Open/Close Principle. --- README.md | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 1ccdc835..9b8646d3 100644 --- a/README.md +++ b/README.md @@ -69,20 +69,20 @@ Wikipedia says **Programmatic Example** -First of all we have a door interface and the implementation +First of all we have a Measurable interface, the Door as abstraction and WoodenDoor as concret implementation ```php -interface Door -{ + +interface Measurable { public function getWidth(): float; public function getHeight(): float; } -class WoodenDoor implements Door -{ +abstract class Door implements Measurable { protected $width; protected $height; + protected $material; - public function __construct(float $width, float $height) + public function __construct(float $width, float $height, string $material) { $this->width = $width; $this->height = $height; @@ -97,13 +97,34 @@ class WoodenDoor implements Door { return $this->height; } + + public function getMeterial(): string + { + return $this->material; + } + + public function __toString(): string + { + return "{$this->material} ({$this->width} x {$this->height})" + } + +} + +class WoodenDoor extends Door +{ + public function __construct(float $width, float $height) + { + $this->width = $width; + $this->height = $height; + $this->material = "WOOD"; + } } ``` -Then we have our door factory that makes the door and returns it +Then we have the door factory that instanciates the door and returns it ```php class DoorFactory { - public static function makeDoor($width, $height): Door + public static function makeWoodenDoor($width, $height): Door { return new WoodenDoor($width, $height); } @@ -111,14 +132,13 @@ class DoorFactory ``` And then it can be used as ```php -// Make me a door of 100x200 -$door = DoorFactory::makeDoor(100, 200); +// It instanciates a Wooden door of 100x200 +$woodenDoor = DoorFactory::makeWoodenDoor(100, 200); -echo 'Width: ' . $door->getWidth(); -echo 'Height: ' . $door->getHeight(); +echo "The Door is {$woodenDoor}"; -// Make me a door of 50x100 -$door2 = DoorFactory::makeDoor(50, 100); +// It instanciates a new Wooden door of 100x20050x100 +$woodenDoor2 = DoorFactory::makeWoodenDoor(50, 100); ``` **When to Use?**