Skip to content

Commit 4c3aff6

Browse files
committed
observer design pattern
1 parent 099b015 commit 4c3aff6

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use App\Observable\User;
6+
use App\Observers\UserObserver;
7+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8+
use Symfony\Component\HttpFoundation\Response;
9+
use Symfony\Component\Routing\Annotation\Route;
10+
11+
class ObserverDesignPattern extends AbstractController
12+
{
13+
14+
#[Route(path: "/observerDesignPattern", name: 'observerDesignPattern', methods: ['GET'])]
15+
public function welcomeUser(): Response
16+
{
17+
$newUser = new User();
18+
19+
$newUserObserver = new UserObserver();
20+
$newUser->attach($newUserObserver);
21+
22+
$newUser->setName('rahul');
23+
24+
$newUser->detach($newUserObserver); // It won't notify further updates..
25+
26+
27+
return new Response("Welcome {$newUser->getName()}!, your user id is {$newUser->getUserId()}");
28+
}
29+
}

src/Observable/User.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace App\Observable;
4+
5+
use SplObserver;
6+
7+
class User implements \SplSubject
8+
{
9+
10+
private array $observers = [];
11+
private string $name;
12+
private string $userId;
13+
public function __construct()
14+
{
15+
}
16+
17+
public function attach(SplObserver $observer): void
18+
{
19+
$observerId = spl_object_hash($observer);
20+
$this->observers[$observerId] = $observer;
21+
}
22+
23+
public function detach(SplObserver $observer): void
24+
{
25+
$observerId = spl_object_hash($observer);
26+
unset($this->observers[$observerId]);
27+
}
28+
29+
public function notify(): void
30+
{
31+
foreach ($this->observers as $observer) {
32+
$observer->update($this);
33+
}
34+
}
35+
36+
public function setName($name) {
37+
$this->name = $name;
38+
$this->notify();
39+
}
40+
41+
public function getName():string
42+
{
43+
return $this->name;
44+
}
45+
46+
/**
47+
* @return string
48+
*/
49+
public function getUserId(): string
50+
{
51+
return $this->userId;
52+
}
53+
54+
/**
55+
* @param string $userId
56+
*/
57+
public function setUserId(string $userId): void
58+
{
59+
$this->userId = $userId;
60+
}
61+
62+
}

src/Observers/UserObserver.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Observers;
4+
5+
use App\Observable\User;
6+
use SplSubject;
7+
8+
class UserObserver implements \SplObserver
9+
{
10+
11+
public function update(SplSubject $subject): void
12+
{
13+
if (!$subject instanceof User) {
14+
return;
15+
}
16+
17+
$userId = $this->generateRandomUserID();
18+
19+
$subject->setUserId($userId);
20+
}
21+
22+
private function generateRandomUserID(int $length = 8): string
23+
{
24+
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
25+
$userID = '';
26+
$charCount = strlen($characters);
27+
28+
for ($i = 0; $i < $length; $i++) {
29+
$randomChar = $characters[random_int(0, $charCount - 1)];
30+
$userID .= $randomChar;
31+
}
32+
33+
return $userID;
34+
}
35+
}

0 commit comments

Comments
 (0)