步步为营 .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; }
        }
    }

三、Hide Delegate(隐藏[委托关系])

动机(Motivation)

[封装]即使不是对象的最关键特征,也是最关键特征之一。[封装]意味每个对象都应该尽可能少了解系统的其它部分。如此一来,一旦发生变化,需要了解这变化的对象就会比较少,这会使谈化比较容易进行。

示例

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

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

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

        public Department Depart
        {
            get { return _depart; }
            set { _depart = value; }
        }
 
    }

    public class Department
    {
        private string _departName;
        private User _manager;

        public string DepartName
        {
            get { return _departName; }
            set { _departName = value; }
        }

        public User Manager
        {
            get { return _manager; }
            set { _manager = value; }
        }
    }

改为

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

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

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

        public Department Depart
        {
            get { return _depart; }
            set { _depart = value; }
        }

        public string DepartmentManager
        {
            get { return _depart.Manager; }
        }
    }

    public class Department
    {
        private string _departName;
        private User _manager;

        public string DepartName
        {
            get { return _departName; }
            set { _departName = value; }
        }

        public User Manager
        {
            get { return _manager; }
            set { _manager = value; }
        }
    }

这样取部门管理者就更直接,更方便,适合于少量使用。

但如果有大量的方法都用到了DepartmentManager里,我们就要去掉中间人了。

四、Remove Middle Man(移除中间人)

动机(Motivation)

大量重复的代码是万恶之源。这些重复性代码应该抽出来放进同一个函数中。

示例

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

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

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

        public Department Depart
        {
            get { return _depart; }
            set { _depart = value; }
        }

        public string DepartmentManager
        {
            get { return _depart.Manager; }
        }
    }

    public class Department
    {
        private string _departName;
        private User _manager;

        public string DepartName
        {
            get { return _departName; }
            set { _departName = value; }
        }

        public User Manager
        {
            get { return _manager; }
            set { _manager = value; }
        }
    }

改为

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

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

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

        public Department Depart
        {
            get { return _depart; }
            set { _depart = value; }
        }

    }

    public class Department
    {
        private string _departName;
        private User _manager;

        public string DepartName
        {
            get { return _departName; }
            set { _departName = value; }
        }

        public User Manager
        {
            get { return _manager; }
            set { _manager = value; }
        }
    }

在调用时就直接Depart.Manager来取得它的值.

posted @ 2011-05-25 23:36  spring yang  阅读(2006)  评论(4编辑  收藏  举报