这(通用范围类)条件逻辑应该用多态性代替吗?如果是,那为什么? [英] Should this (Generic Range Class) Conditional logic be replaced with Polymorphism? If yes, then why?

查看:141
本文介绍了这(通用范围类)条件逻辑应该用多态性代替吗?如果是,那为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面显示的是一个通用范围类。这样做的目的是保存一个范围,然后当请求指定(布尔值)时,如果给定的值在范围内。



我已经阅读了多篇文章,博客等,说:用多态性取代条件



我的问题是,真的值得将代码分成多个类,每个类实际上只有一行代码。希望下面的代码能够显示我的意思。



这个类依赖于另外两个类,这里​​没有显示,但如果有人需要它,我可以稍后加入。

  namespace Common.Utilities 
{
public class GenericRange< T>
其中T:struct,IComparable< T>
{
#region属性
public T Min {get;私人设置; }
public T Max {get;私人设置; }
public GenericRangeType RangeType {get;私人设置; }
#endregion

#region构造函数
public GenericRange(T min,T max,GenericRangeType rangeType = GenericRangeType.Inclusive)
{
// Check参数
Min = min;
Max = max;
RangeType = rangeType;
}
#endregion

#region方法
#region私有
私有bool IsInclusive(T值)
{
返回值.IsGreaterThanOrEqualTo(Min)&& value.IsLessThanOrEqualTo(最大值);
}

private bool IsInclusiveMin(T值)
{
返回值.IsGreaterThanOrEqualTo(Min)&& value.IsLessThan(最大值);
}

private bool IsInclusiveMax(T值)
{
返回值.IsGreaterThan(Min)&& value.IsLessThanOrEqualTo(最大值);
}

private bool IsExclusive(T值)
{
返回值.IsGreaterThan(Min)&& value.IsLessThan(最大值);

#endregion

#region Public
public bool Contains(T value)
{
switch(RangeType)
{
case GenericRangeType.Inclusive:return IsInclusive(value);
case GenericRangeType.InclusiveMin:return IsInclusiveMin(value);
case GenericRangeType.InclusiveMax:return IsInclusiveMax(value);
case GenericRangeType.Exclusive:return IsExclusive(value);
default:throw new NotImplementedException();


$ b public override string ToString()
{
return String.Format(Min:{0},Max:{1} ,类型:{2},最小值,最大值,范围类型);
}
#endregion
#endregion
}
}

唯一的Public方法是:Contain和ToString。
如果我通过Polymorphism正确理解它,我应该为每个comparisson类型创建一个单独的具体类,然后使Contain成为一个虚拟方法。



主要的东西我想了解的是,这些好处/好处是什么?

如果这是这个问题的错误地方,那么我很抱歉。



编辑1:
如果有人需要它的附加代码:

  public static class ComparableExtensions 
{
public static bool IsEqualTo< T>(this T leftHand,T值)其中T:IComparable< T>
{
返回leftHand.CompareTo(value)== 0;
}

public static bool IsGreaterThan< T>(this T leftHand,T value)其中T:IComparable< T>
{
返回leftHand.CompareTo(value)> 0;
}
public static bool IsGreaterThanOrEqualTo< T>(this T leftHand,T value)其中T:IComparable< T>
{
返回leftHand.CompareTo(value)> = 0;
}

public static bool IsLessThan< T>(this T leftHand,T value)其中T:IComparable< T>
{
返回leftHand.CompareTo(value)< 0;
}
public static bool IsLessThanOrEqualTo< T>(this T leftHand,T value)其中T:IComparable< T>
{
返回leftHand.CompareTo(value)< = 0;


$ b $ public enum GenericRangeType
{
包含,
独占,
InclusiveMin,
InclusiveMax


解决方案

您扩展Contains而不更改现有代码。在这种情况下,它没有太大意义,因为您已经涵盖了这里的所有包含的基础,但是在其他情况下,可扩展性可能非常有用。


Shown below is a Generic Range class. The purpose of this is to save a Range and then later when requested specify (boolean) if a given value is in the range.

I have read multiple posts, questions, blogs etc. that say "Replace Conditional with Polymorphism"

My question, is it really worth separating the code out into multiple classes where each class would literally have one line of code. Hopefully the code below will show what I mean.

The class is dependent on two more classes, which are not shown here, but if anyone needs it I can include it later.

namespace Common.Utilities
{
    public class GenericRange<T>
       where T : struct, IComparable<T>
   {
      #region Properties
      public T Min { get; private set; }
      public T Max { get; private set; }
      public GenericRangeType RangeType { get; private set; }
      #endregion

      #region Constructors
      public GenericRange(T min, T max, GenericRangeType rangeType = GenericRangeType.Inclusive)
      {
         // Check Parameters
         Min = min;
         Max = max;
         RangeType = rangeType;
      }
      #endregion

      #region Methods
      #region Private
      private bool IsInclusive(T value)
      {
         return value.IsGreaterThanOrEqualTo(Min) && value.IsLessThanOrEqualTo(Max);
      }

      private bool IsInclusiveMin(T value)
      {
         return value.IsGreaterThanOrEqualTo(Min) && value.IsLessThan(Max);
      }

      private bool IsInclusiveMax(T value)
      {
         return value.IsGreaterThan(Min) && value.IsLessThanOrEqualTo(Max);
      }

      private bool IsExclusive(T value)
      {
         return value.IsGreaterThan(Min) && value.IsLessThan(Max);
      }
      #endregion

      #region Public
      public bool Contains(T value)
      {
         switch (RangeType)
         {
            case GenericRangeType.Inclusive: return IsInclusive(value);
            case GenericRangeType.InclusiveMin: return IsInclusiveMin(value);
            case GenericRangeType.InclusiveMax: return IsInclusiveMax(value);
            case GenericRangeType.Exclusive: return IsExclusive(value);
            default: throw new NotImplementedException();
         }
      }

      public override string ToString()
      {
         return String.Format("Min: {0}, Max: {1}, Type: {2}", Min, Max, RangeType);
      }
      #endregion
      #endregion
    }
}

The only Public methods are: Contain and ToString. If I understand it correctly through Polymorphism I should create a separate concrete class for each of the comparisson types and then make Contain a virtual method.

The main thing I am trying to understand is, what would the benefits/advantages be?

If this is the wrong place for this question, then I am sorry. Let me know and I will move it.

EDIT 1: The additional code to make this complete if anyone needs it:

public static class ComparableExtensions
{
    public static bool IsEqualTo<T>(this T leftHand, T value) where T : IComparable<T>
    {
        return leftHand.CompareTo(value) == 0;
   }

    public static bool IsGreaterThan<T>(this T leftHand, T value) where T : IComparable<T>
    {
        return leftHand.CompareTo(value) > 0;
    }
    public static bool IsGreaterThanOrEqualTo<T>(this T leftHand, T value) where T : IComparable<T>
    {
        return leftHand.CompareTo(value) >= 0;
    }

    public static bool IsLessThan<T>(this T leftHand, T value) where T : IComparable<T>
    {
        return leftHand.CompareTo(value) < 0;
    }
    public static bool IsLessThanOrEqualTo<T>(this T leftHand, T value) where T : IComparable<T>
    {
        return leftHand.CompareTo(value) <= 0;
    }
}   

public enum GenericRangeType
{
    Inclusive,
    Exclusive,
    InclusiveMin,
    InclusiveMax
}

解决方案

Breaking it out to different classes lets you extend Contains without changing the existing code. In this case it doesn't make too much sense as you have covered all the bases of contains here, but in other cases that extensibility can be very useful.

这篇关于这(通用范围类)条件逻辑应该用多态性代替吗?如果是,那为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