c#中策略模式的实现有些问题

本文关键字:问题 实现 策略 模式 | 更新日期: 2023-09-27 18:04:32

我正在尝试实现嵌套类的策略模式。

public class Restriction
{
    protected SpecificRestriction _specificRestriction;
    public void SetGreaterRestriction(decimal value)
    {
        Greater greaterRestriction = new Greater();
        greaterRestriction.GreaterValue = value;
        _specificRestriction = greaterRestriction;
    }
    public void SetLessRestriction(decimal value)
    {
        Less lessRestriction = new Less();
        lessRestriction.LessValue = value;
        _specificRestriction = lessRestriction;
    }
    public void SetRangeRestriction(decimal lessValue, decimal greaterValue)
    {
        Range r = new Range();
        r.GreaterValue= greaterValue;
        r.LessValue= lessValue;
        _specificRestriction = r;
    }
    public bool Eval(decimal Value2)
    { 
        return _specificRestriction.Eval(Value2);
    }

    /* Nested strategies classes */
    protected abstract class SpecificRestriction
    {     
        public abstract bool Eval(decimal Value);
    }
    protected class Less : SpecificRestriction
    {
        public decimal LessValue { get; set; }
        public override bool Eval(decimal lessValue)
        {
            return lessValue < LessValue ;
        }
    }
    protected class Greater : SpecificRestriction
    {
        public decimal GreaterValue { get; set; }
        public override bool Eval(decimal greaterValue)
        {
            return greaterValue > GreaterValue;
        }
    }
    protected class Range : SpecificRestriction
    {
        public decimal LessValue { get; set; }
        public decimal GreaterValue { get; set; }
        public override bool Eval(decimal mediumValue)
        {
            return LessValue <= mediumValue && mediumValue <= GreaterValue;
        }
    }
}

测试:

        Restriction r = new Restriction();
        r.SetLessRestriction(12);
        r.Eval(13)  // Return false   <- Works!
        r.Eval(11)  // Return True    <- Works!

        r.SetGreaterRestriction(12);
        r.Eval(13)     // Return True    <- Works!
        r.Eval(11)     // Return False   <- Works!
        r.SetRangeRestriction(12, 15);
        r.Eval(13)  // Return false     <- It does not works
        r.Eval(11)  // Return false     <- Works!
        r.Eval(16)  // Return false     <- Works!

为什么Range不工作?我在Range课上做错了什么吗?

c#中策略模式的实现有些问题

限制类需要一些改变,它工作

public class{

    public void SetGreaterRestriction(decimal value)
    {
        Greater greaterRestriction = new Greater();
        greaterRestriction.GreaterValue = value;
        _specificRestriction = greaterRestriction;
    }
    public void SetLessRestriction(decimal value)
    {
        Less lessRestriction = new Less();
        lessRestriction.LessValue = value;
        _specificRestriction = lessRestriction;
    }
    public void SetRangeRestriction(decimal lessValue, decimal greaterValue)
    {
        Range r = new Range();
        r.GreaterValue = greaterValue;
        r.LessValue = lessValue;
        _specificRestriction = r;
    }
    public bool Eval(decimal Value2)
    {
        return _specificRestriction.Eval(Value2);
    }

    /* Nested strategies classes */
    protected abstract class SpecificRestriction
    {
        public abstract bool Eval(decimal Value);
    }
    protected class Less : SpecificRestriction
    {
        public decimal LessValue { get; set; }
        public override bool Eval(decimal lessValue)
        {
            return lessValue < LessValue;
        }
    }
    protected class Greater : SpecificRestriction
    {
        public decimal GreaterValue { get; set; }
        public override bool Eval(decimal greaterValue)
        {
            return greaterValue > GreaterValue;
        }
    }
    protected class Range : SpecificRestriction
    {
        public decimal LessValue { get; set; }
        public decimal GreaterValue { get; set; }
        public override bool Eval(decimal mediumValue)
        {
            return LessValue <= mediumValue && mediumValue <= GreaterValue;
        }
    }
}

你的Range类没有覆盖Eval方法,它覆盖了Evaluar方法