Introduce Foreign Method(引入外加函数)
来源:互联网 发布:如何隐藏域名映射的ip 编辑:程序博客网 时间:2024/05/01 19:45
你所使用的server class需要一个额外函数,但你无法修改这个class。
在client class中建立一个函数,并以一个server class实体作为第一引数(argument)。
Date newStart = new Date(previousEnd.getYear(), previousEnd.getMonth(), previousEnd.getDate() + 1);
==〉
Date newStart = nextDay(previousEnd);
private static Date nextDay(Date arg) {
return new Date(arg.getYear(), arg.getMonth(), arg.getDate() + 1);
}
动机
进行本项重构时,如果你以外加函数实现一项功能,那就是一个明确信号;这个函数原本应该在提供服务的(server) class中加以实现。
如果你发现自己为一个server class建立了大量外加函数,或如果你发现有许多classes都需要同样的外加函数,你就不应该再使用本项重构,而应该使用Introduce Local Extensiong(164)。
作法
1. 在client class中建立一个函数,用来提供你需要的功能。
这个函数不应该取用client class的任何特性。如果它需要一个值,把该值当作参数传给它。
2. 以server class实体作为该函数的第一个参数。
3. 将该函数注释为:[外加函数(foreign method),应在server class实现。]
这么一来,将来如果有机会将外加函数搬移到server class中,你便可以轻松找出这些外加函数。
程序中,我需要跨过一个收费周期。原本代码像这样:
Date newStart = new Date(previousEnd.getYear(), previousEnd.getMonth(), previousEnd.getDate() + 1);
我可以将赋值右侧代码提炼到一个函数中,这个函数就是Date Class的一个外加函数。
Date newStart = nextDay(previousEnd);
private static Date nextDay(Date arg) {
return new Date(arg.getYear(), arg.getMonth(), arg.getDate() + 1);
}
- Introduce Foreign Method(引入外加函数)
- 7.7 introduce foreign method(引入外加函数)
- 在对象之间搬移特性之七 :Introduce Foreign Method(引入外加函数)
- 重构笔记——引入外加函数
- 重新组织函数--Introduce Explaining Variable(引入解释性变量)
- 重新组织你的函数之五 :Introduce Explaining Variable(引入解释性变量)
- 简化函数调用之九 :Introduce Parameter Object(引入参数对象)
- Introduce Explaining Variable(引入解释性变量)
- Introduce Local Extension(引入本地扩展)
- Introduce Null Object(引入Null对象)
- 9.7 introduce Null object (引入null对象)
- 7.8 Introduce local extension (引入本地扩展)
- 6.5 Introduce Explaining Variable(引入解释性变量)
- Introduce Parameter Object(引入参数对象)
- introduce
- Introduce
- introduce
- 简化条件表达式之八 :Introduce Assertion(引入断言)
- C#中 @ 的用法
- 基础小常识集锦
- Pattern-Oriented Software Architecture v1巨详细读书笔记 10
- COM与接口
- ListBox单选模式,有相同项时,为什么ListBox.SelectedIndex总是得到最小值?
- Introduce Foreign Method(引入外加函数)
- SQL中索引的原理
- 常见数据类型转换
- Visual Studio 2008 每日提示(二十六)
- LinkTrust UTM解决方案
- VC 中调用默认邮件发送程序(Outlook)发送 指定附件的email
- http://www.scriptlover.com/controls/
- Excel教你实现不同表格数据的同步更新
- 在Repeater中实例化控件