PHP Design Patterns: Repository, Factory ve Strategy
z
zafer ak
Yazar
13 November 2025
2 dakika okuma
2 görüntülenme
PHP'de yaygın design pattern'lar. Repository, Factory, Strategy, Observer ve Singleton implementasyonları.
Design Patterns Nedir?
Design patterns, yazılım geliştirmede sık karşılaşılan problemlere yeniden kullanılabilir çözümlerdir. Gang of Four (GoF) tarafından popülerleştirildi.
Repository Pattern
Data access logic'i soyutlama:
// Interface
interface UserRepositoryInterface
{
public function find(int $id): ?User;
public function findByEmail(string $email): ?User;
public function save(User $user): void;
}
// Implementation
class EloquentUserRepository implements UserRepositoryInterface
{
public function find(int $id): ?User
{
return User::find($id);
}
public function findByEmail(string $email): ?User
{
return User::where('email', $email)->first();
}
public function save(User $user): void
{
$user->save();
}
}
// Service Provider binding
$this->app->bind(UserRepositoryInterface::class, EloquentUserRepository::class);
Factory Pattern
Object creation'ı soyutlama:
interface NotificationFactory
{
public function create(string $type): Notification;
}
class NotificationFactoryImpl implements NotificationFactory
{
public function create(string $type): Notification
{
return match($type) {
'email' => new EmailNotification(),
'sms' => new SmsNotification(),
'push' => new PushNotification(),
default => throw new InvalidArgumentException()
};
}
}
// Kullanım
$notification = $factory->create('email');
$notification->send($user, $message);
Strategy Pattern
Algoritma seçimini runtime'da değiştirme:
interface PaymentStrategy
{
public function pay(float $amount): bool;
}
class CreditCardPayment implements PaymentStrategy
{
public function pay(float $amount): bool
{
// Kredi kartı işlemi
return true;
}
}
class PayPalPayment implements PaymentStrategy
{
public function pay(float $amount): bool
{
// PayPal işlemi
return true;
}
}
class PaymentProcessor
{
public function __construct(
private PaymentStrategy $strategy
) {}
public function process(float $amount): bool
{
return $this->strategy->pay($amount);
}
}
// Kullanım
$processor = new PaymentProcessor(new CreditCardPayment());
$processor->process(100.00);
Observer Pattern
Event-driven communication:
// Laravel Events
class OrderCreated
{
public function __construct(public Order $order) {}
}
class SendOrderConfirmation
{
public function handle(OrderCreated $event): void
{
Mail::to($event->order->user)->send(new OrderConfirmationMail($event->order));
}
}
// Dispatch
event(new OrderCreated($order));
Singleton Pattern
class Database
{
private static ?self $instance = null;
private function __construct() {}
public static function getInstance(): self
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
}
// Laravel'de Service Container ile
$this->app->singleton(PaymentGateway::class);
Decorator Pattern
interface Logger
{
public function log(string $message): void;
}
class FileLogger implements Logger
{
public function log(string $message): void
{
file_put_contents('log.txt', $message, FILE_APPEND);
}
}
class TimestampLogger implements Logger
{
public function __construct(private Logger $logger) {}
public function log(string $message): void
{
$this->logger->log('[' . date('Y-m-d H:i:s') . '] ' . $message);
}
}
$logger = new TimestampLogger(new FileLogger());
Sonuç
Design patterns, kod kalitesini ve maintainability'yi artırır. Laravel zaten birçok pattern'ı kullanır (Repository, Factory, Observer).