zoukankan      html  css  js  c++  java
  • 【转】编写高质量代码改善C#程序的157个建议——建议147:重构多个相关属性为一个类

    建议147:重构多个相关属性为一个类

    若存在多个相关属性,就应该考虑是否将其重构为一个类。查看如下类:

        class Person
        {
            public string Address { get; set; }
            public string ZipCode { get; set; }
            public string Mobile { get; set; }
            public string Hotmail { get; set; }
            //其他省略
        }

    上面代码中的这四个属性全部跟联系方式有关,所以,我们应该重构一个Contact类型,代码如下所示:

        class Person
        {
            public Contact Contact { get; set; }
            //其他省略
        }
    
        class Contact
        {
            public string Address { get; set; }
            public string ZipCode { get; set; }
            public string Mobile { get; set; }
            public string Hotmail { get; set; }
        }

    记住,类型中的相关属性超过3个,就可以考虑将其重构为一个类了。

    转自:《编写高质量代码改善C#程序的157个建议》陆敏技

  • 相关阅读:
    [CF1198D] Rectangle Painting 1
    [CF696B] Puzzles
    [CF540D] Bad Luck Island
    [P1654] OSU!
    [P6154] 游走
    [CF1265E] Beautiful Mirrors
    [CF920F] SUM and REPLACE
    [CF453B] Little Pony and Harmony Chest
    [CF808D] Array Division
    [CF1155D] Beautiful Array
  • 原文地址:https://www.cnblogs.com/farmer-y/p/8022110.html
Copyright © 2011-2022 走看看