品味编程,不是一味的根据书本上的知识讲,而是在实践开发过程中总结出来的,比较有感悟的东西,对于一个问题,可能有多种方法,但无论你使用哪种方式,请记住,一定要用代码的扩展性,可读性及健壮性上考虑,你写的代码是否合理,这需要你自己用心去体会,用心去设计,在代码设计上,请千万不要模棱两可!
就像下面的例子,都是设计一个Font类,可两种结果却完全不同
例子1:
class Font { /// <summary> /// 大小 /// </summary> public int Size { get; set; } /// <summary> /// 字体大小单位 /// </summary> public string Unit{get;set;} /// <summary> /// 字体名称 /// </summary> public string Name { get; set; } /// <summary> /// 样式 /// </summary> public string Style { get; set; } }
例子2:比较注重代码可读性,扩展性及合理性,虽然代码长度增加了,但并不啰嗦,对于上层的开发人员来说,是友好的。
/// <summary> /// 字体样式枚举 /// </summary> [Flags] enum FontStyle { Bold = 1, Italic = 2, Regular = 0, Strikeout = 8, Underline = 4 } /// <summary> /// 单位 /// </summary> enum Unit { Pixed, Point } /// <summary> /// 字体大小 /// </summary> struct FontSize { public FontSize(int size, Unit unit) : this() { Size = size; Unit = unit; } /// <summary> /// 大小 /// </summary> public int Size { get; set; } /// <summary> /// 单位 /// </summary> public Unit Unit { get; set; } } /// <summary> /// 字体类 /// </summary> class Font { /// <summary> /// 大小 /// </summary> public FontSize Size { get; set; } /// <summary> /// 字体名称 /// </summary> public string Name { get; set; } /// <summary> /// 样式 /// </summary> public FontStyle Style { get; set; } }
提供一下输出字体样式的方法
/// <summary> /// 根据样式枚举值,得到它所包括的样式名称 /// </summary> /// <param name="fontStyle"></param> /// <returns></returns> static IEnumerable<string> GetFontStyle(FontStyle fontStyle) { if ((fontStyle & FontStyle.Bold) == FontStyle.Bold) yield return "粗体"; if ((fontStyle & FontStyle.Italic) == FontStyle.Italic) yield return "协体"; if ((fontStyle & FontStyle.Strikeout) == FontStyle.Strikeout) yield return "删除线"; if ((fontStyle & FontStyle.Underline) == FontStyle.Underline) yield return "下画线"; if (fontStyle == FontStyle.Regular) yield return "正常"; }
OK,进行测试一下
Font font = new Font { Name = "宋体", Size = new FontSize(12, Unit.Pixed), Style = FontStyle.Bold | FontStyle.Italic }; Console.WriteLine("字体名称:{0}", font.Name); Console.WriteLine("字体大小和单位:{0} {1}", font.Size.Size, font.Size.Unit); Console.Write("字体样式:"); GetFontStyle(font.Style).ToList().ForEach(i => Console.Write(i)); Console.ReadKey();
结果如图:
QQu/yIJT+EUWnNq/yELzxPxFFsYWlff0ECp5f5ENsV7WWFzWxGOt9ovswYpjjidtTtSdOll/9ijneW/FHjqG4Cf0/EOlYwiOb290T1mO/d1rqVsxNiB1Kx0460/HEJIUejpaTNAT9AQ9QU/QS6CP+7/dIiPz2VKqSCTDlFJVVVVIIpmkEPQpNrW2ttqdjhJqIhvqxce3UlWs50tKILlBn6KLb3Shbw1LqZWeYjpfUgKJQd+KlhBBAwLPsBK/6gSVxhCj+ZISSzz0mPqej9wzjmMFt1aSxeAGJVK+zZeUcALo9QhgHtU0iU+W/C2l1SIXMlTk8yUlooQ9vdKzlGDtWZmV3fizDwDfl548oynNl5S4knyRBcnTsD1nIxOeG8duvTsumOhKA3pK8EkpOfRy4u0VlCDQy6yYhsjeMRI6Jej7g/ReWUoSMCaF4/82kJAXXdTk+EY4X1JiiX2RxYtvrgqBamQhmuOlEFz1E0CO3N4jQZ+sivDHKTt/bkmRESlZD/ZLO2RuXaToQu8ZFlmZuE8aRQV69qeQth0jYKB3WzkSp57cQskHqTRfUsIpcugdM7QkCDLTC04MnaqZXnXkjl1gWpESS9Ha3uBb6UGPjIBkzrGmo0d7DZMSV47Q4/+y1oPeTZ7xY7qLiNF8SQmnCI8W+5/pYwcZQW+K4nWeXqlm7CIrrUD7LSI+KRWv8/RKNVXHg9/cO4aVt9WeLylRFJfz9Ko1MfUlI1SKrLo9I+iTT/6fp9eoqVTfPkK39aC6vDXGTEpE+XyeXh5NtXdMNUkWV7rknXrzxcjxE8YH9xxPVAbJdxR5qDjIn/P0EkrkfbW6IGuv5hZK3gp/6TbfKMpx+syD7F3eJIpTiPWnEUPF6z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4EfQk40TQk4wTQU8yTgQ9yTgR9CTjRNCTjBNBTzJOBD3JOBH0JONE0JOME0FPMk4EPck4EfQk40TQk4wTQU8yTgQ9yTgR9CTjRNCTjBNBTzJOBD3JOBH0JNP0fx9E/1qigMBeAAAAAElFTkSuQmCC" alt="" />
事实上,从例子2中,我们可以看到很多东西,它用到了类,结构,枚举,迭代等等,其中我们看到了FontStyle这个枚举它被标识了Flags特性,这说时,它可以支持位运算,本例中,提供了一个方法,它以迭代的方式返回位运算的结果集,这是一个亮点,我们通过这个方法,可以扩展到其它枚举类型如,用户权限设计(查看,删除,添加,更新),文件属性设计(只读,归档,隐藏),对于底层开发人员来说,需要将这些类型对应的方法进行开发,这样的设计对于表层开发人员是很友好的!