Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply SOLID principle at Door code example #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -97,28 +97,48 @@ 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);
}
}
```
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?**
Expand Down