Junior — Middle
Describe your experience implementing design patterns using abstract interfaces and concepts.
sobes.tech AI
Answer from AI
In my experience working with PHP, I often used design patterns based on abstract interfaces and concepts to enhance the flexibility and extensibility of the code. For example, when implementing the "Factory" pattern, I created an interface for products, and specific classes implemented this interface. The abstract interface allowed client code to work with products without knowing their specific classes.
Example:
interface LoggerInterface {
public function log(string $message);
}
class FileLogger implements LoggerInterface {
public function log(string $message) {
// write to file
}
}
class DatabaseLogger implements LoggerInterface {
public function log(string $message) {
// write to database
}
}
function process(LoggerInterface $logger) {
$logger->log('Process started');
}
// It's easy to change the logger implementation without modifying the process function
process(new FileLogger());
This approach makes it easy to introduce new implementations, test the code, and adhere to SOLID principles, especially Dependency Inversion and Open/Closed.