2. Specification Pattern by TungNT 2
Introduction
“In computer programming, the specification pattern is a
particular software design pattern, whereby business rules can
be recombined by chaining the business rules together using
boolean logic. The pattern is frequently used in the context of
domain-driven design.”
- Wikipedia -
4. Specification Pattern by TungNT 4
Let's start by example
● Let's assume we need to build a recruitment
system and we have a user story look like this:
– As recruiter I can check on the status of candidate
– As recruiter I can get list of candidate who passed
Note: Candidate passed if:
– Scala skill test score >= 80
– Interview passed
– ...
6. Specification Pattern by TungNT 6
OOP way
● In “As recruiter I can check on the status of candidate”
~> Created Candidate class which has isPassed
method to check status of candidate
7. Specification Pattern by TungNT 7
OOP way (cont.)
● In “As recruiter I can get list of candidate who passed”
~> Created Candidate Repository to get all
candidate who passed from persitence
10. Specification Pattern by TungNT 10
Specification way
● In “As recruiter I can check on the status of candidate”
~>
11. Specification Pattern by TungNT 11
Specification way (cont.)
● In “As recruiter I can get list of candidate who passed”
~>
12. Specification Pattern by TungNT 12
As you'll soon realize...
● specifications are:
– cheap to write
– easy to test
– encapsulate the business rule
– use the Specification Object anywhere
– can be combined to represent very complex
business rules
20. Specification Pattern by TungNT 20
Conclusion
● Some key benefits of the Specification Pattern
are:
– Loose coupling of the filter logic from the objects
being filtered
– Single responsibility: Interface of repository/data
provider isn’t polluted with lots of filter methods
– Encapsulate the business rule and...
– Can be combined to represent very complex
business rules
21. Specification Pattern by TungNT 21
References
● Links:
1. https://en.wikipedia.org/wiki/Specification_pattern
2. http://marcaube.ca/2015/05/specifications/
3. Is the Specification Pattern obsolete?