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#

public interface ISpecification { bool IsSatisfiedBy(object candidate); ISpecification And(ISpecification other); ISpecification AndNot(ISpecification other); ISpecification Or(ISpecification other); ISpecification OrNot(ISpecification other); ISpecification Not(); }

public abstract class CompositeSpecification : ISpecification { public abstract bool IsSatisfiedBy(object candidate);

public ISpecification And(ISpecification other)

return new AndSpecification(this, other);

public ISpecification AndNot(ISpecification other)

return new AndNotSpecification(this, other);

public ISpecification Or(ISpecification other)

return new OrSpecification(this, other);

public ISpecification OrNot(ISpecification other)

return new OrNotSpecification(this, other);

public ISpecification Not()

return new NotSpecification(this);

public class AndSpecification : CompositeSpecification { private ISpecification _leftCondition; private ISpecification _rightCondition;

public AndSpecification(ISpecification left, ISpecification right)

_leftCondition = left; _rightCondition = right;

public override bool IsSatisfiedBy(object candidate)

return _leftCondition.IsSatisfiedBy(candidate) && _rightCondition.IsSatisfiedBy(candidate);

public class AndNotSpecification : CompositeSpecification { private ISpecification _leftCondition; private ISpecification _rightCondition;

public AndNotSpecification(ISpecification left, ISpecification right)

_leftCondition = left; _rightCondition = right;

public override bool IsSatisfiedBy(object candidate)

return _leftCondition.IsSatisfiedBy(candidate) && _rightCondition.IsSatisfiedBy(candidate) != true;

public class OrSpecification : CompositeSpecification { private ISpecification _leftCondition; private ISpecification _rightCondition;

public OrSpecification(ISpecification left, ISpecification right)

_leftCondition = left; _rightCondition = right;

public override bool IsSatisfiedBy(object candidate)

return _leftCondition.IsSatisfiedBy(candidate) || _rightCondition.IsSatisfiedBy(candidate);

public class OrNotSpecification : CompositeSpecification { private ISpecification _leftCondition; private ISpecification _rightCondition;

public OrNotSpecification(ISpecification left, ISpecification right)

_leftCondition = left; _rightCondition = right;

public override bool IsSatisfiedBy(object candidate)

return _leftCondition.IsSatisfiedBy(candidate) || _rightCondition.IsSatisfiedBy(candidate) != true;

public class NotSpecification : CompositeSpecification { private ISpecification _wrapped;

public NotSpecification(ISpecification x)

_wrapped = x;

public override bool IsSatisfiedBy(object candidate)

return !_wrapped.IsSatisfiedBy(candidate);

C# 6.0 with generics

public interface ISpecification<T> { bool IsSatisfiedBy(T candidate); ISpecification<T> And(ISpecification<T> other); ISpecification<T> AndNot(ISpecification<T> other); ISpecification<T> Or(ISpecification<T> other); ISpecification<T> OrNot(ISpecification<T> other); ISpecification<T> Not(); }

public abstract class LinqSpecification<T> : CompositeSpecification<T> { public abstract Expression<Func<T, bool>> AsExpression(); public override bool IsSatisfiedBy(T candidate) => AsExpression().Compile()(candidate); }

public abstract class CompositeSpecification<T> : ISpecification<T> { public abstract bool IsSatisfiedBy(T candidate); public ISpecification<T> And(ISpecification<T> other) => new AndSpecification<T>(this, other); public ISpecification<T> AndNot(ISpecification<T> other) => new AndNotSpecification<T>(this, other); public ISpecification<T> Or(ISpecification<T> other) => new OrSpecification<T>(this, other); public ISpecification<T> OrNot(ISpecification<T> other) => new OrNotSpecification<T>(this, other); public ISpecification<T> Not() => new NotSpecification<T>(this); }

public class AndSpecification<T> : CompositeSpecification<T> { private ISpecification<T> _left; private ISpecification<T> _right;

public AndSpecification(ISpecification<T> left, ISpecification<T> right)

_left = left; _right = right;

public override bool IsSatisfiedBy(T candidate) => _left.IsSatisfiedBy(candidate) && _right.IsSatisfiedBy(candidate); }

public class AndNotSpecification<T> : CompositeSpecification<T> { private ISpecification<T> _left; private ISpecification<T> _right;

public AndNotSpecification(ISpecification<T> left, ISpecification<T> right)

_left = left; _right = right;

public override bool IsSatisfiedBy(T candidate) => _left.IsSatisfiedBy(candidate) && !_right.IsSatisfiedBy(candidate); }

public class OrSpecification<T> : CompositeSpecification<T> { private ISpecification<T> _left; private ISpecification<T> _right;

public OrSpecification(ISpecification<T> left, ISpecification<T> right)

_left = left; _right = right;

public override bool IsSatisfiedBy(T candidate) => _left.IsSatisfiedBy(candidate) || _right.IsSatisfiedBy(candidate); } public class OrNotSpecification<T> : CompositeSpecification<T> { private ISpecification<T> _left; private ISpecification<T> _right;

public OrNotSpecification(ISpecification<T> left, ISpecification<T> right)

_left = left; _right = right;

public override bool IsSatisfiedBy(T candidate) => _left.IsSatisfiedBy(candidate) || !_right.IsSatisfiedBy(candidate); }

public class NotSpecification<T> : CompositeSpecification<T> { ISpecification<T> other; public NotSpecification(ISpecification<T> other) => this.other = other; public override bool IsSatisfiedBy(T candidate) => !other.IsSatisfiedBy(candidate); }

Python

from abc import ABC, abstractmethod from dataclasses import dataclass from typing import Any

class BaseSpecification(ABC): @abstractmethod def is_satisfied_by(self, candidate: Any) -> bool: raise NotImplementedError()

def __call__(self, candidate: Any) -> bool: return self.is_satisfied_by(candidate)

def __and__(self, other: "BaseSpecification") -> "AndSpecification": return AndSpecification(self, other)

def __or__(self, other: "BaseSpecification") -> "OrSpecification": return OrSpecification(self, other)

def __neg__(self) -> "NotSpecification": return NotSpecification(self)

@dataclass(frozen=True) class AndSpecification(BaseSpecification): first: BaseSpecification second: BaseSpecification

def is_satisfied_by(self, candidate: Any) -> bool: return self.first.is_satisfied_by(candidate) and self.second.is_satisfied_by(candidate)

@dataclass(frozen=True) class OrSpecification(BaseSpecification): first: BaseSpecification second: BaseSpecification

def is_satisfied_by(self, candidate: Any) -> bool: return self.first.is_satisfied_by(candidate) or self.second.is_satisfied_by(candidate)

@dataclass(frozen=True) class NotSpecification(BaseSpecification): subject: BaseSpecification

def is_satisfied_by(self, candidate: Any) -> bool: return not self.subject.is_satisfied_by(candidate)

C++

template <class T> class ISpecification { public: virtual ~ISpecification() = default; virtual bool IsSatisfiedBy(T Candidate) const = 0; virtual ISpecification<T>* And(const ISpecification<T>& Other) const = 0; virtual ISpecification<T>* AndNot(const ISpecification<T>& Other) const = 0; virtual ISpecification<T>* Or(const ISpecification<T>& Other) const = 0; virtual ISpecification<T>* OrNot(const ISpecification<T>& Other) const = 0; virtual ISpecification<T>* Not() const = 0;

template <class T> class CompositeSpecification : public ISpecification<T> { public: virtual bool IsSatisfiedBy(T Candidate) const override = 0;

virtual ISpecification<T>* And(const ISpecification<T>& Other) const override; virtual ISpecification<T>* AndNot(const ISpecification<T>& Other) const override; virtual ISpecification<T>* Or(const ISpecification<T>& Other) const override; virtual ISpecification<T>* OrNot(const ISpecification<T>& Other) const override; virtual ISpecification<T>* Not() const override;

template <class T> class AndSpecification final : public CompositeSpecification<T> { public: const ISpecification<T>& Left; const ISpecification<T>& Right;

AndSpecification(const ISpecification<T>& InLeft, const ISpecification<T>& InRight) : Left(InLeft), Right(InRight) { }

virtual bool IsSatisfiedBy(T Candidate) const override { return Left.IsSatisfiedBy(Candidate) && Right.IsSatisfiedBy(Candidate);

template <class T> ISpecification<T>* CompositeSpecification<T>::And(const ISpecification<T>& Other) const { return new AndSpecification<T>(*this, Other); }

template <class T> class AndNotSpecification final : public CompositeSpecification<T> { public: const ISpecification<T>& Left; const ISpecification<T>& Right;

AndNotSpecification(const ISpecification<T>& InLeft, const ISpecification<T>& InRight) : Left(InLeft), Right(InRight) { }

virtual bool IsSatisfiedBy(T Candidate) const override { return Left.IsSatisfiedBy(Candidate) && !Right.IsSatisfiedBy(Candidate);

template <class T> class OrSpecification final : public CompositeSpecification<T> { public: const ISpecification<T>& Left; const ISpecification<T>& Right;

OrSpecification(const ISpecification<T>& InLeft, const ISpecification<T>& InRight) : Left(InLeft), Right(InRight) { }

virtual bool IsSatisfiedBy(T Candidate) const override { return Left.IsSatisfiedBy(Candidate) || Right.IsSatisfiedBy(Candidate);

template <class T> class OrNotSpecification final : public CompositeSpecification<T> { public: const ISpecification<T>& Left; const ISpecification<T>& Right;

OrNotSpecification(const ISpecification<T>& InLeft, const ISpecification<T>& InRight) : Left(InLeft), Right(InRight) { }

virtual bool IsSatisfiedBy(T Candidate) const override { return Left.IsSatisfiedBy(Candidate) || !Right.IsSatisfiedBy(Candidate);

template <class T> class NotSpecification final : public CompositeSpecification<T> { public: const ISpecification<T>& Other;

NotSpecification(const ISpecification<T>& InOther) : Other(InOther) { }

virtual bool IsSatisfiedBy(T Candidate) const override { return !Other.IsSatisfiedBy(Candidate);

template <class T> ISpecification<T>* CompositeSpecification<T>::AndNot(const ISpecification<T>& Other) const { return new AndNotSpecification<T>(*this, Other); }

template <class T> ISpecification<T>* CompositeSpecification<T>::Or(const ISpecification<T>& Other) const { return new OrSpecification<T>(*this, Other); }

template <class T> ISpecification<T>* CompositeSpecification<T>::OrNot(const ISpecification<T>& Other) const {