Specifications pattern
TungNT @ Septeni Technolgy
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 -
Specification Pattern by TungNT 3
Hmmm...
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
– ...
Specification Pattern by TungNT 5
Go! go! go!...
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
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
Specification Pattern by TungNT 8
But...
Specification Pattern by TungNT 9
Is there a better way to do this?
Specification Pattern by TungNT 10
Specification way
● In “As recruiter I can check on the status of candidate”
~>
Specification Pattern by TungNT 11
Specification way (cont.)
● In “As recruiter I can get list of candidate who passed”
~>
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
Specification Pattern by TungNT 13
How it work?
Specification Pattern by TungNT 14
By DDD sample
● Create a Specification interface:
Specification Pattern by TungNT 15
By DDD sample (cont.)
● Create a Specification abstract:
Specification Pattern by TungNT 16
By DDD sample (cont.)
● Create a AND Specification:
Specification Pattern by TungNT 17
By DDD sample (cont.)
● Create a OR Specification:
Specification Pattern by TungNT 18
By DDD sample (cont.)
● Create a NOT Specification:
Specification Pattern by TungNT 19
Using combined specs
● And operator
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
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?
Specification Pattern by TungNT 22
Thank you!

Specifications pattern

  • 1.
  • 2.
    Specification Pattern byTungNT 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 -
  • 3.
    Specification Pattern byTungNT 3 Hmmm...
  • 4.
    Specification Pattern byTungNT 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 – ...
  • 5.
    Specification Pattern byTungNT 5 Go! go! go!...
  • 6.
    Specification Pattern byTungNT 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 byTungNT 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
  • 8.
    Specification Pattern byTungNT 8 But...
  • 9.
    Specification Pattern byTungNT 9 Is there a better way to do this?
  • 10.
    Specification Pattern byTungNT 10 Specification way ● In “As recruiter I can check on the status of candidate” ~>
  • 11.
    Specification Pattern byTungNT 11 Specification way (cont.) ● In “As recruiter I can get list of candidate who passed” ~>
  • 12.
    Specification Pattern byTungNT 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
  • 13.
    Specification Pattern byTungNT 13 How it work?
  • 14.
    Specification Pattern byTungNT 14 By DDD sample ● Create a Specification interface:
  • 15.
    Specification Pattern byTungNT 15 By DDD sample (cont.) ● Create a Specification abstract:
  • 16.
    Specification Pattern byTungNT 16 By DDD sample (cont.) ● Create a AND Specification:
  • 17.
    Specification Pattern byTungNT 17 By DDD sample (cont.) ● Create a OR Specification:
  • 18.
    Specification Pattern byTungNT 18 By DDD sample (cont.) ● Create a NOT Specification:
  • 19.
    Specification Pattern byTungNT 19 Using combined specs ● And operator
  • 20.
    Specification Pattern byTungNT 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 byTungNT 21 References ● Links: 1. https://en.wikipedia.org/wiki/Specification_pattern 2. http://marcaube.ca/2015/05/specifications/ 3. Is the Specification Pattern obsolete?
  • 22.
    Specification Pattern byTungNT 22 Thank you!