品味编程~底层开发人员应该这样设计一个字体类

品味编程,不是一味的根据书本上的知识讲,而是在实践开发过程中总结出来的,比较有感悟的东西,对于一个问题,可能有多种方法,但无论你使用哪种方式,请记住,一定要用代码的扩展性,可读性及健壮性上考虑,你写的代码是否合理,这需要你自己用心去体会,用心去设计,在代码设计上,请千万不要模棱两可!

就像下面的例子,都是设计一个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();

结果如图:

事实上,从例子2中,我们可以看到很多东西,它用到了类,结构,枚举,迭代等等,其中我们看到了FontStyle这个枚举它被标识了Flags特性,这说时,它可以支持位运算,本例中,提供了一个方法,它以迭代的方式返回位运算的结果集,这是一个亮点,我们通过这个方法,可以扩展到其它枚举类型如,用户权限设计(查看,删除,添加,更新),文件属性设计(只读,归档,隐藏),对于底层开发人员来说,需要将这些类型对应的方法进行开发,这样的设计对于表层开发人员是很友好的!

原文地址:https://www.cnblogs.com/lori/p/3218960.html