• 代码修改的一个范例


    所做的是系统中的一个模块A,为了程序的扩展性,使用了3层结构,如下图所示:

    这样是比较容易扩展和修改的结构,数据的展现和数据的持久化相互不会影响,并把业务逻辑封装在一起。
    模型代码为:
     1 using System;
     2 
     3 namespace Example
     4 {
     5     /**//// <summary>
     6     /// 示例
     7     /// </summary>

     8     class Example
     9     {
    10        /**//// <summary>
    11        /// 应用程序的主入口点。
    12        /// </summary>

    13        [STAThread]
    14        static void Main(string[] args)
    15        {
    16            PageView view = new PageView() ;
    17            view.Show() ;
    18        }

    19        #region 20050712
    20        /// <summary>
    21        /// 对象
    22        /// </summary>

    23        public class Person
    24        {
    25            private string name = String.Empty ;
    26            private string telephone = String.Empty ;
    27
    28            public Person() {}
    29
    30            public string Name
    31            {
    32                set{ name = value ; }
    33                getreturn name ; }
    34            }

    35            public string Telephone
    36            {
    37                set{ telephone = value ; }
    38                getreturn telephone ; }
    39            }

    40        }

    41        /// <summary>
    42        /// 界面展现层
    43        /// </summary>

    44        public class PageView
    45        {
    46            public PageView() {}
    47            
    48            public void Show()
    49            {
    50                string name = BusinessRule.GetPersonName() ;
    51                string telephone = BusinessRule.GetPersonTelephone() ;
    52                Console.WriteLine( string.Format( "{0}的电话是{1}." , name ,telephone ) ) ;
    53            }

    54        }

    55        /// <summary>
    56        /// 业务逻辑层
    57        /// </summary>

    58        public class BusinessRule
    59        {
    60            public BusinessRule() {}
    61
    62            public static string GetPersonName()
    63            {
    64                //业务逻辑代码
    65                return ( new Data() ).GetPerson().Name ;
    66            }

    67            public static string GetPersonTelephone()
    68            {
    69                //业务逻辑代码
    70                return ( new Data() ).GetPerson().Telephone ;
    71            }

    72        }

    73        /// <summary>
    74        /// 数据持久化层
    75        /// </summary>

    76        public class Data
    77        {
    78            public Data() {}
    79
    80            public Person GetPerson()
    81            {
    82                Person person = new Person() ;
    83                person.Name = "冯超" ;
    84                person.Telephone = "88888888" ;
    85                return person ;
    86            }

    87        }

    88        #endregion

    89    }

    90}

    由于开始数据库的设计不是很合理,把A模块的信息都集中存放在一个表中,用一个对象来表示,结果到现在变得不易与其他模块共用数据,所以就把这个表拆成几个小表,形成多个小对象,并关联起来。
    由于我思维定势,在修改了数据持久层后又去修改业务逻辑层。把原本好的设计的优势浪费了 。
    改动为(不好的修改):
      1using System;
      2 
      3namespace Example
      4{
      5    /**//// <summary>
      6    /// 示例
      7    /// </summary>

      8    class Example
      9    {
     10        /**//// <summary>
     11        /// 应用程序的主入口点。
     12        /// </summary>

     13        [STAThread]
     14        static void Main(string[] args)
     15        {
     16            PageView view = new PageView() ;
     17            view.Show() ;
     18        }

     19        #region 20050712
     20        
     21        /// <summary>
     22        /// 原person对象拆分为2个对象BasicPerson 和 Link
     23        /// 去掉原对象person
     24        /// </summary>

     25        public class BasicPerson                 
     26        {
     27            private string name = String.Empty ;
     28
     29            public BasicPerson() {}
     30
     31            public string Name                                
     32            {
     33                set{ name = value ; }
     34                getreturn name ; }
     35            }

     36        }

     37        public class Link           
     38        {
     39            private string telephone = String.Empty ;
     40
     41            public Link() {}
     42                 
     43            public string Telephone                               
     44            {
     45                set{ telephone = value ; }
     46                getreturn telephone ; }
     47            }

     48        }

     49
     50        /// <summary>
     51        /// 界面展现层
     52        /// </summary>

     53        public class PageView
     54        {
     55            public PageView() {}
     56            
     57            public void Show()
     58            {
     59                string name = BusinessRule.GetPersonName() ;
     60                string telephone = BusinessRule.GetPersonTelephone() ;
     61                Console.WriteLine( string.Format( "{0}的电话是{1}." , name ,telephone ) ) ;
     62            }

     63        }

     64        /// <summary>
     65        /// 业务逻辑层
     66        /// </summary>

     67        public class BusinessRule
     68        {
     69            public BusinessRule() {}
     70
     71            public static string GetPersonName()
     72            {
     73                //业务逻辑代码
     74                return ( new Data() ).GetBasicPerson().Name ;
     75            }

     76            public static string GetPersonTelephone()
     77            {
     78                //业务逻辑代码
     79                return ( new Data() ).GetLink().Telephone ;
     80            }

     81        }

     82        
     83        /// <summary>
     84        /// 数据持久化层
     85        /// </summary>

     86        public class Data
     87        {
     88            public Data() {}
     89
     90            public BasicPerson GetBasicPerson()
     91            {
     92                BasicPerson basic = new BasicPerson() ;
     93                basic.Name = "冯超" ;
     94                return basic ;
     95            }

     96            public Link GetLink()
     97            {
     98                Link link = new Link() ;
     99                link.Telephone = "88888888" ;
    100                return link ;
    101            }

    102        }

    103        #endregion

    104    }

    105}

    本来这样的改动是不用修改界面展现层和业务逻辑层的,只要在业务逻辑层和数据持久化层插入一个数据对象转换层就能很好的解决问题。
    比较好的解决方法是:

    模型代码为(好的修改):
      1using System;
      2 
      3namespace Example
      4{
      5    /**//// <summary>
      6    /// 示例
      7    /// </summary>

      8    class Example
      9    {
     10        /**//// <summary>
     11        /// 应用程序的主入口点。
     12        /// </summary>

     13        [STAThread]
     14        static void Main(string[] args)
     15        {
     16            PageView view = new PageView() ;
     17            view.Show() ;
     18        }

     19        #region 20050712
     20        /// <summary>
     21        /// 原对象person
     22        /// </summary>

     23        public class Person
     24        {
     25            private string name = String.Empty ;
     26            private string telephone = String.Empty ;
     27
     28            public Person() {}
     29
     30            public string Name
     31            {
     32                set{ name = value ; }
     33                getreturn name ; }
     34            }

     35            public string Telephone
     36            {
     37                set{ telephone = value ; }
     38                getreturn telephone ; }
     39            }

     40        }

     41        /// <summary>
     42        /// 原person对象拆分为2个对象BasicPerson 和 Link
     43        /// </summary>

     44        public class BasicPerson                 
     45        {
     46            private string name = String.Empty ;
     47
     48            public BasicPerson() {}
     49
     50            public string Name                                
     51            {
     52                set{ name = value ; }
     53                getreturn name ; }
     54            }

     55        }

     56        public class Link           
     57        {
     58            private string telephone = String.Empty ;
     59
     60            public Link() {}
     61                 
     62            public string Telephone                               
     63            {
     64                set{ telephone = value ; }
     65                getreturn telephone ; }
     66            }

     67        }

     68
     69        /// <summary>
     70        /// 界面展现层
     71        /// </summary>

     72        public class PageView
     73        {
     74            public PageView() {}
     75            
     76            public void Show()
     77            {
     78                string name = BusinessRule.GetPersonName() ;
     79                string telephone = BusinessRule.GetPersonTelephone() ;
     80                Console.WriteLine( string.Format( "{0}的电话是{1}." , name ,telephone ) ) ;
     81            }

     82        }

     83        /// <summary>
     84        /// 业务逻辑层
     85        /// </summary>

     86        public class BusinessRule
     87        {
     88            public BusinessRule() {}
     89
     90            public static string GetPersonName()
     91            {
     92                //业务逻辑代码
     93                return ( new Data() ).GetPerson().Name ;
     94            }

     95            public static string GetPersonTelephone()
     96            {
     97                //业务逻辑代码
     98                return ( new Data() ).GetPerson().Telephone ;
     99            }

    100        }

    101        /// <summary>
    102        /// 数据对象转换层
    103        /// </summary>

    104        public class Combination
    105        {
    106            public Combination() {} 
    107            
    108            public static Person GetPerson( BasicPerson basic , Link link )
    109            {
    110                Person person = new Person() ;
    111                person.Name = basic.Name ;
    112                person.Telephone = link.Telephone ;
    113                return person ;
    114            }

    115        }

    116        /// <summary>
    117        /// 数据持久化层
    118        /// </summary>

    119        public class Data
    120        {
    121            public Data() {}
    122
    123            public Person GetPerson()
    124            {
    125                Person person = new Person() ;
    126                BasicPerson basic = new BasicPerson() ;
    127                Link link = new Link() ;
    128                
    129                basic.Name = "冯超" ;
    130                link.Telephone = "88888888" ;
    131                //组合被拆分的小对象
    132                person = Combination.GetPerson( basic , link ) ;
    133                return person ;
    134            }

    135        }

    136        #endregion

    137    }

    138}

    即是说,好的修改应该是内聚的。

  • 相关阅读:
    ABAP 表格控制(Table Control)和步循环
    ABAP中正则表达式的简单使用方法 (转老白BLOG)
    ABAP常用函数集锦
    ALV用例大全
    DXP 笔记
    STM32笔记——Power Controller(PWR)
    STM32之glossary
    STM32 解析futaba S-bus协议
    Windows下 vundle的安装和使用
    使用串口绘制实时曲线 —— SerialChart
  • 原文地址:https://www.cnblogs.com/fengchao/p/191758.html
Copyright © 2020-2023  润新知