Replace Method with Method Object <IMG SRC = "http://www.refactoring.com/catalog/updated.gif" border=0>
2003. 8. 3. 11:55
Replace Method with Method Object
Extract Method
Turn the method into its own object so that all the local variables become fields on that object. You can then decompose the method into other methods on the same object.
class Order...
double price() {
double primaryBasePrice;
double secondaryBasePrice;
double tertiaryBasePrice;
// long computation;
...
}
![](http://www.refactoring.com/catalog/arrow.gif)
![](http://www.refactoring.com/catalog/repMethodWithObj.gif)
Additional Comments
Using a Static Method
You can do this refactoring with a static method, but in this case you don't need the first field pointing back to the original object.
Alternative Steps with Temps
Marnix Klooster suggested an alternative to the mechanics. Rather than creating fields in the Method Object for all the temps, leave the temps as temps and only turn them into fields as you need to in order to use Extract Method. It means you have to use a special form of Extract Method, but may help in reducing the scope of the temps. Of course you can use Reduce Scope of Variable afterwards on any temp that's only used in one method.
Contributors
- Marnix Klooster
For more information see page 135 of Refactoring
***** 아름다운프로님에 의해서 게시물 복사 + 카테고리변경되었습니다 (2003-12-18 17:27)