现在的位置: 首页 > 综合 > 正文

代码重构~方法归子

2013年05月07日 ⁄ 综合 ⁄ 共 507字 ⁄ 字号 评论关闭
文章目录

方法归父正好相反,当父类的方法只为一个子类提供时,这时应该考虑把这个方法放到子类中,这就是“方法归子”。

方法归子  — 或译函数下移,指的是方法从父类移动到子类。

代码如下:

 

 1        #region 方法归子
 2         /// <summary>
 3         /// 如果InsertBase中的InsertSub方法只在SubInsert中使用,
 4         /// 那么,应该考虑将InsertSub方法移到SubInsert类中
 5         /// </summary>
 6         class SubInsert : InsertBase
 7         {
 8         }
 9         abstract class InsertBase
10         {
11             /// <summary>
12             /// 插入方法
13             /// </summary>
14             public virtual void InsertSub()
15             {
16                 throw new NotImplementedException("SubInsert插入方法");
17             }
18         }
19         #endregion

 

 代码重构目录

1 封装成员变量(Encapsulate Field)

2 提取方法(Extract Method)

3 提取到类(Extract Class)

4 方法归父(方法上移)

5 方法归子(方法下移)

6 方法更名 

7 代码注释

抱歉!评论已关闭.