趣味编程:C#中Specification模式的实现
2010-09-30 20:58:58 来源:WEB开发网CompositeSpecification提供了构建复合Specification的基础逻辑,它提供了And、Or和Not方法的实现,让其他Specification类只需要专注于IsSatisfiedBy方法的实现即可。例如,以下便是三种逻辑组合的具体实现:
public class AndSpecification<T> : CompositeSpecification<T>
{
private ISpecification<T> m_one;
private ISpecification<T> m_other;
public AndSpecification(ISpecification<T> x, ISpecification<T> y)
{
m_one = x;
m_other = y;
}
public override bool IsSatisfiedBy(T candidate)
{
return m_one.IsSatisfiedBy(candidate) && m_other.IsSatisfiedBy(candidate);
}
}
public class OrSpecification<T> : CompositeSpecification<T>
{
private ISpecification<T> m_one;
private ISpecification<T> m_other;
public OrSpecification(ISpecification<T> x, ISpecification<T> y)
{
m_one = x;
m_other = y;
}
public override bool IsSatisfiedBy(T candidate)
{
return m_one.IsSatisfiedBy(candidate) || m_other.IsSatisfiedBy(candidate);
}
}
public class NotSpecification<T> : CompositeSpecification<T>
{
private ISpecification<T> m_wrapped;
public NotSpecification(ISpecification<T> x)
{
m_wrapped = x;
}
public override bool IsSatisfiedBy(T candidate)
{
return !m_wrapped.IsSatisfiedBy(candidate);
}
}
Tags:趣味 编程 Specification
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接