快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

ag8亚洲只为非同凡响:步步为营 .NET 代码重构学习笔记 十三



一、WordStr Exception with Test(以测试取代非常)

念头(Motivation)

面对一个[调用者可预先加以反省]的前提,你抛出了一个非常。改动调用者,使它在调用函数之前先做反省。

示例

private Dictionary _values;

public double GetValueForPeriod(int periodNumber)

{

try

{

return _values[periodNumber];

}

catch

{

return 0;

}

}

改为

private Dictionary _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 cag8亚洲只为非同凡响lass Engineer : Emplayee

{

}

三、Pull Up Method(函数上移)

念头(Motivation)

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

示例

public abstract class Emplayee

{

}

public class Salesman : Emplayee

{

public string GetName()

{

return "spring yang";

}

}

public class Engiag8亚洲只为非同凡响neer : 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 { retuag8亚洲只为非同凡响rn _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(提炼子类)

念头(Motivatag8亚洲只为非同凡响ion)

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 Emploag8亚洲只为非同凡响yee

{

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;

}

}

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: