The specification pattern is a particular software design pattern, whereby business rules can be recombined by chaining the business rules together using boolean logic. An example case is explained (where I use the postfix Condition instead of Specification) in Groovy.
2. “Separate the statement of how to
match a candidate, from the candidate
object that it is matched against”
-- Martin Fowler
http://martinfowler.com/apsupp/spec.pdf
6. Using a specification as an input to a route selector. This decouples the route selector from the
shipment.
7. Advantages
Treating the specification as a separate object has a number of advantages
Lets you decouple the design of requirements, fulfillment, and validation
Allows you to make your systemdefinitions more clear and declarative
8.
9. 3 simple requirements
1. Animal should be
female
2. Animal should not have
been tested before
(Herdbook API)
3. No existing genomic
tests (Breeding API)
10. Is a genomic test allowed?
version 1
(Warning: Groovy ahead!)
11. Render only animals for which genomic test is
allowed
Need:
HousingService housingService
Iterate and check:
Collection<Animal> animals = housingService.retrieveAnimals()
animals.each { animal ->
boolean matches = isGenomicTestAllowed(animal)
12. Animal should be female
Need:
class Animal {
AnimalId id
String name
String gender
}
Check:
gender.toLowerCase() == ‘female’
13. Animal should not have been tested before
Need:
HerdbookRepository herdbookRepository
Check:
AnimalHeredityResource heredity =
herdbookRepository.retrieveHeredity(animal.id)
heredity != null