Design Patterns
The Repository Pattern
Mediate between the domain model and the data mapping layer using a collection-like interface for accessing domain objects.
A Repository encapsulates the logic for querying and persisting aggregates, presenting a collection-like interface to the domain layer. The domain asks the repository for objects by identity or specification; it never writes SQL or calls an ORM directly. This decouples the domain from persistence infrastructure and makes it straightforward to test the domain with in-memory fakes.
interface OrderRepository
{
public function findById(OrderId $id): ?Order;
public function findPendingOlderThan(\DateTimeImmutable $cutoff): array;
public function save(Order $order): void;
public function remove(Order $order): void;
}Continue reading
- →
Dependency Injection and IoC Containers
Invert control of dependency creation to keep classes decoupled from their collaborators and trivially testable.
- →
The Observer Pattern and Event-Driven Design
Decouple publishers from subscribers by letting objects register interest in events without the producer knowing who is listening.
- →
The Strategy Pattern: Encapsulating Algorithms
Define a family of algorithms, encapsulate each one, and make them interchangeable — open for extension, closed for modification.