Specification pattern: Difference between revisions

Content deleted Content added
Rdckc (talk | contribs)
m corrected compilation error
 
(31 intermediate revisions by 20 users not shown)
Line 1:
{{Short description|Software design pattern}}
[[Image:Specification_UML_v2.png|right|thumb|300px|Specification Pattern in [[Unified Modeling Language|UML]] ]]
[[File:Specification UML.png|right|thumb|300px|Specification Pattern in [[Unified Modeling Language|UML]] ]]
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]].
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 algebra|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 Sharp (programming language)|C#]] ===
{{Further|C Sharp (programming language)}}
<syntaxhighlight lang="csharp">
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
<source lang="csharp">
{
public interface ISpecification
public abstract bool IsSatisfiedBy(object candidate);
 
public ISpecification And(ISpecification other)
{
boolreturn IsSatisfiedBynew AndSpecification(objectthis, candidateother);
ISpecification And(ISpecification other);
ISpecification AndNot(ISpecification other);
ISpecification Or(ISpecification other);
ISpecification OrNot(ISpecification other);
ISpecification Not();
}
 
public ISpecification AndNot(ISpecification other)
public abstract class CompositeSpecification : ISpecification
{
publicreturn abstractnew bool IsSatisfiedByAndNotSpecification(objectthis, candidateother);
}
 
public ISpecification AndOr(ISpecification other)
{
return new AndSpecificationOrSpecification(this, other);
}
 
public ISpecification AndNotOrNot(ISpecification other)
{
return new AndNotSpecificationOrNotSpecification(this, other);
}
 
public ISpecification OrNot(ISpecification other)
{
return new OrSpecificationNotSpecification(this, other);
}
}
 
public class AndSpecification : CompositeSpecification
public ISpecification OrNot(ISpecification other)
{
{
private ISpecification _leftCondition;
return new OrNotSpecification(this, other);
private ISpecification _rightCondition;
}
 
public AndSpecification(ISpecification publicleft, ISpecification Not(right)
{
_leftCondition = left;
return new NotSpecification(this);
}_rightCondition = right;
}
 
public override bool IsSatisfiedBy(object candidate)
public class AndSpecification : CompositeSpecification
{
return _leftCondition.IsSatisfiedBy(candidate) && _rightCondition.IsSatisfiedBy(candidate);
private ISpecification leftCondition;
}
private ISpecification rightCondition;
}
 
public class AndNotSpecification : CompositeSpecification
public AndSpecification(ISpecification left, ISpecification right)
{
{
private ISpecification _leftCondition;
leftCondition = left;
private ISpecification _rightCondition;
rightCondition = right;
}
 
public AndNotSpecification(ISpecification left, ISpecification right)
public override bool IsSatisfiedBy(object candidate)
{
_leftCondition = left;
return leftCondition.IsSatisfiedBy(candidate) && rightCondition.IsSatisfiedBy(candidate);
}_rightCondition = right;
}
 
public override bool IsSatisfiedBy(object candidate)
public class AndNotSpecification : CompositeSpecification
{
return _leftCondition.IsSatisfiedBy(candidate) && _rightCondition.IsSatisfiedBy(candidate) != true;
private ISpecification leftCondition;
}
private ISpecification rightCondition;
}
 
public class OrSpecification : CompositeSpecification
public AndNotSpecification(ISpecification left, ISpecification right)
{
{
private ISpecification _leftCondition;
leftCondition = left;
private ISpecification _rightCondition;
rightCondition = right;
}
 
public OrSpecification(ISpecification left, ISpecification right)
public override bool IsSatisfiedBy(object candidate)
{
_leftCondition = left;
return leftCondition.IsSatisfiedBy(candidate) && rightCondition.IsSatisfiedBy(candidate) != true;
}_rightCondition = right;
}
 
public override bool IsSatisfiedBy(object candidate)
public class OrSpecification : CompositeSpecification
{
return _leftCondition.IsSatisfiedBy(candidate) || _rightCondition.IsSatisfiedBy(candidate);
private ISpecification leftCondition;
}
private ISpecification rightCondition;
}
 
