当前位置:编程学习 > asp >>

步步为营 .NET 代码重构学习笔记 六

一、Extract Class(提炼类)

动机(Motivation)

一个class应该是一个清楚的抽象(abstract),处理一些明确的责任。但在实际工作中,class会不断成长扩展。这个class会变得过份复杂,很快,class就会变成一团乱麻。

示例

    public class Person
    {
        private string _name;
        private string _officeAreaCode;
        private string _officeNumber;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string OfficeAreaCode
        {
            get { return _officeAreaCode; }
            set { _officeAreaCode = value; }
        }

        public string OfficeNumber
        {
            get { return _officeNumber; }
            set { _officeNumber = value; }
        }

        public string TelephoneNumber
        {
            get { return "(" + _officeAreaCode + ")" + _officeNumber; }
        }
    }

 

改为

  public class Person
    {
        private string _name;
        private Telephone _telephoneNumber;

        public Telephone TelephoneNumber
        {
            get { return _telephoneNumber; }
            set { _telephoneNumber = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string TemphoneNumber
        {
            get { return _telephoneNumber.TelephoneNumber; }
        }

    }
    public class Telephone
    {
        private string _officeAreaCode;
        private string _officeNumber;
        public string OfficeAreaCode
        {
            get { return _officeAreaCode; }
            set { _officeAreaCode = value; }
        }

        public string OfficeNumber
        {
            get { return _officeNumber; }
            set { _officeNumber = value; }
        }

        public string TelephoneNumber
        {
            get { return "(" + _officeAreaCode + ")" + _officeNumber; }
        }
    }

 

二、Inline Class(将类内联化)

动机(Motivation)

一个class不再承担足够责任、不再有单独存在的理由,将class的所有特性搬移到另一个class中,然后移除原class。

示例

    public class User
    {
        private int _ID;
        private string _name;

        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }


    }
    public class UserInfo
    {
        private int _ID;
        private string _Age;

        public string ID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        public string Age
        {
            get { return _Age; }
            set { _Age = value; }
        }
    }

 

改为

    public class User
    {
        private int _ID;
        private string _name;
        private string _Age;

        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string Age
        {
            get { return _Age; }
            set { _Age = value; }
        }
    }

 

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,