Introduction
Design patterns provide efficient solutions to recurring software development problems. In this article, we'll explore two powerful design patterns Flyweight and Singleton with practical examples from my GitHub repositories.
Flyweight Pattern 🏗️
The Flyweight pattern is a structural design pattern that optimizes memory usage by sharing common data among objects. It divides object states into:
- Intrinsic State: Shared among many objects.
- Extrinsic State: Unique to each object, provided at runtime.
This pattern is beneficial when handling a large number of similar objects efficiently, reducing memory overhead.
Below is an example from my PHP Design Patterns repository. The Flyweight
class encapsulates shared state and accepts unique state.
<?php
namespace Behavorial;
class Flyweight {
private $sharedState;
public function __construct($sharedState) {
$this->sharedState = $sharedState;
}
/**
* Performs an operation using shared (intrinsic) and unique (extrinsic) states.
*
* @param mixed $uniqueState The extrinsic state supplied at runtime.
*/
public function operation($uniqueState) {
echo "Flyweight: Shared state (" . $this->sharedState . ") " .
"and unique state (" . $uniqueState . ")<br>";
}
}
// Example usage:
$flyweight = new Flyweight('Shared Data');
$flyweight->operation('Unique Data');
Benefits of Flyweight
- Memory Efficiency: Reduces redundant object storage.
- Performance Optimization: Speeds up applications dealing with numerous similar objects.
- Scalability: Useful for caching, rendering systems, and object pooling.
Singleton Pattern
The Singleton pattern is a creational design pattern that ensures a class has only one instance, providing global access to it. It is commonly used for logging, configuration management, and shared resource control.
Here’s an example from my Design Patterns repository. This LuzSingleton
class manages a light's state:
<?php
class LuzSingleton {
private static ?LuzSingleton $instance = null;
private string $lightStatus;
private function __construct() {
$this->lightStatus = 'OFF';
}
public static function getInstance(): LuzSingleton {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Turns the light ON.
*/
public function turnOn(): void {
$this->lightStatus = 'ON';
echo "Light turned {$this->lightStatus}\n";
}
/**
* Turns the light OFF.
*/
public function turnOff(): void {
$this->lightStatus = 'OFF';
echo "Light turned {$this->lightStatus}\n";
}
/**
* Retrieves the current status of the light.
*
* @return string The current light status.
*/
public function getStatus(): string {
return $this->lightStatus;
}
}
// Exemplo de uso
$luz = LuzSingleton::getInstance();
$luz->turnOn();
echo $luz->getStatus(); // ON
Benefits of Singleton
- Consistent State: Ensures a single point of truth for shared resources.
- Global Access: Prevents redundant object creation.
- Better Resource Management: Useful for logging, caching, and configuration settings.
Conclusion
Both Flyweight and Singleton patterns are essential tools in software development. While Flyweight optimizes memory usage by sharing common data, Singleton guarantees a single instance for centralized control.
By integrating these design patterns into your PHP and Node.js applications, you can build more efficient, maintainable, and scalable solutions.
🚀 Explore my GitHub repositories for more examples:
💬 Let’s Discuss!
Do you use these patterns in your projects? Share your thoughts in the comments below!
Top comments (0)