Specification pattern: Difference between revisions

Content deleted Content added
Add external link
m change source to syntaxhighlight
Line 9:
=== [[C Sharp (programming language)|C#]] ===
 
<sourcesyntaxhighlight lang="csharp">
public interface ISpecification
{
Line 133:
}
 
</syntaxhighlight>
</source>
 
=== [[C Sharp (programming language)|C# 6.0]] with generics ===
 
<sourcesyntaxhighlight lang="csharp">
public interface ISpecification<T>
{
Line 225:
public override bool IsSatisfiedBy(T candidate) => !other.IsSatisfiedBy(candidate);
}
</syntaxhighlight>
</source>
 
=== [[Python (programming language)|Python]] ===
<sourcesyntaxhighlight lang="python">
from abc import abstractmethod
from dataclasses import dataclass
Line 270:
return not self.subject.is_satisfied_by(candidate)
 
</syntaxhighlight>
</source>
 
==Example of use==
Line 286:
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.
 
<sourcesyntaxhighlight lang="csharp">
var OverDue = new OverDueSpecification();
var NoticeSent = new NoticeSentSpecification();
Line 301:
}
}
</syntaxhighlight>
</source>
 
== Criticisms ==
Line 312:
Alternative example, without the Specification Pattern:
 
<sourcesyntaxhighlight lang="csharp">
var InvoiceCollection = Service.GetInvoices();
foreach (var invoice in InvoiceCollection) invoice.SendToCollectionIfNecessary();
Line 324:
private bool ShouldSendToCollection() => currentInvoice.OverDue && currentInvoice.NoticeSent && !currentInvoice.InCollection;
 
</syntaxhighlight>
</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).
 
==References==