帮忙去掉重复代码

wxssaa 2011-12-06 04:29:21
PriorityPaired和RefreshStatePaired都是继承自PairedBase。如题,去掉重复代码。实在没分,不好意思。


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

...全文
157 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wxssaa 2011-12-07
  • 打赏
  • 举报
回复
太感谢了,就是这样的,结贴了!!!

[Quote=引用 7 楼 k0mmdu 的回复:]

C# code

public class Dic<T> where T : PairedBase, new()
{
private static PairedBase _paired = new T();
public static string GetValue(string key)
{
return……
[/Quote]
k0mmDu 2011-12-07
  • 打赏
  • 举报
回复
楼主参考下是不是要这个效果
k0mmDu 2011-12-07
  • 打赏
  • 举报
回复

public class Dic<T> where T : PairedBase, new()
{
private static PairedBase _paired = new T();
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-07
  • 打赏
  • 举报
回复
那请问有什么别的办法,把那些重复的代码去掉吗。如果不去掉,以后添加新的字典,又有一堆重复代码。

[Quote=引用 5 楼 mking0412 的回复:]

你的这个代码用接口处理灰常灰常适合!

但是
接口不能解决你的代码量和你要求的去除看似‘重复的代码’的问题
[/Quote]
MKing0412 2011-12-07
  • 打赏
  • 举报
回复
你的这个代码用接口处理灰常灰常适合!

但是
接口不能解决你的代码量和你要求的去除看似‘重复的代码’的问题
wxssaa 2011-12-06
  • 打赏
  • 举报
回复
重贴一下

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
  • 打赏
  • 举报
回复
发现静态的方法不能override,于是写了个非静态的PairedBase类,继承PairedBase的类可以添加自动以的字典。

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


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);
}
}
[Quote=引用 2 楼 jiuhexuan 的回复:]

叫重构吧,兄弟
看不明白你的代码目的
[/Quote]
jiuhexuan 2011-12-06
  • 打赏
  • 举报
回复
叫重构吧,兄弟
看不明白你的代码目的
Waldenz 2011-12-06
  • 打赏
  • 举报
回复
用接口..interface

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