Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Design Patterns by D.pdf
Скачиваний:
28
Добавлен:
19.02.2016
Размер:
2.57 Mб
Скачать

Specification (Specification)

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.

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.

Структура

Пример реализации

public interface ISpecification<TEntity>

{

bool IsSatisfiedBy(TEntity entity);

}

internal class AndSpecification<TEntity> : ISpecification<TEntity>

{

private ISpecification<TEntity> Spec1; private ISpecification<TEntity> Spec2;

internal AndSpecification(ISpecification<TEntity> s1, ISpecification<TEntity> s2)

{

Spec1 = s1; Spec2 = s2;

}

public bool IsSatisfiedBy(TEntity candidate)

{

return Spec1.IsSatisfiedBy(candidate) && Spec2.IsSatisfiedBy(candidate);

}

}

internal class OrSpecification<TEntity> : ISpecification<TEntity>

{

121

private ISpecification<TEntity> Spec1; private ISpecification<TEntity> Spec2;

internal OrSpecification(ISpecification<TEntity> s1, ISpecification<TEntity> s2)

{

Spec1 = s1; Spec2 = s2;

}

public bool IsSatisfiedBy(TEntity candidate)

{

return Spec1.IsSatisfiedBy(candidate) || Spec2.IsSatisfiedBy(candidate);

}

}

internal class NotSpecification<TEntity> : ISpecification<TEntity>

{

private ISpecification<TEntity> Wrapped;

internal NotSpecification(ISpecification<TEntity> x)

{

Wrapped = x;

}

public bool IsSatisfiedBy(TEntity candidate)

{

return !Wrapped.IsSatisfiedBy(candidate);

}

}

public static class ExtensionMethods

{

public static ISpecification<TEntity> And<TEntity>(this ISpecification<TEntity> s1, ISpecification<TEntity> s2)

{

return new AndSpecification<TEntity>(s1, s2);

}

public static ISpecification<TEntity> Or<TEntity>(this ISpecification<TEntity> s1, ISpecification<TEntity> s2)

{

return new OrSpecification<TEntity>(s1, s2);

}

public static ISpecification<TEntity> Not<TEntity>(this ISpecification<TEntity> s)

{

return new NotSpecification<TEntity>(s);

}

}

Пример использования

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.

122

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();

}

}

123