Skip to content

Commit

Permalink
Implement example of Singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielanhaia committed Nov 19, 2021
1 parent 0101672 commit c1f3027
Showing 4 changed files with 55 additions and 4 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ COLOR_TITLE = \033[0;31m

help:
@echo " ${COLOR_TITLE}> GENERAL COMMANDS:${COLOR_RESET}"
@echo " ${COLOR_COMMENT}# make${COLOR_RESET} ${COLOR_INFO}build${COLOR_RESET}"
@echo " ${COLOR_COMMENT}# make${COLOR_RESET} ${COLOR_INFO}build${COLOR_RESET} (Docker command)"
@echo " ${COLOR_TITLE}> LIST OF DESIGN PATTERNS AVAILABLE:${COLOR_RESET}"
@echo " ${COLOR_COMMENT}# make${COLOR_RESET} ${COLOR_INFO}abstract-factory${COLOR_RESET}"
@echo " ${COLOR_COMMENT}# make${COLOR_RESET} ${COLOR_INFO}adapter${COLOR_RESET}"
@@ -19,10 +19,11 @@ help:
@echo " ${COLOR_COMMENT}# make${COLOR_RESET} ${COLOR_INFO}null-object${COLOR_RESET}"
@echo " ${COLOR_COMMENT}# make${COLOR_RESET} ${COLOR_INFO}observer${COLOR_RESET}"
@echo " ${COLOR_COMMENT}# make${COLOR_RESET} ${COLOR_INFO}proxy${COLOR_RESET}"
@echo " ${COLOR_COMMENT}# make${COLOR_RESET} ${COLOR_INFO}singleton${COLOR_RESET}"
@echo " ${COLOR_COMMENT}# make${COLOR_RESET} ${COLOR_INFO}strategy${COLOR_RESET}"
@echo " ${COLOR_COMMENT}# make${COLOR_RESET} ${COLOR_INFO}template-method${COLOR_RESET}"

build project (Docker):
build:
docker build -t design-patterns .

strategy:
@@ -59,4 +60,7 @@ abstract-factory:
docker run -it design-patterns php /app/example/abstract_factory.php

proxy:
docker run -it design-patterns php /app/example/proxy.php
docker run -it design-patterns php /app/example/proxy.php

singleton:
docker run -it design-patterns php /app/example/singleton.php
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ problems. Once you learn the design pattern and its concept, it will be effortle
| Null Object |[HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/NullObject) | Soon |
| Observer |[HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Observer) | Soon |
| Proxy |[HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Proxy) | Soon |
| Singleton |[HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Singleton) | Soon |

## Coming soon

@@ -41,7 +42,6 @@ problems. Once you learn the design pattern and its concept, it will be effortle
- Memento
- Object Pool
- Prototype
- Singleton
- State
- Visitor

17 changes: 17 additions & 0 deletions example/singleton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use App\{Singleton};

require_once 'autoloader.php';

$dbConnectionInstance1 = Singleton\DBConnectionSingleton::getConnection();
$dbConnectionInstance2 = Singleton\DBConnectionSingleton::getConnection();

echo $dbConnectionInstance1->doSomething();
ln();
echo $dbConnectionInstance2->doSomething();
ln();

if (spl_object_id($dbConnectionInstance1) === spl_object_id($dbConnectionInstance2)) {
dump('Object "$dbConnectionInstance1" and "$dbConnectionInstance2" are exactly the same in memory.');
}
30 changes: 30 additions & 0 deletions src/Singleton/DBConnectionSingleton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Singleton;

class DBConnectionSingleton
{
private static ?DBConnectionSingleton $instance = null;

private function __construct()
{
}

private function __clone(): void
{
}

public static function getConnection(): self
{
if (self::$instance === null) {
self::$instance = new self();
}

return self::$instance;
}

public function doSomething(): string
{
return 'It worked!';
}
}

0 comments on commit c1f3027

Please sign in to comment.