Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions posts/adapter-design-pattern.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,97 @@ In this article, we’ll take a deep dive into Adapter design pattern, exploring
- Adapter design pattern

# Adapter design pattern

The **Adapter** is a structural design pattern that allows incompatible interfaces to work together.
It acts as a bridge between *two objects* by converting the *interface of one class into an interface* expected by the client.

### Expl: Logging Adapter

```php
<?php

class LegacyFileLogger
{
public function saveLogToFile(string $message): void
{
echo "Log saved to file: $message\n";
}
}
```

```php
<?php

interface LoggerInterface
{
public function log(string $message): void;
}
```

```php
<?php

use App\Legacy\LegacyFileLogger;
use App\Contracts\LoggerInterface;

class FileLoggerAdapter implements LoggerInterface
{
private LegacyFileLogger $legacyLogger;

public function __construct(LegacyFileLogger $legacyLogger)
{
$this->legacyLogger = $legacyLogger;
}

public function log(string $message): void
{
$this->legacyLogger->saveLogToFile($message);
}
}
```

```php
<?php

use App\Legacy\LegacyFileLogger;
use App\Contracts\LoggerInterface;
use App\Adapters\FileLoggerAdapter;

class LoggerServiceProvider
{
public function register()
{
$this->app->bind(LoggerInterface::class, function ($app) {
$legacyLogger = new LegacyFileLogger();
return new FileLoggerAdapter($legacyLogger);
});
}
}
```

```php
<?php

class LogController
{
private LoggerInterface $logger;

public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}

public function logAction()
{
// Logging an action using the adapted logger
$this->logger->log('An important action has been executed.');
}
}
```

### Key features:

- **Integrates legacy systems**: Use old code without rewriting it.
- **Flexibility**: You can switch to another logging system (e.g., cloud logs) by creating a new adapter.
- **Maintainability**: Decouples your application from specific implementations, following the Dependency Inversion Principle.

121 changes: 121 additions & 0 deletions posts/bridge-design-pattern.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,124 @@ In this article, we’ll take a deep dive into Bridge design pattern, exploring
- Bridge design pattern

# Bridge design pattern

The **Bridge** is a structural design pattern that separates an abstraction (interface) from its implementation, allowing them to evolve independently.
It’s useful for avoiding a large inheritance hierarchy by combining different abstractions and implementations dynamically.

### Expl: Messaging System

```php
interface MessageSenderInterface {
public function send(string $message): void;
}
```

```php
<?php

class SmsSenderService implements MessageSenderInterface
{
public function send(string $message): void
{
echo "Sending SMS: $message\n";
}
}

class EmailSenderService implements MessageSenderInterface
{
public function send(string $message): void
{
echo "Sending Email: $message\n";
}
}
```

```php
use App\Services\SmsSenderService;
use App\Services\EmailSenderService;

public function register()
{
$this->app->singleton(SmsSenderService::class, function ($app) {
return new SmsSenderService();
});

$this->app->singleton(EmailSenderService::class, function ($app) {
return new EmailSenderService();
});
}
```

```php
// Message Abstract Class
abstract class Message
{
protected MessageSenderInterface $messageSender;

public function __construct(MessageSenderInterface $messageSender)
{
$this->messageSender = $messageSender;
}

abstract public function sendMessage(string $message): void;
}

// Urgent Message Concrete Class
class UrgentMessage extends Message
{
public function sendMessage(string $message): void
{
echo '[Urgent] ';
$this->messageSender->send($message);
}
}

// Normal Message Concrete Class
class NormalMessage extends Message
{
public function sendMessage(string $message): void
{
echo '[Normal] ';
$this->messageSender->send($message);
}
}
```

```php
<?php

use App\Services\SmsSenderService;
use App\Services\EmailSenderService;
use App\Messages\UrgentMessage;
use App\Messages\NormalMessage;

class MessageController
{
protected SmsSenderService $smsSenderService;
protected EmailSenderService $emailSenderService;

public function __construct(SmsSenderService $smsSenderService, EmailSenderService $emailSenderService)
{
$this->smsSenderService = $smsSenderService;
$this->emailSenderService = $emailSenderService;
}

public function sendUrgentSms()
{
$urgentSmsMessage = new UrgentMessage($this->smsSenderService);
$urgentSmsMessage->sendMessage('System is down!');
}

public function sendNormalEmail()
{
$normalEmailMessage = new NormalMessage($this->emailSenderService);
$normalEmailMessage->sendMessage('Monthly report available.');
}
}
```

### Key features:

- **Decouples abstraction from implementation** (e.g., message type vs. sender).
- **Extensible**: Add new message types or platforms independently.
- **Usage**: Easily manage combinations in messaging, payment gateways, or rendering systems.
7 changes: 7 additions & 0 deletions posts/design-patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@ In this article, we’ll take a deep dive into design pattern, exploring its typ
**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.

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

# Structural design pattern

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

- **Adapter:** Converts one interface into another expected by clients, enabling incompatible interfaces to work together.
[adapter-design-pattern](https://engineering.multividas.com/posts/adapter-design-pattern)

- **Bridge:** Separates an abstraction from its implementation, allowing both to vary independently.
[bridge-design-pattern](https://engineering.multividas.com/posts/bridge-design-pattern)

- **Facade:** Provides a unified interface to a set of interfaces in a subsystem, making it easier to use and interact with.
[facade-design-pattern](https://engineering.multividas.com/posts/facade-design-pattern)


# Behavioral design pattern

Expand Down
2 changes: 1 addition & 1 deletion posts/facade-design-pattern.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ In this article, we’ll take a deep dive into Facade design pattern, exploring

# Facade design pattern

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

Key features:

Expand Down
2 changes: 1 addition & 1 deletion posts/singleton-design-pattern.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ In this article, we’ll take a deep dive into Singleton design pattern, explori

# Singleton design pattern

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

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

Expand Down
Loading