Specification design pattern

The Specification design pattern is incredibly powerful. Here, I will attempt to provide a high-level overview of it, but there is plenty to explore; I highly recommend the paper Specifications by Eric Evans and Martin Fowler if you are interested in learning more.

This design pattern is used to codify business rules that state something about an object. They tell us whether an object satisfies some business criteria or not.

We can use them in the following ways:

  • To make assertions about an object, for validation
  • To fetch a selection of objects from a given collection
  • To specify how an object can be created by building to order

In this example, we're going to build Specification to query

Let's take the following objects: ...

Get Mastering PHP Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.