Specification Design Pattern

Definition

The central idea of Specification is to separate the statement of how to match a candidate, from the candidate object that it is matched against. The specification responsibility is to identify an object that satisfies it, and to combine with other specifications in various ways that support that basic responsibility (Eric Evans & Martin Fowler)

Applicability

  • You need to select a subset of objects based on some criteria, and to refresh the selection at various times.
  • You need to check or validate that only suitable objects are used for a certain purpose.
  • You need to describe what an object might do, without explaining the details of how the object does it, but in such a way that a candidate might be built to fulfil the requirement.


Consequences

  • Decouple the design of requirements, fulfilment, and validation
  • Allows clear and declarative system definitions

Downsides

  • There lacks a well-defined purpose for this pattern, and there's no guide when to implement it or not.
  • Separate classes for each part of the specification fragments what could be a cohesive object.

Sample Code

Generic Specification
Gold Customers Specification Sample

    Comments

    Popular Posts