Encapsulating Criteria Using Specifications
"Specification provides a concise way of expressing certain kinds of rules, extricating them from conditional logic and making them explicit in the model."
Eric Evans : Domain Driven Design
In our domain model we have a normal Customer object that looks like this:
With this code, we have defined a customer as something with a first name, last name, and a signup date. We have also specified (using the IsAnActiveCustomer method) that an active customer is a customer who has signed up with us within the current year.
This method that determines whether or not a customer is active as it stands isn't very flexible. To make the method more flexible we can extract the criteria that determines whether or not a customer is active or not and add it as a parameter to the IsAnActiveCustomer method so that the criteria can be passed in.
In order to encapsulate the active customer we can leverage the specification pattern. Our first attempt at making an active customer specification might look something like this:
and in the customer class:
By adding the specification as a parameter to the IsAnActiveCustomer method we have inverted the control of the definition of an active customer to consumers of the customer class. There may be different definitions of what an active customer is in different situations.
We can make one more improvement to the specification class before moving on. What happens if another class wants to leverage the specification interface? They have to create a new specification for their new type...or they can use generics to save themselves the time of writing a new specification interface for every new type. Here is the result of our change:
Remember Me
a@href@title, strike
Theme design by Jelle Druyts
Pick a theme: BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2 BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Steven Rockarts
E-mail