Specification pattern

Specification pattern

In computer programming, the specification pattern is a particular software design pattern, whereby business logic can be recombined by chaining the business logic together using boolean logic.

A Specification pattern outlines a unit of business logic that is combinable with other business logic units. 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.

Diagram

+-----------------+
<>
ISpecification
-----------------
-----------------| +-----------------------+
And() | |Composite Specification
IsSatisifiedBy() |<>------|
Not() | |-----------------------
Or() | |-----------------------
+-----------------+ |And()
IsSatisifiedBy()
+-------------|>|Not() |<|------+
|Or() |
+-----------------------+
.
/_
|
|
|
|
+-----------+---------+ +---------------+-----+ +---------------------+
AndSpecification | | OrSpecification | | NotSpecification
---------------------| |---------------------| |---------------------
ISpecification: One | |ISpecification: One | |ISpecification:
ISpecification: Other| |ISpecification: Other| | Wrapped
---------------------| |---------------------| |---------------------
AndSpecification() | |OrSpecification() | |NotSpecification()
IsSatisfiedBy() | |IsSatisfiedBy() | |IsSatisfiedBy()
+---------------------+ +---------------------+ +---------------------+

Code Examples


= C# =

public interface ISpecification { bool IsSatisfiedBy(object candidate);

ISpecification And(ISpecification other);

ISpecification Or(ISpecification other);

ISpecification Not(); }

public class CompositeSpecification : ISpecification { public virtual bool IsSatisfiedBy(object candidate) { throw new Exception("The method or operation is not implemented."); }

public ISpecification And(ISpecification other) { return new AndSpecification(this, other); }

public ISpecification Or(ISpecification other) { return new OrSpecification(this, other); }

public ISpecification Not() { return new NotSpecification(this); } }

public class AndSpecification : CompositeSpecification { private ISpecification One; private ISpecification Other;

public AndSpecification(ISpecification x, ISpecification y) { One = x; Other = y; }

public override bool IsSatisfiedBy(object candidate) { return One.IsSatisfiedBy(candidate) && Other.IsSatisfiedBy(candidate); } }

public class OrSpecification : CompositeSpecification { private ISpecification One; private ISpecification Other;

public OrSpecification(ISpecification x, ISpecification y) { One = x; Other = y; }

public override bool IsSatisfiedBy(object candidate) { return One.IsSatisfiedBy(candidate) || Other.IsSatisfiedBy(candidate); } }

public class NotSpecification : CompositeSpecification { private ISpecification Wrapped;

public NotSpecification(ISpecification x) { Wrapped = x; }

public override bool IsSatisfiedBy(object candidate) { return !Wrapped.IsSatisfiedBy(candidate); } }

Example of use

In this example, we are retrieving invoices and sending them to a collection agency if they are overdue, notices have been sent and they are not already with the collection agency.

We previously defined an OverdueSpecification class that it 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.

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.

OverDueSpecification OverDue = new OverDueSpecification();NoticeSentSpecification NoticeSent = new NoticeSentSpecification();InCollectionSpecification InCollection = new InCollectionSpecification();

ISpecification SendToCollection = OverDue.And(NoticeSent).And(InCollection.Not()));

InvoiceCollection = Service.GetInvoices();

foreach (Invoice currentInvoice in InvoiceCollection){ if (SendToCollection.IsSatisfiedBy(currentInvoice)) { currentInvoice.SendToCollection();

References

Evans, E: "Domain-Driven Design.", page 224. Addison-Wesley, 2004.

External links

* [http://www.martinfowler.com/apsupp/spec.pdf Specifications] by Eric Evans and Martin Fowler
* [http://www.mattberther.com/2005/03/25/the-specification-pattern-a-primer/ The Specification Pattern: A Primer] by Matt Berther
* [http://www.dpdk.nl/opensource/specification-pattern-for-selection-on-lists specification pattern in flash actionscript 3] by Rolf Vreijdenberger


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Pattern formation — The science of pattern formation deals with the visible, (statistically) orderly outcomes of self organisation and the common principles behind similar patterns. In developmental biology, pattern formation refers to the generation of complex… …   Wikipedia

  • Behavioral pattern — In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.… …   Wikipedia

  • Regional specification — In the field of developmental biology, regional specification is the process by which different areas are identified in the development of the early embryo. The process by which the cells become specified differs between organisms. Contents 1… …   Wikipedia

  • Software design pattern — In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into code. It is a… …   Wikipedia

  • Canadian Military Pattern truck — The Canadian Military Pattern (CMP) truck was a class of military truck made in large quantities in Canada during World War II to British Army specifications for use in the armies of the British Commonwealth allies. CMP trucks were also sent to… …   Wikipedia

  • Design pattern (computer science) — In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for …   Wikipedia

  • Java Portlet Specification — The Java Portlet Specification defines a contract between the portlet container and portlets and provides a convenient programming model for portlet developers. JSR 168 The Java Portlet Specification V1.0 was developed under the Java Community… …   Wikipedia

  • 1908 and 1912 Pattern British Army Cavalry Swords — The 1908 Pattern Cavalry Trooper s Sword (and the 1912 Pattern, the equivalent for officers) was the last service sword issued to the cavalry of the British Army. It is widely considered [ cite book |last=Robson |first=Brian |title=Swords of the… …   Wikipedia

  • Singleton Pattern — Das Singleton (auch Einzelstück genannt) ist ein in der Softwareentwicklung eingesetztes Entwurfsmuster und gehört zur Kategorie der Erzeugungsmuster (engl. Creational Patterns). Es verhindert, dass von einer Klasse mehr als ein Objekt erzeugt… …   Deutsch Wikipedia

  • Abstract factory pattern — The abstract factory pattern is a software design pattern that provides a way to encapsulate a group of individual factories that have a common theme. In normal usage, the client software creates a concrete implementation of the abstract factory… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”