-A topic that can easily make anyone's mind wobble. Here I try to make them stick in to your mind (and maybe mine) by explaining them in the simplest way possible.
+A topic that can easily make anyone's mind wobble. Here I try to make them stick in to your mind (and maybe mine) by explaining them in the simplest way possible.
***
-Check out my [blog](http://kamranahmed.info) and say "hi" on [Twitter](https://twitter.com/kamranahmedse).
+Check out my [other project](http://roadmap.sh) and say "hi" on [Twitter](https://twitter.com/kamrify).
+
+
+
+|[Creational Design Patterns](#creational-design-patterns)|[Structural Design Patterns](#structural-design-patterns)|[Behavioral Design Patterns](#behavioral-design-patterns)|
+|:-|:-|:-|
+|[Simple Factory](#-simple-factory)|[Adapter](#-adapter)|[Chain of Responsibility](#-chain-of-responsibility)|
+|[Factory Method](#-factory-method)|[Bridge](#-bridge)|[Command](#-command)|
+|[Abstract Factory](#-abstract-factory)|[Composite](#-composite)|[Iterator](#-iterator)|
+|[Builder](#-builder)|[Decorator](#-decorator)|[Mediator](#-mediator)|
+|[Prototype](#-prototype)|[Facade](#-facade)|[Memento](#-memento)|
+|[Singleton](#-singleton)|[Flyweight](#-flyweight)|[Observer](#-observer)|
+||[Proxy](#-proxy)|[Visitor](#-visitor)|
+|||[Strategy](#-strategy)|
+|||[State](#-state)|
+|||[Template Method](#-template-method)|
+
+
Introduction
=================
@@ -1226,7 +1246,7 @@ class LabDoor implements Door
```
Then we have a proxy to secure any doors that we want
```php
-class SecuredDoor
+class SecuredDoor implements Door
{
protected $door;
@@ -2048,28 +2068,35 @@ And then we have our client that is going to use any strategy
```php
class Sorter
{
- protected $sorter;
+ protected $sorterSmall;
+ protected $sorterBig;
- public function __construct(SortStrategy $sorter)
+ public function __construct(SortStrategy $sorterSmall, SortStrategy $sorterBig)
{
- $this->sorter = $sorter;
+ $this->sorterSmall = $sorterSmall;
+ $this->sorterBig = $sorterBig;
}
public function sort(array $dataset): array
{
- return $this->sorter->sort($dataset);
+ if (count($dataset) > 5) {
+ return $this->sorterBig->sort($dataset);
+ } else {
+ return $this->sorterSmall->sort($dataset);
+ }
}
}
```
And it can be used as
```php
-$dataset = [1, 5, 4, 3, 2, 8];
+$smalldataset = [1, 3, 4, 2];
+$bigdataset = [1, 4, 3, 2, 8, 10, 5, 6, 9, 7];
+
+$sorter = new Sorter(new BubbleSortStrategy(), new QuickSortStrategy());
-$sorter = new Sorter(new BubbleSortStrategy());
$sorter->sort($dataset); // Output : Sorting using bubble sort
-$sorter = new Sorter(new QuickSortStrategy());
-$sorter->sort($dataset); // Output : Sorting using quick sort
+$sorter->sort($bigdataset); // Output : Sorting using quick sort
```
💢 State
@@ -2086,84 +2113,81 @@ Wikipedia says
**Programmatic example**
-Let's take an example of text editor, it lets you change the state of text that is typed i.e. if you have selected bold, it starts writing in bold, if italic then in italics etc.
-
-First of all we have our state interface and some state implementations
+Let's take an example of a phone. First of all we have our state interface and some state implementations
```php
-interface WritingState
-{
- public function write(string $words);
+interface PhoneState {
+ public function pickUp(): PhoneState;
+ public function hangUp(): PhoneState;
+ public function dial(): PhoneState;
}
-class UpperCase implements WritingState
-{
- public function write(string $words)
- {
- echo strtoupper($words);
+// states implementation
+class PhoneStateIdle implements PhoneState {
+ public function pickUp(): PhoneState {
+ return new PhoneStatePickedUp();
+ }
+ public function hangUp(): PhoneState {
+ throw new Exception("already idle");
+ }
+ public function dial(): PhoneState {
+ throw new Exception("unable to dial in idle state");
}
}
-class LowerCase implements WritingState
-{
- public function write(string $words)
- {
- echo strtolower($words);
+class PhoneStatePickedUp implements PhoneState {
+ public function pickUp(): PhoneState {
+ throw new Exception("already picked up");
+ }
+ public function hangUp(): PhoneState {
+ return new PhoneStateIdle();
+ }
+ public function dial(): PhoneState {
+ return new PhoneStateCalling();
}
}
-class DefaultText implements WritingState
-{
- public function write(string $words)
- {
- echo $words;
+class PhoneStateCalling implements PhoneState {
+ public function pickUp(): PhoneState {
+ throw new Exception("already picked up");
+ }
+ public function hangUp(): PhoneState {
+ return new PhoneStateIdle();
+ }
+ public function dial(): PhoneState {
+ throw new Exception("already dialing");
}
}
```
-Then we have our editor
+
+Then we have our Phone class that changes the state on different behavior calls
+
```php
-class TextEditor
-{
- protected $state;
+class Phone {
+ private $state;
- public function __construct(WritingState $state)
- {
- $this->state = $state;
+ public function __construct() {
+ $this->state = new PhoneStateIdle();
}
-
- public function setState(WritingState $state)
- {
- $this->state = $state;
+ public function pickUp() {
+ $this->state = $this->state->pickUp();
}
-
- public function type(string $words)
- {
- $this->state->write($words);
+ public function hangUp() {
+ $this->state = $this->state->hangUp();
+ }
+ public function dial() {
+ $this->state = $this->state->dial();
}
}
```
-And then it can be used as
-```php
-$editor = new TextEditor(new DefaultText());
-
-$editor->type('First line');
-$editor->setState(new UpperCase());
+And then it can be used as follows and it will call the relevant state methods:
-$editor->type('Second line');
-$editor->type('Third line');
-
-$editor->setState(new LowerCase());
-
-$editor->type('Fourth line');
-$editor->type('Fifth line');
+```php
+$phone = new Phone();
-// Output:
-// First line
-// SECOND LINE
-// THIRD LINE
-// fourth line
-// fifth line
+$phone->pickUp();
+$phone->dial();
```
📒 Template Method
@@ -2289,7 +2313,7 @@ And that about wraps it up. I will continue to improve this, so you might want t
- Report issues
- Open pull request with improvements
- Spread the word
-- Reach out with any feedback [![Twitter URL](https://img.shields.io/twitter/url/https/twitter.com/kamranahmedse.svg?style=social&label=Follow%20%40kamranahmedse)](https://twitter.com/kamranahmedse)
+- Reach out with any feedback [![Twitter URL](https://img.shields.io/twitter/url/https/twitter.com/kamrify.svg?style=social&label=Follow%20%40kamrify)](https://twitter.com/kamrify)
## License