简体   繁体   English

在C#中实施Strategy Pattern时出错

[英]Something it is wrong in implementation of Strategy Pattern in c#

I am trying implement a Strategy pattern with nested classes. 我正在尝试使用嵌套类实现策略模式。

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;
        }
    }
}

Testing: 测试:

        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!

Why Range it does not works? 为什么范围不起作用? Am I doing something wrong in Range class? 我在Range课上做错了吗?

There is some change required in Restriction Class and it works 限制类中需要进行一些更改,并且该更改有效

public class Restriction { protected SpecificRestriction _specificRestriction; 公共类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; } } } 

您的Range类不会覆盖Eval方法,而是覆盖Evaluar方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM