One minute
Specification pattern
Introduction
Hi. Today, we’re going to take a look at the Specification pattern. Without further ado, let’s dive in.
Specification is an architectural design pattern introduced by Eric Evans. It is most commonly found in Domain-Driven Design (DDD).
It is mainly based on the mechanisms of composition and Boolean algebra.
The main element of this pattern is the CompositeSpecification class, which implements an interface declaring methods that allow business rules to be nested (And, Or, Not), as well as the IsSatisfiedBy method, which validates all the business rules.
classDiagram
CompositeSpecification <|-- AndSpecification
CompositeSpecification <|-- OrSpecification
CompositeSpecification <|-- NotSpecification
CompositeSpecification: +bool And()
CompositeSpecification: +bool Or()
CompositeSpecification: +bool Not()
CompositeSpecification: +bool IsSatisfiedBy()
Specification -- CompositeSpecification
class Specification{
<>
+bool And()
+bool Or()
+bool Not()
+bool IsSatisfiedBy()
}
class AndSpecification{
+String beakColor
+swim()
+quack()
}
class OrSpecification{
-int sizeInFeet
-canEat()
}
class NotSpecification{
+bool wrapped
+run()
}
A real-life example — sending an unpaid invoice to collections.
In order to send an invoice to collections, it must meet the following conditions:
- the payment due date was more than 30 days ago
- the customer has been notified about the unpaid invoice
- the invoice has not already been sent to collections
php coe here