ArticleslgStudy

computer science

Specification pattern

Specification pattern is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Specification pattern rather than just read about it. In short: 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.

Specification pattern — main illustration
Specification pattern — illustration

Key takeaways

  • Specification pattern belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Specification pattern to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Specification pattern from memory before moving on to harder problems.

Reference excerpt

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. A specification pattern outlines a business rule that is combinable with other business rules. In this pattern, a unit of business logic inherits its functionality from the abstract aggregate Composite Specification class. The Composite Specification class has one function called IsSatisfiedBy that returns a Boolean value. After instantiation, the specification is "chained" with other specifications, making new specifications easily maintainable, yet highly customizable business logic. Furthermore, upon instantiation the business logic may, through method invocation or inversion of control, have its state altered in order to become a delegate of other classes such as a persistence repository. As a consequence of performing runtime composition of high-level business/domain logic, the Specification pattern is a convenient tool for converting ad-hoc user search criteria into low level logic to be processed by repositories. Since a specification is an encapsulation of logic in a reusable form it is very simple to thoroughly unit test, and when used in this context is also an implementation of the humble object pattern.

Code examples

C#

C# 10.0 with generics

Python

C++

TypeScript

Example of use In the next example, invoices are retrieved and sent to a collection agency if:

they are overdue, notices have been sent, and they are not already with the collection agency. This example is meant to show the result of how the logic is 'chained' together. This usage example assumes a previously defined OverdueSpecification class that is satisfied when an invoice's due date is 30 days or older, a NoticeSentSpecification class that is satisfied when three notices have been sent to the customer, and an InCollectionSpecification class that is satisfied when an invoice has already been sent to the collection agency. The implementation of these classes isn't important here. Using these three specifications, we created a new specification called SendToCollection which will be satisfied when an invoice is overdue, when notices have been sent to the customer, and are not already with the collection agency.

References Evans, Eric (2004). Domain Driven Design. Addison-Wesley. p. 224.

External links Specifications by Eric Evans and Martin Fowler The Specification Pattern: A Primer by Matt Berther The Specification Pattern: A Four Part Introduction using VB.Net by Richard Dalton The Specification Pattern in PHP by Moshe Brevda Happyr Doctrine Specification in PHP by Happyr The Specification Pattern in Swift by Simon Strandgaard The Specification Pattern in TypeScript and JavaScript by Thiago Delgado Pinto specification pattern in flash actionscript 3 by Rolf Vreijdenberger

Illustrations

Specification pattern: Specification Pattern in UML
Specification Pattern in UML

Worked examples

Example 1 — a first encounter with Specification pattern

Start with the simplest possible case. Write down what Specification pattern claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Specification pattern before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Specification pattern ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Specification pattern

In research
Specification pattern appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Specification pattern in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Specification pattern is common in secondary-school and first-year university syllabi. It links to neighbouring topics Architectural pattern (computer science), Programming language comparisons, Software design patterns, so understanding it makes those chapters shorter.
In everyday life
Look for Specification pattern outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Specification pattern” →

How to study Specification pattern in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Specification pattern means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Specification pattern out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Specification pattern in simple terms?

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.

Why does Specification pattern matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Specification pattern?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Specification pattern.

Tags

  • Architectural pattern (computer science)
  • Programming language comparisons
  • Software design patterns

Keep exploring