+ -
当前位置:首页 → 问答吧 → 帮忙去掉重复代码

帮忙去掉重复代码

时间:2011-12-06

来源:互联网

PriorityPaired和RefreshStatePaired都是继承自PairedBase。如题,去掉重复代码。实在没分,不好意思。

C# code

    public class PriorityDic
    {
        private static PairedBase _paired = new PriorityPaired();

        public static string GetValue(string key)
        {
            return _paired.GetValue(key);
        }

        public static string GetKey(string value)
        {
            return _paired.GetKey(value);
        }

        public static bool ContainsKey(string key)
        {
            return _paired.ContainsKey(key);
        }

        public static bool ContainsValue(string value)
        {
            return _paired.ContainsValue(value);
        }
    }

    public class RefreshStateDic
    {
        private static PairedBase _paired = new RefreshStatePaired();

        public static string GetValue(string key)
        {
            return _paired.GetValue(key);
        }

        public static string GetKey(string value)
        {
            return _paired.GetKey(value);
        }

        public static bool ContainsKey(string key)
        {
            return _paired.ContainsKey(key);
        }

        public static bool ContainsValue(string value)
        {
            return _paired.ContainsValue(value);
        }
    }


作者: wxssaa   发布时间: 2011-12-06

用接口..interface

作者: enter89   发布时间: 2011-12-06

叫重构吧,兄弟
看不明白你的代码目的

作者: jiuhexuan   发布时间: 2011-12-06

发现静态的方法不能override,于是写了个非静态的PairedBase类,继承PairedBase的类可以添加自动以的字典。

想静态使用GetValue()和GetKey()方法,于是又包了一层PriorityDic。我把全部代码贴过来吧,就是长点。

[code=C#][/code]
  public abstract class PairedBase
  {
  protected Dictionary<string, string> _dictionary = new Dictionary<string, string>();

  protected PairedBase()
  {
  Initialize();
  }

  protected abstract void Initialize();

  public string GetValue(string key)
  {
  return _dictionary.ContainsKey(key) ? _dictionary[key] : string.Empty;
  }

  public string GetKey(string value)
  {
  if (!_dictionary.ContainsValue(value))
  return string.Empty;

  var item = _dictionary.Where(c => c.Value == value);

  return item.Count() > 0 ? item.FirstOrDefault().Key : string.Empty;
  }

  public bool ContainsKey(string key)
  {
  return _dictionary.ContainsKey(key);
  }

  public bool ContainsValue(string value)
  {
  return _dictionary.ContainsValue(value);
  }
  }

  public class PriorityPaired : PairedBase
  {
  protected override void Initialize()
  {
  _dictionary.Add("高", "1");
  _dictionary.Add("中", "2");
  _dictionary.Add("低", "3");
  }
  }

  public class RefreshStatePaired : PairedBase
  {
  protected override void Initialize()
  {
  _dictionary.Add("发生更新", "1");
  _dictionary.Add("正则表达式失效", "2");
  _dictionary.Add("网站响应超时", "3");
  _dictionary.Add("网站出现异常", "4");
  _dictionary.Add("数据库信息有误", "5");
  }
  }

  public class SoftStatePaired : PairedBase
  {
  protected override void Initialize()
  {
  _dictionary.Add("自动刷新", "1");
  _dictionary.Add("手动刷新", "2");
  _dictionary.Add("不需要刷新", "3");
  _dictionary.Add("软件停运", "4");
  }
  }

  public class PriorityDic
  {
  private static PairedBase _paired = new PriorityPaired();

  public static string GetValue(string key)
  {
  return _paired.GetValue(key);
  }

  public static string GetKey(string value)
  {
  return _paired.GetKey(value);
  }

  public static bool ContainsKey(string key)
  {
  return _paired.ContainsKey(key);
  }

  public static bool ContainsValue(string value)
  {
  return _paired.ContainsValue(value);
  }
  }

  public class RefreshStateDic
  {
  private static PairedBase _paired = new RefreshStatePaired();

  public static string GetValue(string key)
  {
  return _paired.GetValue(key);
  }

  public static string GetKey(string value)
  {
  return _paired.GetKey(value);
  }

  public static bool ContainsKey(string key)
  {
  return _paired.ContainsKey(key);
  }

  public static bool ContainsValue(string value)
  {
  return _paired.ContainsValue(value);
  }
  }

  public class SoftStateDic
  {
  private static PairedBase _paired = new SoftStatePaired();

  public static string GetValue(string key)
  {
  return _paired.GetValue(key);
  }

  public static string GetKey(string value)
  {
  return _paired.GetKey(value);
  }

  public static bool ContainsKey(string key)
  {
  return _paired.ContainsKey(key);
  }

  public static bool ContainsValue(string value)
  {
  return _paired.ContainsValue(value);
  }
  }
引用 2 楼 jiuhexuan 的回复:

叫重构吧,兄弟
看不明白你的代码目的

作者: wxssaa   发布时间: 2011-12-06

重贴一下
C# code

    public abstract class PairedBase
    {
        protected Dictionary<string, string> _dictionary = new Dictionary<string, string>();

        protected PairedBase()
        {
            Initialize();
        }

        protected abstract void Initialize();

        public string GetValue(string key)
        {
            return _dictionary.ContainsKey(key) ? _dictionary[key] : string.Empty;
        }

        public string GetKey(string value)
        {
            if (!_dictionary.ContainsValue(value))
                return string.Empty;

            var item = _dictionary.Where(c => c.Value == value);

            return item.Count() > 0 ? item.FirstOrDefault().Key : string.Empty;
        }

        public bool ContainsKey(string key)
        {
            return _dictionary.ContainsKey(key);
        }

        public bool ContainsValue(string value)
        {
            return _dictionary.ContainsValue(value);
        }
    }

    public class PriorityPaired : PairedBase
    {
        protected override void Initialize()
        {
            _dictionary.Add("", "1");
            _dictionary.Add("", "2");
            _dictionary.Add("", "3");
        }
    }

    public class RefreshStatePaired : PairedBase
    {
        protected override void Initialize()
        {
            _dictionary.Add("发生更新", "1");
            _dictionary.Add("正则表达式失效", "2");
            _dictionary.Add("网站响应超时", "3");
            _dictionary.Add("网站出现异常", "4");
            _dictionary.Add("数据库信息有误", "5");
        }
    }

    public class PriorityDic
    {
        private static PairedBase _paired = new PriorityPaired();

        public static string GetValue(string key)
        {
            return _paired.GetValue(key);
        }

        public static string GetKey(string value)
        {
            return _paired.GetKey(value);
        }

        public static bool ContainsKey(string key)
        {
            return _paired.ContainsKey(key);
        }

        public static bool ContainsValue(string value)
        {
            return _paired.ContainsValue(value);
        }
    }

    public class RefreshStateDic
    {
        private static PairedBase _paired = new RefreshStatePaired();

        public static string GetValue(string key)
        {
            return _paired.GetValue(key);
        }

        public static string GetKey(string value)
        {
            return _paired.GetKey(value);
        }

        public static bool ContainsKey(string key)
        {
            return _paired.ContainsKey(key);
        }

        public static bool ContainsValue(string value)
        {
            return _paired.ContainsValue(value);
        }
    }

作者: wxssaa   发布时间: 2011-12-06