我想将Quine-McCluskey 算法与Composite Specification Pattern结合起来。
首先,让我们考虑一下基础知识,即 C# 中的规范模式:
public interface ISpecification
{
bool IsSatisfiedBy(object candidate);
ISpecification And(ISpecification other);
ISpecification Or(ISpecification other);
ISpecification Not();
}
public abstract class CompositeSpecification : ISpecification
{
public abstract bool IsSatisfiedBy(object candidate);
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);
}
}
现在假设我有一个 MacroSpecification 和一些 MicroSpecification:
public class MacroSpecification : CompositeSpecification
{
MicroSpecification1 Spec1 = new MicroSpecification1();
MicroSpecification2 Spec2 = new MicroSpecification2();
MicroSpecification3 Spec3 = new MicroSpecification3();
MicroSpecification4 Spec4 = new MicroSpecification4();
public override bool IsSatisfiedBy (object candidate)
{
if (candidate is int)
{
return Spec1.And(Spec2).And(Spec3).And(Spec4).IsSatisfiedBy(candidate);
}
}
}
public class MicroSpecification1 : CompositeSpecification
{
public override bool IsSatisfiedBy (object candidate)
{
if (candidate is int)
{
return (candidate > 100);
}
return false;
}
}
public class MicroSpecification2 : CompositeSpecification
{
public override bool IsSatisfiedBy (object candidate)
{
if (candidate is int)
{
return (candidate > 300);
}
return false;
}
}
public class MicroSpecification3 : CompositeSpecification
{
public override bool IsSatisfiedBy (object candidate)
{
if (candidate is int)
{
return (candidate < 1000000);
}
return false;
}
}
public class MicroSpecification4 : CompositeSpecification
{
public override bool IsSatisfiedBy (object candidate)
{
if (candidate is int)
{
return (candidate < 500000);
}
return false;
}
}
对于简化的、等效的 MacroSpecification IsSatisfiedBy 方法,我真的只需要 MicroSpecification4 和 MicroSpecification2。
所以我的问题是,有没有办法使用 Quine-McCluskey 将宏规范 IsSatisfiedBy 语句从四个规范简化为两个规范(或等效的东西)?
一般来说,必须给复合规范模式(并且不失一般性)模式设计提供某种“智能”会很好。