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

一、Introduce Foreign Method(引入外加函数)

动机(Motivation)

在client class 中建立一个函数,并以一个server class实体作为第一引数(argument)。

示例

1
DateTime newStart = DateTime.Now.AddDays(1);

改为

1
2
3
4
5
public DateTime  NextDate()
{
    return  DateTime.Now.AddDays(1);
  
}

二、Introduce Local Extension(引入本地扩展)

动机(Motivation)

建立一个新class,使它包含这些额外函数。让这个扩展品成为source class的subclass(子类)或wrapper(外覆类)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
protected void Main()
{
    Computer _computer;
    StringBuilder strCom = new StringBuilder();
    strCom.AppendLine("你的电脑配置如下:");
    strCom.AppendLine("主板是:" + _computer.MainBoard());
    strCom.AppendLine("处理器是:" + _computer.Cpu());
    strCom.AppendLine("显卡是:" + _computer.PhenoType());
    strCom.AppendLine("内存是:" + _computer.Memory());
    strCom.AppendLine("硬盘是:" + _computer.HardDisk());
    strCom.AppendLine("显示器是:" + _computer.Display());
    strCom.AppendLine("己组装完成");
    Console.WriteLine(strCom.ToString);
}

改为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
protected void Main()
     {
         Console.WriteLine(ShowComputerConfigure());
     }
 
     public string ShowComputerConfigure()
     {
         Computer _computer;
         StringBuilder strCom = new StringBuilder();
         strCom.AppendLine("你的电脑配置如下:");
         strCom.AppendLine("主板是:" + _computer.MainBoard());
         strCom.AppendLine("处理器是:" + _computer.Cpu());
         strCom.AppendLine("显卡是:" + _computer.PhenoType());
         strCom.AppendLine("内存是:" + _computer.Memory());
         strCom.AppendLine("硬盘是:" + _computer.HardDisk());
         strCom.AppendLine("显示器是:" + _computer.Display());
         strCom.AppendLine("己组装完成");
 
         return strCom.ToString();
     }

三、Self Encapsulate Field(自封装值域)

动机(Motivation)

为这个值域建立取值/设置函数(getting/setting methods),并且只以这些函数来访问值域。

示例

1
2
3
4
5
public  int _low, _high;
public bool Includes(int arg)
{
    return arg >= _low && arg <= _high;
}

改为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private int _low, _high;
 
public int Low
{
    get { return _low; }
    set { _low = value; }
}
 
public int High
{
    get { return _high; }
    set { _high = value; }
}
 
public bool Includes(int arg)
{
    return arg >= Low && arg <= High;
}

四、Replace Data Value with Object(以对象取代数据值)

动机(Motivation)

将数据项变成一个对象

示例

1
2
3
4
5
6
7
8
9
public class Customer
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
}

改为

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Customer
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    public Customer(string name)
    {
        this._name = name;
    }
}

引用时

1
string name = new Customer("spring yang");

五、Change Value to Referencce(将实值对象改为引用对象)

动机(Motivation)

将value object(实值对象)变成一个reference object(引用对象)

示例

1
2
3
4
public void GetCustomers()
{
    string[] UserName = { new Customer("Spring Yang"), new Customer("Lemon Car"), new Customer("Associated Coffee") };
}

改为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private Dictionary<string, Customer> dicUserName = new Dictionary<string, Customer>();
 
       public void GetCustomers()
       {
           string[] UserName = { dicUserName.TryGetValue("Spring Yang"), dicUserName.TryGetValue("Lemon Car"),
                                   dicUserName.TryGetValue("Associated Coffee") };
       }
 
       private void LoadCustomers()
       {
           AddCustomer("Spring Yang");
           AddCustomer("Lemon Car");
           AddCustomer("Associated Coffee");
       }
 
       private void AddCustomer(string name)
       {
           dicUserName.Add(name, new Customer(name));
       }

六、Change Reference to Value(将引用对象改为实值对象)

动机(Motivation)

reference object(引用对象),很小且不可变(immutable),而且不易管理。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private Dictionary<string, Customer> dicUserName = new Dictionary<string, Customer>();
 
       public void GetCustomers()
       {
           string[] UserName = { dicUserName.TryGetValue("Spring Yang"), dicUserName.TryGetValue("Lemon Car"),
                                   dicUserName.TryGetValue("Associated Coffee") };
       }
 
       private void LoadCustomers()
       {
           AddCustomer("Spring Yang");
           AddCustomer("Lemon Car");
           AddCustomer("Associated Coffee");
       }
 
       private void AddCustomer(string name)
       {
           dicUserName.Add(name, new Customer(name));
       }

改为

1
2
3
4
public void GetCustomers()
{
    string[] UserName = { new Customer("Spring Yang"), new Customer("Lemon Car"), new Customer("Associated Coffee") };
}

七、Replace Array with Object(以对象取代数组)

动机(Motivation)

以对象替换数组。对于数组中的每个元素,以一个值域表示。

示例

1
2
3
4
5
6
7
public void Main()
{
    string[] UserInfo = new string[3];
    UserInfo[0] = "1";
    UserInfo[1] = "spring yang";
    UserInfo[2] = "IT";
}

改为

1
2
3
4
5
6
7
public void Main()
{
    User user = new User();
    user.ID = "1";
    user.Name = "spring yang";
    user.Depart = "IT";
}
posted @   spring yang  阅读(1698)  评论(2编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示