From 1fe7d9397519a07edd3ff79afc74b1087caae82e Mon Sep 17 00:00:00 2001 From: Soulaimane Yahya Date: Sun, 19 Jan 2025 21:06:25 +0100 Subject: [PATCH] feat/ARTICLE/DESIGN-PATTERNS --- posts/adapter-design-pattern.md | 94 +++++++++++++++++++++++ posts/bridge-design-pattern.md | 121 ++++++++++++++++++++++++++++++ posts/design-patterns.md | 7 ++ posts/facade-design-pattern.md | 2 +- posts/singleton-design-pattern.md | 2 +- 5 files changed, 224 insertions(+), 2 deletions(-) diff --git a/posts/adapter-design-pattern.md b/posts/adapter-design-pattern.md index 62f6f5d..0c69db7 100755 --- a/posts/adapter-design-pattern.md +++ b/posts/adapter-design-pattern.md @@ -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 +legacyLogger = $legacyLogger; + } + + public function log(string $message): void + { + $this->legacyLogger->saveLogToFile($message); + } +} +``` + +```php +app->bind(LoggerInterface::class, function ($app) { + $legacyLogger = new LegacyFileLogger(); + return new FileLoggerAdapter($legacyLogger); + }); + } +} +``` + +```php +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. + diff --git a/posts/bridge-design-pattern.md b/posts/bridge-design-pattern.md index adba007..775dbef 100755 --- a/posts/bridge-design-pattern.md +++ b/posts/bridge-design-pattern.md @@ -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 +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 +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. diff --git a/posts/design-patterns.md b/posts/design-patterns.md index 5722194..3964e8c 100755 --- a/posts/design-patterns.md +++ b/posts/design-patterns.md @@ -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 diff --git a/posts/facade-design-pattern.md b/posts/facade-design-pattern.md index 28b7901..c239a56 100755 --- a/posts/facade-design-pattern.md +++ b/posts/facade-design-pattern.md @@ -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: diff --git a/posts/singleton-design-pattern.md b/posts/singleton-design-pattern.md index 24202c1..758be75 100755 --- a/posts/singleton-design-pattern.md +++ b/posts/singleton-design-pattern.md @@ -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. singleton-design-pattern