Specification pattern: Difference between revisions

Content deleted Content added
Fix C# 3.0 simplified with generics, CompositeSpecification.AndNot method body
Clarity and implementation of AndNot / OrNot for both versions (with & without generics)
Line 12:
bool IsSatisfiedBy(object candidate);
ISpecification And(ISpecification other);
ISpecification AndNot(ISpecification other);
ISpecification Or(ISpecification other);
ISpecification OrNot(ISpecification other);
ISpecification Not();
}
Line 23 ⟶ 25:
{
return new AndSpecification(this, other);
}
 
public ISpecification AndNot(ISpecification other)
{
return new AndNotSpecification(this, other);
}
 
Line 28 ⟶ 35:
{
return new OrSpecification(this, other);
}
 
public ISpecification OrNot(ISpecification other)
{
return new OrNotSpecification(this, other);
}
 
Line 38 ⟶ 50:
public class AndSpecification : CompositeSpecification
{
private ISpecification OneleftCondition;
private ISpecification OtherrightCondition;
 
public AndSpecification(ISpecification xleft, ISpecification yright)
{
OneleftCondition = xleft;
OtherrightCondition = yright;
}
 
public override bool IsSatisfiedBy(object candidate)
{
return OneleftCondition.IsSatisfiedBy(candidate) && OtherrightCondition.IsSatisfiedBy(candidate);
}
}
 
public class AndNotSpecification : 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) != true;
}
}
Line 55 ⟶ 84:
public class OrSpecification : CompositeSpecification
{
private ISpecification OneleftCondition;
private ISpecification OtherrightCondition;
 
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 OrSpecification(ISpecification xleft, ISpecification yright)
{
OneleftCondition = xleft;
OtherrightCondition = yright;
}
 
public override bool IsSatisfiedBy(object candidate)
{
return OneleftCondition.IsSatisfiedBy(candidate) || OtherrightCondition.IsSatisfiedBy(candidate) != true;
}
}
Line 96 ⟶ 142:
ISpecification<T> AndNot(ISpecification<T> other);
ISpecification<T> Or(ISpecification<T> other);
ISpecification<T> OrNot(ISpecification<T> other);
ISpecification<T> Not();
}
Line 184 ⟶ 231:
{
return left.IsSatisfiedBy(candidate) || right.IsSatisfiedBy(candidate);
}
}
public class OrNotSpecification<T> : CompositeSpecification<T>
{
private readonly ISpecification<T> left;
private readonly ISpecification<T> right;
 
public OrNotSpecification(ISpecification<T> left, ISpecification<T> right)
{
this.left = left;
this.right = right;
}
 
public override bool IsSatisfiedBy(T candidate)
{
return left.IsSatisfiedBy(candidate) || right.IsSatisfiedBy(candidate) != true;
}
}