Chinaunix首页 | 论坛 | 博客
  • 博客访问: 222314
  • 博文数量: 136
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 09:08
文章分类

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: Java

2011-04-19 08:34:47

You have a code fragment that can be grouped together.
Tips: Turn the fragement into a method whose name explains the purpose of the method.

Mechanics:
  • Create a new method, and name it after the intention of the method (name it by what it does, not by how it does it).
  • Copy the extracted code from the source method into the new target method.
  • Scan the extracted code for references to any variables that are local in scope to the source method. These are local variables and parameters to the method.
  • See whether any temporary variables are used only within this extracted code. If so, declare them in the target method as temporary variables.
  • Look to see whether any of these local-scope variables are modified by the extracted code. If one variable is modified, see whether you can treat the extracted code as a query and assign the result to the varialbe concerned. If this is awkward, or if there is more than one such variable, you may need to use Split Temporary Variable and try again. You can eliminate temporary variables with Replace Temp with Query.
  • Pass into the target method as parameters local-scope variables that are read from the extracted code.
  • compile when you have dealt with all the locally-scoped variables.
  • Replace the extracted code in the source method with a call to the target method.
  • Compile and test.

  1. void printOwing(double amount) {
  2.     printBanner();

  3.     //print details
  4.     System.out.println("name:"+_name);
  5.     System.out.println("amount"+amount);
  6. }
                                          ==>

  1. void printOwing(double amount) {
  2.     printBanner();
  3.     printDetails(amount);
  4. }

  5. void printDetails(double amount) {
  6.     System.out.println("name:"+_name);
  7.     System.out.println("amount"+amount);
  8. }

阅读(814) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~