[信息与通信]第13讲_Ch19-21_可见性设计 将设计映射为代码 测试驱动开发和重构.pptVIP

  • 3
  • 0
  • 约2.66千字
  • 约 30页
  • 2018-03-02 发布于浙江
  • 举报

[信息与通信]第13讲_Ch19-21_可见性设计 将设计映射为代码 测试驱动开发和重构.ppt

[信息与通信]第13讲_Ch19-21_可见性设计 将设计映射为代码 测试驱动开发和重构

* NextGen Pos程序简介 * NextGen Pos程序简介 * NextGen Pos程序简介 * NextGen Pos程序简介 * 测试驱动开发 * 重构 public class Player { private Piece piece; private Board board; private Die[] dice; // … public void takeTurn() { // roll dice int rollTotal = 0; for (int i = 0; i dice.length; i++) { dice[i].roll(); rollTotal += dice[i].getFaceValue(); } Square newLoc = board.getSquare(piece.getLocation(), rollTotal); piece.setLocation(newLoc); } } // end of class 提炼方法 * 重构 public class Player { private Piece piece; private Board board; private Die[] dice; // … public void takeTurn() { // the refactored helper method int rollTotal = rollDice(); Square newLoc = board.getSquare(piece.getLocation(), rollTotal); piece.setLocation(newLoc); } private int rollDice() { int rollTotal = 0; for (int i = 0; i dice.length; i++) { dice[i].roll(); rollTotal += dice[i].getFaceValue(); } return rollTotal; } } // end of class * 重构 // good method name, but the logic of the body is not clear boolean isLeapYear( int year ) { return ( ( ( year % 400 ) == 0 ) || ( ( ( year % 4 ) == 0 ) ( ( year % 100 ) != 0 ) ) ); } // that’s better! boolean isLeapYear( int year ) { boolean isFourthYear = ( ( year % 4 ) == 0 ); boolean isHundrethYear = ( ( year % 100 ) == 0); boolean is4HundrethYear = ( ( year % 400 ) == 0); return ( is4HundrethYear || ( isFourthYear ! isHundrethYear ) ); } 引入解释变量 * 重构 IDE对重构的支持(提炼之前) * 重构 IDE对重构的支持(提炼之前) * IntellAgile IntellAgile Copyright ? 2002 Craig Larman. All rights reserved. IntellAgile * IntellAgile * IntellAgile * IntellAgile * IntellAgile * IntellAgile * IntellAgile * IntellAgile * IntellAgile * IntellAgile * 可见性设计-代码 * 对可见性进行设计 可见性是一个对象看见其他对象或引用其他对象的能力 For an object A to send a message to an object B, B must be visible to A. * 四种类型的可见性(一) 属性可见性 * 四种类型的可见性(二) 参数可见性 对Sale的makeLineItem范围内desc是参数可见 * 四种类型的可见性(三) 参数可见性转化为属性可见性 * 四种类型的可见性(四) 局部可见性 创建实例分配给局部变量 将方法调用返回的对象分配给局部变量

文档评论(0)

1亿VIP精品文档

相关文档