Skip to content

Commit e78bedc

Browse files
Merge pull request #40 from multividas/develop
Develop to main
2 parents 38192de + b2452bf commit e78bedc

5 files changed

Lines changed: 224 additions & 2 deletions

File tree

posts/adapter-design-pattern.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,97 @@ In this article, we’ll take a deep dive into Adapter design pattern, exploring
1616
- Adapter design pattern
1717

1818
# Adapter design pattern
19+
20+
The **Adapter** is a structural design pattern that allows incompatible interfaces to work together.
21+
It acts as a bridge between *two objects* by converting the *interface of one class into an interface* expected by the client.
22+
23+
### Expl: Logging Adapter
24+
25+
```php
26+
<?php
27+
28+
class LegacyFileLogger
29+
{
30+
public function saveLogToFile(string $message): void
31+
{
32+
echo "Log saved to file: $message\n";
33+
}
34+
}
35+
```
36+
37+
```php
38+
<?php
39+
40+
interface LoggerInterface
41+
{
42+
public function log(string $message): void;
43+
}
44+
```
45+
46+
```php
47+
<?php
48+
49+
use App\Legacy\LegacyFileLogger;
50+
use App\Contracts\LoggerInterface;
51+
52+
class FileLoggerAdapter implements LoggerInterface
53+
{
54+
private LegacyFileLogger $legacyLogger;
55+
56+
public function __construct(LegacyFileLogger $legacyLogger)
57+
{
58+
$this->legacyLogger = $legacyLogger;
59+
}
60+
61+
public function log(string $message): void
62+
{
63+
$this->legacyLogger->saveLogToFile($message);
64+
}
65+
}
66+
```
67+
68+
```php
69+
<?php
70+
71+
use App\Legacy\LegacyFileLogger;
72+
use App\Contracts\LoggerInterface;
73+
use App\Adapters\FileLoggerAdapter;
74+
75+
class LoggerServiceProvider
76+
{
77+
public function register()
78+
{
79+
$this->app->bind(LoggerInterface::class, function ($app) {
80+
$legacyLogger = new LegacyFileLogger();
81+
return new FileLoggerAdapter($legacyLogger);
82+
});
83+
}
84+
}
85+
```
86+
87+
```php
88+
<?php
89+
90+
class LogController
91+
{
92+
private LoggerInterface $logger;
93+
94+
public function __construct(LoggerInterface $logger)
95+
{
96+
$this->logger = $logger;
97+
}
98+
99+
public function logAction()
100+
{
101+
// Logging an action using the adapted logger
102+
$this->logger->log('An important action has been executed.');
103+
}
104+
}
105+
```
106+
107+
### Key features:
108+
109+
- **Integrates legacy systems**: Use old code without rewriting it.
110+
- **Flexibility**: You can switch to another logging system (e.g., cloud logs) by creating a new adapter.
111+
- **Maintainability**: Decouples your application from specific implementations, following the Dependency Inversion Principle.
112+

posts/bridge-design-pattern.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,124 @@ In this article, we’ll take a deep dive into Bridge design pattern, exploring
1616
- Bridge design pattern
1717

1818
# 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);
130+
$normalEmailMessage->sendMessage('Monthly report available.');
131+
}
132+
}
133+
```
134+
135+
### Key features:
136+
137+
- **Decouples abstraction from implementation** (e.g., message type vs. sender).
138+
- **Extensible**: Add new message types or platforms independently.
139+
- **Usage**: Easily manage combinations in messaging, payment gateways, or rendering systems.

posts/design-patterns.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,21 @@ In this article, we’ll take a deep dive into design pattern, exploring its typ
2222
**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.
2323

2424
- **Singleton:** Ensures a class has only one instance and provides a global point of access to it.
25+
[singleton-design-pattern](https://engineering.multividas.com/posts/singleton-design-pattern)
2526

2627
# Structural design pattern
2728

2829
**Structural Design Patterns** focus on how objects and classes are arranged or composed.
2930

3031
- **Adapter:** Converts one interface into another expected by clients, enabling incompatible interfaces to work together.
32+
[adapter-design-pattern](https://engineering.multividas.com/posts/adapter-design-pattern)
33+
3134
- **Bridge:** Separates an abstraction from its implementation, allowing both to vary independently.
35+
[bridge-design-pattern](https://engineering.multividas.com/posts/bridge-design-pattern)
36+
3237
- **Facade:** Provides a unified interface to a set of interfaces in a subsystem, making it easier to use and interact with.
38+
[facade-design-pattern](https://engineering.multividas.com/posts/facade-design-pattern)
39+
3340

3441
# Behavioral design pattern
3542

posts/facade-design-pattern.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ In this article, we’ll take a deep dive into Facade design pattern, exploring
2020

2121
# Facade design pattern
2222

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.
2424

2525
Key features:
2626

posts/singleton-design-pattern.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ In this article, we’ll take a deep dive into Singleton design pattern, explori
2121

2222
# Singleton design pattern
2323

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.
2525

2626
<img src="https://refactoring.guru/images/patterns/diagrams/singleton/structure-en.png" alt="singleton-design-pattern" />
2727

0 commit comments

Comments
 (0)