public class OrNotSpecification : CompositeSpecification
public OrSpecification(ISpecification left, ISpecification right)
{
{
private ISpecification _leftCondition;
leftCondition = left;
private ISpecification _rightCondition;
rightCondition = right;
}
 
public OrNotSpecification(ISpecification left, ISpecification right)
public override bool IsSatisfiedBy(object candidate)
{
_leftCondition = left;
return leftCondition.IsSatisfiedBy(candidate) || rightCondition.IsSatisfiedBy(candidate);
}_rightCondition = right;
}
 
public override bool IsSatisfiedBy(object candidate)
public class OrNotSpecification : CompositeSpecification
{
return _leftCondition.IsSatisfiedBy(candidate) || _rightCondition.IsSatisfiedBy(candidate) != true;
private ISpecification leftCondition;
}
private ISpecification rightCondition;
}
 
public class NotSpecification : CompositeSpecification
public OrNotSpecification(ISpecification left, ISpecification right)
{
{
private ISpecification _wrapped;
leftCondition = left;
rightCondition = right;
}
 
public override bool IsSatisfiedByNotSpecification(objectISpecification candidatex)
{
_wrapped = x;
return leftCondition.IsSatisfiedBy(candidate) || rightCondition.IsSatisfiedBy(candidate) != true;
}
}
 
public override bool IsSatisfiedBy(object candidate)
public class NotSpecification : CompositeSpecification
{
return !_wrapped.IsSatisfiedBy(candidate);
private ISpecification Wrapped;
}
}
</syntaxhighlight>
 
=== C# 6.0 with generics ===
public NotSpecification(ISpecification x)
{{Further|C Sharp (programming language)}}
{
<syntaxhighlight lang="csharp">
Wrapped = x;
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 override bool IsSatisfiedBy(object candidate)
{
{
public abstract Expression<Func<T, bool>> AsExpression();
return !Wrapped.IsSatisfiedBy(candidate);
public override bool IsSatisfiedBy(T candidate) => AsExpression().Compile()(candidate);
}
}
}
 
