You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: posts/bridge-design-pattern.md
+121Lines changed: 121 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,3 +16,124 @@ In this article, we’ll take a deep dive into Bridge design pattern, exploring
16
16
- Bridge design pattern
17
17
18
18
# Bridge design pattern
19
+
20
+
The **Bridge** is a structural design pattern that separates an abstraction (interface) from its implementation, allowing them to evolve independently.
21
+
It’s useful for avoiding a large inheritance hierarchy by combining different abstractions and implementations dynamically.
22
+
23
+
### Expl: Messaging System
24
+
25
+
```php
26
+
interface MessageSenderInterface {
27
+
public function send(string $message): void;
28
+
}
29
+
```
30
+
31
+
```php
32
+
<?php
33
+
34
+
class SmsSenderService implements MessageSenderInterface
35
+
{
36
+
public function send(string $message): void
37
+
{
38
+
echo "Sending SMS: $message\n";
39
+
}
40
+
}
41
+
42
+
class EmailSenderService implements MessageSenderInterface
43
+
{
44
+
public function send(string $message): void
45
+
{
46
+
echo "Sending Email: $message\n";
47
+
}
48
+
}
49
+
```
50
+
51
+
```php
52
+
use App\Services\SmsSenderService;
53
+
use App\Services\EmailSenderService;
54
+
55
+
public function register()
56
+
{
57
+
$this->app->singleton(SmsSenderService::class, function ($app) {
58
+
return new SmsSenderService();
59
+
});
60
+
61
+
$this->app->singleton(EmailSenderService::class, function ($app) {
62
+
return new EmailSenderService();
63
+
});
64
+
}
65
+
```
66
+
67
+
```php
68
+
// Message Abstract Class
69
+
abstract class Message
70
+
{
71
+
protected MessageSenderInterface $messageSender;
72
+
73
+
public function __construct(MessageSenderInterface $messageSender)
74
+
{
75
+
$this->messageSender = $messageSender;
76
+
}
77
+
78
+
abstract public function sendMessage(string $message): void;
79
+
}
80
+
81
+
// Urgent Message Concrete Class
82
+
class UrgentMessage extends Message
83
+
{
84
+
public function sendMessage(string $message): void
85
+
{
86
+
echo '[Urgent] ';
87
+
$this->messageSender->send($message);
88
+
}
89
+
}
90
+
91
+
// Normal Message Concrete Class
92
+
class NormalMessage extends Message
93
+
{
94
+
public function sendMessage(string $message): void
95
+
{
96
+
echo '[Normal] ';
97
+
$this->messageSender->send($message);
98
+
}
99
+
}
100
+
```
101
+
102
+
```php
103
+
<?php
104
+
105
+
use App\Services\SmsSenderService;
106
+
use App\Services\EmailSenderService;
107
+
use App\Messages\UrgentMessage;
108
+
use App\Messages\NormalMessage;
109
+
110
+
class MessageController
111
+
{
112
+
protected SmsSenderService $smsSenderService;
113
+
protected EmailSenderService $emailSenderService;
114
+
115
+
public function __construct(SmsSenderService $smsSenderService, EmailSenderService $emailSenderService)
116
+
{
117
+
$this->smsSenderService = $smsSenderService;
118
+
$this->emailSenderService = $emailSenderService;
119
+
}
120
+
121
+
public function sendUrgentSms()
122
+
{
123
+
$urgentSmsMessage = new UrgentMessage($this->smsSenderService);
124
+
$urgentSmsMessage->sendMessage('System is down!');
125
+
}
126
+
127
+
public function sendNormalEmail()
128
+
{
129
+
$normalEmailMessage = new NormalMessage($this->emailSenderService);
Copy file name to clipboardExpand all lines: posts/design-patterns.md
+7Lines changed: 7 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,14 +22,21 @@ In this article, we’ll take a deep dive into design pattern, exploring its typ
22
22
**Creation Design Pattern** focuses on the process of object creation, ensuring that objects are created in a manner suitable to our project requirements. It helps abstract the instantiation logic from the client code.
23
23
24
24
-**Singleton:** Ensures a class has only one instance and provides a global point of access to it.
Copy file name to clipboardExpand all lines: posts/facade-design-pattern.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ In this article, we’ll take a deep dive into Facade design pattern, exploring
20
20
21
21
# Facade design pattern
22
22
23
-
**Facade Design Pattern** provides a simplified, `unified interface` to a complex system or set of subsystems. It hides the complexity of the system by exposing only essential methods, making it easier for clients to interact with the system.
23
+
The **Facade** is a structural design pattern provides a simplified, `unified interface` to a complex system or set of subsystems. It hides the complexity of the system by exposing only essential methods, making it easier for clients to interact with the system.
Copy file name to clipboardExpand all lines: posts/singleton-design-pattern.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ In this article, we’ll take a deep dive into Singleton design pattern, explori
21
21
22
22
# Singleton design pattern
23
23
24
-
The Singleton design pattern ensures that a class has only one instance and provides a global point of access to that instance. It's useful when you need a single, shared resource across the application, such as a configuration manager or a database connection.
24
+
The **Singleton** is a creational design pattern ensures that a class has only one instance and provides a global point of access to that instance. It's useful when you need a single, shared resource across the application, such as a configuration manager or a database connection.
0 commit comments