From e1a507ce14f9794bd02a5b309cc7f64d2b196219 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Sun, 22 Nov 2020 23:44:49 +0400 Subject: [PATCH 1/9] Add funding info --- .github/FUNDING.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 00000000..cc5c56e7 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,3 @@ +# These are supported funding model platforms + +github: [kamranahmedse] From 77076db6ceafa11984c54768a032308016def7bf Mon Sep 17 00:00:00 2001 From: Amir Zpr <45946337+amirzpr@users.noreply.github.com> Date: Sat, 27 Aug 2022 14:50:27 +0430 Subject: [PATCH 2/9] Proxy class has not implemented interface (#134) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1ccdc835..a724328d 100644 --- a/README.md +++ b/README.md @@ -1226,7 +1226,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; From e0de93e60a9ca55e309f28942b6214128a08108b Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Thu, 27 Apr 2023 11:28:47 +0100 Subject: [PATCH 3/9] Add banner --- .github/banner.svg | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .github/banner.svg diff --git a/.github/banner.svg b/.github/banner.svg new file mode 100644 index 00000000..8deae71a --- /dev/null +++ b/.github/banner.svg @@ -0,0 +1,5 @@ + + + + + From 739322eb8cf166cf9b7fd0cec7bb5df20766b8fa Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Thu, 27 Apr 2023 11:59:11 +0100 Subject: [PATCH 4/9] Fix broken banner --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a724328d..94c7cf97 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,8 @@ -![Design Patterns For Humans](https://cloud.githubusercontent.com/assets/11269635/23065273/1b7e5938-f515-11e6-8dd3-d0d58de6bb9a.png) +
+

+ +

+
*** From 65805a0802f46720ef21308e7556bedbb228b701 Mon Sep 17 00:00:00 2001 From: Lukas Schmid Date: Thu, 27 Apr 2023 13:00:12 +0200 Subject: [PATCH 5/9] Added runtime decision to sorter (#137) --- README.md | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 94c7cf97..f39e9ae6 100644 --- a/README.md +++ b/README.md @@ -2052,28 +2052,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 From 8d7b9edd433c4094edcf4482211743f8b122201e Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Thu, 27 Apr 2023 12:06:53 +0100 Subject: [PATCH 6/9] Update state pattern implementation --- README.md => readme.md | 107 ++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 55 deletions(-) rename README.md => readme.md (97%) diff --git a/README.md b/readme.md similarity index 97% rename from README.md rename to readme.md index f39e9ae6..66f63628 100644 --- a/README.md +++ b/readme.md @@ -2097,84 +2097,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()); -$editor->type('Second line'); -$editor->type('Third line'); +And then it can be used as follows and it will call the relevant state methods: -$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 From 5e44e6a7ce47e44c50fa4d6fb3bf6c5b1a85129b Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Thu, 27 Apr 2023 12:32:39 +0100 Subject: [PATCH 7/9] Update header --- readme.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index 66f63628..4153f965 100644 --- a/readme.md +++ b/readme.md @@ -1,8 +1,7 @@

- +

-
*** @@ -10,12 +9,12 @@ 🎉 Ultra-simplified explanation to design patterns! 🎉

-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/kamranahmedse). Introduction ================= From ba92c416a750ed9a287985d62c57cd34fefca0cc Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Wed, 17 May 2023 01:34:18 +0100 Subject: [PATCH 8/9] Add table of contents --- readme.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/readme.md b/readme.md index 4153f965..3ee4f4d4 100644 --- a/readme.md +++ b/readme.md @@ -16,6 +16,23 @@ A topic that can easily make anyone's mind wobble. Here I try to make them stick Check out my [other project](http://roadmap.sh) and say "hi" on [Twitter](https://twitter.com/kamranahmedse). +
+ +|[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 ================= From 7023f30d183b6502fe8945d705f2eafdd07dcf4a Mon Sep 17 00:00:00 2001 From: fellalli Date: Mon, 2 Dec 2024 05:54:47 +0100 Subject: [PATCH 9/9] Fix link in readme.md (#143) * Fix link in readme.md * Fix link in readme.md --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 3ee4f4d4..bacfcaa2 100644 --- a/readme.md +++ b/readme.md @@ -14,7 +14,7 @@ A topic that can easily make anyone's mind wobble. Here I try to make them stick *** -Check out my [other project](http://roadmap.sh) 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).
@@ -2313,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