public abstract class CompositeSpecification<T> : ISpecification<T>
</source>
{
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>
=== [[C Sharp (programming language)|C# 6.0]] with generics ===
{
private ISpecification<T> _left;
private ISpecification<T> _right;
 
public AndSpecification(ISpecification<T> left, ISpecification<T> right)
<source lang="csharp">
public interface ISpecification<T>
{
bool_left IsSatisfiedBy(T= candidate)left;
_right = right;
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 override bool IsSatisfiedBy(T candidate) => _left.IsSatisfiedBy(candidate) && _right.IsSatisfiedBy(candidate);
public abstract class LinqSpecification<T> : CompositeSpecification<T>
}
 
public class AndNotSpecification<T> : CompositeSpecification<T>
{
private ISpecification<T> _left;
private ISpecification<T> _right;
 
public AndNotSpecification(ISpecification<T> left, ISpecification<T> right)
{
_left = left;
public abstract Expression<Func<T, bool>> AsExpression();
_right = right;
public override bool IsSatisfiedBy(T candidate) => AsExpression().Compile()(candidate);
}
 
public override bool IsSatisfiedBy(T candidate) => _left.IsSatisfiedBy(candidate) && !_right.IsSatisfiedBy(candidate);
public abstract class CompositeSpecification<T> : ISpecification<T>
}
 
public class OrSpecification<T> : CompositeSpecification<T>
{
private ISpecification<T> _left;
private ISpecification<T> _right;
 
public OrSpecification(ISpecification<T> left, ISpecification<T> right)
{
_left = left;
public abstract bool IsSatisfiedBy(T candidate);
_right = right;
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 override bool IsSatisfiedBy(T candidate) => _left.IsSatisfiedBy(candidate) || _right.IsSatisfiedBy(candidate);
public class AndSpecification<T> : CompositeSpecification<T>
}
public class OrNotSpecification<T> : CompositeSpecification<T>
{
private ISpecification<T> _left;
private ISpecification<T> _right;
 
public OrNotSpecification(ISpecification<T> left, ISpecification<T> right)
{
ISpecification<T>_left = left;
ISpecification<T>_right = right;
}
 
public override bool IsSatisfiedBy(T candidate) => _left.IsSatisfiedBy(candidate) || !_right.IsSatisfiedBy(candidate);
public AndSpecification(ISpecification<T> left, ISpecification<T> right)
}
{
this.left = left;
this.right = right;
}
 
public class NotSpecification<T> : CompositeSpecification<T>
public override bool IsSatisfiedBy(T candidate) => left.IsSatisfiedBy(candidate) && right.IsSatisfiedBy(candidate);
{
}
ISpecification<T> other;
public NotSpecification(ISpecification<T> other) => this.other = other;
public override bool IsSatisfiedBy(T candidate) => !other.IsSatisfiedBy(candidate);
}
</syntaxhighlight>
 
=== Python ===
public class AndNotSpecification<T> : CompositeSpecification<T>
{{Further|Python (programming language)}}
{
<syntaxhighlight lang="python">
ISpecification<T> left;
from abc import ABC, abstractmethod
ISpecification<T> right;
from dataclasses import dataclass
from typing import Any
 
class BaseSpecification(ABC):
public AndNotSpecification(ISpecification<T> left, ISpecification<T> right)
{@abstractmethod
def is_satisfied_by(self, candidate: Any) -> bool:
this.left = left;
raise this.right = right;NotImplementedError()
}
 
def __call__(self, candidate: Any) -> bool:
public override bool IsSatisfiedBy(T candidate) => left.IsSatisfiedBy(candidate) && right.IsSatisfiedBy(candidate) != true;
return self.is_satisfied_by(candidate)
}
 
def __and__(self, other: "BaseSpecification") -> "AndSpecification":
public class OrSpecification<T> : CompositeSpecification<T>
return AndSpecification(self, other)
{
ISpecification<T> left;
ISpecification<T> right;
 
def __or__(self, other: "BaseSpecification") -> "OrSpecification":
public OrSpecification(ISpecification<T> left, ISpecification<T> right)
return OrSpecification(self, other)
{
this.left = left;
this.right = right;
}
 
def __neg__(self) -> "NotSpecification":
public override bool IsSatisfiedBy(T candidate) => left.IsSatisfiedBy(candidate) || right.IsSatisfiedBy(candidate);
return NotSpecification(self)
}
public class OrNotSpecification<T> : CompositeSpecification<T>
{
ISpecification<T> left;
ISpecification<T> right;
 
@dataclass(frozen=True)
public OrNotSpecification(ISpecification<T> left, ISpecification<T> right)
class AndSpecification(BaseSpecification):
{
first: BaseSpecification
this.left = left;
second: BaseSpecification
this.right = right;
}
 
def is_satisfied_by(self, candidate: Any) -> bool:
public override bool IsSatisfiedBy(T candidate) => left.IsSatisfiedBy(candidate) || right.IsSatisfiedBy(candidate) != true;
return self.first.is_satisfied_by(candidate) and self.second.is_satisfied_by(candidate)
}
 
@dataclass(frozen=True)
public class NotSpecification<T> : CompositeSpecification<T>
class OrSpecification(BaseSpecification):
{
first: BaseSpecification
ISpecification<T> other;
second: BaseSpecification
public NotSpecification(ISpecification<T> other) => this.other = other;
public override bool IsSatisfiedBy(T candidate) => !other.IsSatisfiedBy(candidate);
}
</source>
 
def is_satisfied_by(self, candidate: Any) -> bool:
==Example of use==
return self.first.is_satisfied_by(candidate) or self.second.is_satisfied_by(candidate)
 
@dataclass(frozen=True)
In the following example, we are retrieving invoices and sending them to a collection agency if
class NotSpecification(BaseSpecification):
subject: BaseSpecification
 
def is_satisfied_by(self, candidate: Any) -> bool:
# they are overdue,
return not self.subject.is_satisfied_by(candidate)
# notices have been sent, and
# they are not already with the collection agency.
 
</syntaxhighlight>
This example is meant to show the end result of how the logic is 'chained' together.
 
=== C++ ===
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.
{{Further|C++}}
<syntaxhighlight lang="cpp">
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>
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.
class CompositeSpecification : public ISpecification<T>
{
public:
virtual bool IsSatisfiedBy(T Candidate) const override = 0;
 
virtual ISpecification<T>* And(const ISpecification<T>& Other) const override;
<source lang="csharp">
virtual ISpecification<T>* AndNot(const ISpecification<T>& Other) const override;
var OverDue = new OverDueSpecification();
virtual ISpecification<T>* Or(const ISpecification<T>& Other) const override;
var NoticeSent = new NoticeSentSpecification();
virtual ISpecification<T>* OrNot(const ISpecification<T>& Other) const override;
var InCollection = new InCollectionSpecification();
virtual ISpecification<T>* Not() const override;
};
 
template <class T>
// example of specification pattern logic chaining
class AndSpecification final : public CompositeSpecification<T>
var SendToCollection = OverDue.And(NoticeSent).And(InCollection.Not());
{
public:
const ISpecification<T>& Left;
const ISpecification<T>& Right;
 
AndSpecification(const ISpecification<T>& InLeft, const ISpecification<T>& InRight)
var InvoiceCollection = Service.GetInvoices();
: Left(InLeft),
Right(InRight) { }
 
virtual bool IsSatisfiedBy(T Candidate) const override
foreach (var currentInvoice in InvoiceCollection) {
{
if (SendToCollection.IsSatisfiedBy(currentInvoice)) {
return Left.IsSatisfiedBy(Candidate) && Right.IsSatisfiedBy(Candidate);
currentInvoice.SendToCollection();
}
}
};
 
template <class T>
ISpecification<T>* CompositeSpecification<T>::And(const ISpecification<T>& Other) const
{
return new AndSpecification<T>(*this, Other);
}
</source>
 
template <class T>
== Criticisms ==
class AndNotSpecification final : public CompositeSpecification<T>
The Specification Pattern could be considered a software [[anti-pattern]]:
{
* [[Cargo cult programming]] - There lacks a well-defined purpose for this pattern, and there's no guide when to implement it or not. Also, see [[Law of the instrument]].
public:
* [[Inner-platform effect]] - And() function which directly replicate [[Short-circuiting operator|&&]] in [[C Sharp (programming language)|C#]]. Also, Not() and potentially more. Also, see [[Reinventing the wheel|Reinventing the square wheel]].
const ISpecification<T>& Left;
* [[Spaghetti code#Lasagna code|Spaghetti/Lasagna Code]] - Separate classes for each part of the specification fragments what could be a cohesive object. In the example above, OverDue is an extra layer between the logic for <code>SendToCollection</code> and the <code>OverDueSpecification</code> implementation.
const ISpecification<T>& Right;
Most natural programming languages can accommodate ___domain-driven design with the core object-oriented concepts.
 
AndNotSpecification(const ISpecification<T>& InLeft, const ISpecification<T>& InRight)
Alternative example, without the Specification Pattern:
: Left(InLeft),
Right(InRight) { }
 
virtual bool IsSatisfiedBy(T Candidate) const override
<source lang="csharp">
{
var InvoiceCollection = Service.GetInvoices();
return Left.IsSatisfiedBy(Candidate) && !Right.IsSatisfiedBy(Candidate);
foreach (var invoice in InvoiceCollection) invoice.SendToCollectionIfNecessary();
}
};
 
template <class T>
// Invoice methods:
class OrSpecification final : public CompositeSpecification<T>
public void SendToCollectionIfNecessary()
{
public:
if (ShouldSendToCollection()) SendToCollection();
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>
private bool ShouldSendToCollection() => currentInvoice.OverDue && currentInvoice.NoticeSent && !currentInvoice.InCollection;
ISpecification<T>* CompositeSpecification<T>::Or(const ISpecification<T>& Other) const
{
return new OrSpecification<T>(*this, Other);
}
 
template <class T>
</source>This alternative uses foundation concepts of get-only properties, condition logic, and functions. The key alternative here is Get-Only Properties, which are well-named to maintain the ___domain-driven language, and enable the continued use of the natural <code>&&</code> operator, instead of the Specification Pattern's <code>And()</code> function. Furthermore, the creation of a well-named function <code>SendToCollectionIfNecessary</code> is potentially more useful and descriptive, than the previous example (which could also be contained in such a function, except not directly on the object apparently).
ISpecification<T>* CompositeSpecification<T>::OrNot(const ISpecification<T>& Other) const
{
return new OrNotSpecification<T>(*this, Other);
}
 
template <class T>
ISpecification<T>* CompositeSpecification<T>::Not() const
{
return new NotSpecification<T>(*this);
}
 
</syntaxhighlight>
 
=== TypeScript ===
{{Further|TypeScript}}
<syntaxhighlight lang="typescript">
export interface ISpecification {
isSatisfiedBy(candidate: unknown): boolean;
and(other: ISpecification): ISpecification;
andNot(other: ISpecification): ISpecification;
or(other: ISpecification): ISpecification;
orNot(other: ISpecification): ISpecification;
not(): ISpecification;
}
 
export abstract class CompositeSpecification implements ISpecification {
abstract isSatisfiedBy(candidate: unknown): boolean;
 
and(other: ISpecification): ISpecification {
return new AndSpecification(this, other);
}
 
andNot(other: ISpecification): ISpecification {
return new AndNotSpecification(this, other);
}
 
or(other: ISpecification): ISpecification {
return new OrSpecification(this, other);
}
 
orNot(other: ISpecification): ISpecification {
return new OrNotSpecification(this, other);
}
 
not(): ISpecification {
return new NotSpecification(this);
}
}
 
export class AndSpecification extends CompositeSpecification {
constructor(private leftCondition: ISpecification, private rightCondition: ISpecification) {
super();
}
 
isSatisfiedBy(candidate: unknown): boolean {
return this.leftCondition.isSatisfiedBy(candidate) && this.rightCondition.isSatisfiedBy(candidate);
}
}
 
export class AndNotSpecification extends CompositeSpecification {
constructor(private leftCondition: ISpecification, private rightCondition: ISpecification) {
super();
}
 
isSatisfiedBy(candidate: unknown): boolean {
return this.leftCondition.isSatisfiedBy(candidate) && this.rightCondition.isSatisfiedBy(candidate) !== true;
}
}
 
export class OrSpecification extends CompositeSpecification {
constructor(private leftCondition: ISpecification, private rightCondition: ISpecification) {
super();
}
 
isSatisfiedBy(candidate: unknown): boolean {
return this.leftCondition.isSatisfiedBy(candidate) || this.rightCondition.isSatisfiedBy(candidate);
}
}
 
export class OrNotSpecification extends CompositeSpecification {
constructor(private leftCondition: ISpecification, private rightCondition: ISpecification) {
super();
}
 
isSatisfiedBy(candidate: unknown): boolean {
return this.leftCondition.isSatisfiedBy(candidate) || this.rightCondition.isSatisfiedBy(candidate) !== true;
}
}
 
export class NotSpecification extends CompositeSpecification {
constructor(private wrapped: ISpecification) {
super();
}
 
isSatisfiedBy(candidate: unknown): boolean {
return !this.wrapped.isSatisfiedBy(candidate);
}
}
</syntaxhighlight>
 
==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 <code>OverdueSpecification</code> class that is satisfied when an invoice's due date is 30 days or older, a <code>NoticeSentSpecification</code> class that is satisfied when three notices have been sent to the customer, and an <code>InCollectionSpecification</code> 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 <code>SendToCollection</code> 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.
 
<syntaxhighlight lang="csharp">
var overdue = new OverdueSpecification();
var noticeSent = new NoticeSentSpecification();
var inCollection = new InCollectionSpecification();
 
// Example of specification pattern logic chaining
var sendToCollection = overdue.And(noticeSent).And(inCollection.Not());
 
var invoices = InvoiceService.GetInvoices();
 
foreach (var invoice in invoices)
{
if (sendToCollection.IsSatisfiedBy(invoice))
{
invoice.SendToCollection();
}
}
</syntaxhighlight>
 
==References==
Line 290 ⟶ 553:
* [https://github.com/Happyr/Doctrine-Specification Happyr Doctrine Specification in PHP] by Happyr
* [https://github.com/neoneye/SpecificationPattern The Specification Pattern in Swift] by Simon Strandgaard
* [https://github.com/thiagodp/spec-pattern The Specification Pattern in TypeScript and JavaScript] by Thiago Delgado Pinto
* [https://web.archive.org/web/20110724151447/http://www.dpdk.nl/opensource/specification-pattern-for-selection-on-lists specification pattern in flash actionscript 3] by Rolf Vreijdenberger
 
{{Design Patterns Patternspatterns}}
 
[[Category:Architectural pattern (computer science)]]
[[Category:Software design patterns]]
[[Category:Programming language comparisons]]
<!-- Hidden categories below -->
[[Category:Articles with example C Sharp code]]
[[Category:Articles with example C++ code]]
[[Category:Articles with example JavaScript code]]
[[Category:Articles with example Python (programming language) code]]