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

一、Replace Exception with Test(以测试取代异常)

动机(Motivation)

面对一个[调用者可预先加以检查]的条件,你抛出了一个异常。修改调用者,使它在调用函数之前先做检查。

示例

        private Dictionary<int, string> _values;

        public double GetValueForPeriod(int periodNumber)
        {
            try
            {
                return _values[periodNumber];
            }
            catch
            {
                return 0;
            }
        }

 

改为

        private Dictionary<int, string> _values;

        public double GetValueForPeriod(int periodNumber)
        {
            if (_values.ContainsKey(periodNumber))
                return _values[periodNumber];
            return 0;
        }

 

 

二、Pull Up Field(值域上移)

动机(Motivation)

两个subclasses拥有相同的值域,将此一值域移至superclass。

示例

    public abstract class Emplayee
    { 
    
    }

    public class Salesman : Emplayee
    {
        private string _name;

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

    public class Engineer : Emplayee
    {
        private string _name;

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

 

改为

    public abstract class Emplayee
    {
        private string _name;

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

    public class Salesman : Emplayee
    {

    }

    public class Engineer : Emplayee
    {

    }

 

三、Pull Up Method(函数上移)

动机(Motivation)

有些函数,在各个subclass中产生完全相同的结果。将该函数移至superclass。

示例

    public abstract class Emplayee
    {

    }

    public class Salesman : Emplayee
    {
        public string GetName()
        {
            return "spring yang";
        }
    }

    public class Engineer : Emplayee
    {
        public string GetName()
        {
            return "spring yang";
        }
    }

 

改为

    public abstract class Emplayee
    {
        public string GetName()
        {
            return "spring yang";
        }
    }

    public class Salesman : Emplayee
    {
    }

    public class Engineer : Emplayee
    {
    }

 

四、Pull Up Constructor Body(构造函数本体上移)

动机(Motivation)

在各个subclass中拥有一些构造函数,它们的本体代码几乎一致,在base中新建一个构造函数,并在subclass构造函数中调用它。

示例

    public abstract class Emplayee
    {
        private string _ID;
        private string _name;

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

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

    public class Salesman : Emplayee
    {
        public Salesman(string id, string name)
        {
            ID = id;
            Name = name;
        }
    }

 

改为

    public abstract class Emplayee
    {
        private string _ID;
        private string _name;

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

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

        public Emplayee(string id, string name)
        {
            _ID = id;
            _name = name;
        }
    }

    public class Salesman : Emplayee
    {
        public Salesman(string id, string name):base(id,name)
        {
        }
    }

 

五、Push Down Method(函数下移)

动机(Motivation)

superclass中的某个函数只与部分(而非全部)subclasses有关。将这个函数移到相关的那些subclasses去。

示例

    public abstract class Emplayee
    {
        public double GetQuota()
        {
            return 0;
        }
    }

    public class Salesman : Emplayee
    {

    }

    public class Engineer : Emplayee
    {

    }

 

改为

    public abstract class Emplayee
    {

    }

    public class Salesman : Emplayee
    {
        public double GetQuota()
        {
            return 0;
        }
    }

    public class Engineer : Emplayee
    {

    }

 

六、Push Down Field(值域下移)

动机(Motivation)

superclass中的某个值域只被部分(而非全部)subclasses用到。将这个值域移到需要它的那些subclasses去。

示例

    public abstract class Emplayee
    {
        private double _quota;
        public double Quota
        {
            get { return _quota; }
            set { _quota = value; }
        }
    }

    public class Salesman : Emplayee
    {
 
    }

    public class Engineer : Emplayee
    {

    }

 

改为

    public abstract class Emplayee
    {

    }

    public class Salesman : Emplayee
    {
        private double _quota;
        public double Quota
        {
            get { return _quota; }
            set { _quota = value; }
        }
    }

    public class Engineer : Emplayee
    {

    }

 

七、Extract Subclass(提炼子类)

动机(Motivation)

class中的某些特性(features)只被某些(而非全部)实体(instances)用到,新建一个subclass,将上面所说的那一部分特性移到subclass中。

示例

    public class JobItem
    {
        public double GetTotalPrice()
        {
            return 1000;
        }

        public double GetUnitPrice()
        {
            return 10;
        }

        public string GetEmployee()
        {
            return 100;
        }
    }

 

改为

    public class JobItem:BaseItem
    {
        public double GetTotalPrice()
        {
            return 1000;
        }

    }

    public class BaseItem
    {
        public double GetUnitPrice()
        {
            return 10;
        }
    }

    public class LaborItem:BaseItem
    {
        public string GetEmployee()
        {
            return 100;
        }
    }

 

八、Extract Superclass(提炼超类)

动机(Motivation)

两个classes有相似特性(similar features)。为这两个classes建立一个superclass,将相同特性移至superclass。

示例

    public class Department
    {
        public double GetAnnualCost()
        {
            return 1000;
        }

        public string GetName()
        {
            return "spring yang";
        }

        public double GetHeadCount()
        {
            return 100;
        }
    }

    public class Employee
    {
        public double GetAnnualCost()
        {
            return 1000;
        }

        public string GetName()
        {
            return "spring yang";
        }

        public int GetID()
        {
            return 1;
        }
    }

 

改为

   public class LaborItem:BaseItem
    {
        public string GetEmployee()
        {
            return 100;
        }
    }

    public class Department:Party
    {
        public double GetHeadCount()
        {
            return 100;
        }
    }

    public class Employee:Party
    {
        public int GetID()
        {
            return 1;
        }
    }

    public class Party
    {
        public double GetAnnualCost()
        {
            return 1000;
        }

        public string GetName()
        {
            return "spring yang";
        }
    }

 

九、Extract Interface(提炼接口)

动机(Motivation)

若干客户使用class接口中的同一子集;或者,两个classes的接口有部分相同。将相同的子集提炼到一个独立接口中。

示例

    public class Employee
    {
        public double GetRate()
        {
            return 1;
        }

        public bool HasSpecialSkill()
        {
            return true;
        }

        public string GetName()
        {
            return "spring yang";
        }

        public string GetDepartMent()
        {
            return "Development";
        }
    }

 

改为

    public class Employee:Billable
    {
        public double GetRate()
        {
            return 1;
        }

        public bool HasSpecialSkill()
        {
            return true;
        }

        public string GetName()
        {
            return "spring yang";
        }

        public string GetDepartMent()
        {
            return "Development";
        }
    }

    public interface Billable
    {
        double GetRate();
        bool HasSpecialSkill();
    }

 

十、Collapse Hierarchy(折叠继承体系)

动机(Motivation)

superclass 和subclass之间无太大区别。将它们合为一体。

示例

    public class Employee 
    {
        public string GetName()
        {
            return "spring yang";
        }

        public string GetDepartMent()
        {
            return "Development";
        }
    }

    public class Salesman
    {
        public int GetID()
        {
            return 1;
        }
    }

 

改为

    public class Employee 
    {
        public string GetName()
        {
            return "spring yang";
        }

        public string GetDepartMent()
        {
            return "Development";
        }

        public int GetID()
        {
            return 1;
        }
    }

 

 

 

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

步步为营 .NET 代码重构学习笔记 一、为何要代码重构

步步为营 .NET 代码重构学习笔记 二、提炼方法(Extract Method)

步步为营 .NET 代码重构学习笔记 三、内联方法(Inline Method)

步步为营 .NET 代码重构学习笔记 四、临时变量(Temporary Variable)

步步为营 .NET 代码重构学习笔记 五、分解函数和替换算法(Replace Method And Substitute Algorithm)

步步为营 .NET 代码重构学习笔记 六、移动函数和移动值域(Move Method And Move Field)

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

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

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

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

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

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

转载于:https://www.cnblogs.com/springyangwc/archive/2011/06/06/2073441.html

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页