Successfully reported this slideshow.

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Refactoring in AS3

  1. 1. Refactoring in AS3
  2. 2. http://blog.eddie.com.tw aquarianboy@ptt eddie@adcube.com.tw aquarianboy@plurk eddiekao@facebook
  3. 3. AS3 OOP Design Pattern
  4. 4. ...
  5. 5. Refactoring...?
  6. 6. B
  7. 7. !?
  8. 8. ?
  9. 9. !=
  10. 10. ?
  11. 11. ?
  12. 12. ?
  13. 13. duplicated code, long method, large class, long parameter list...
  14. 14. If it stinks, change it.
  15. 15. Rule of Three Three strikes and you refactor
  16. 16. ?
  17. 17. ? ! ...
  18. 18. ? ... !
  19. 19. ? ... !
  20. 20. Design Pattern (over-engineering)
  21. 21. Design Pattern v.s. Refactoring
  22. 22. !
  23. 23. &
  24. 24. !
  25. 25. .. 1. 2. 3. 2 2 2 1.5 1.5 3 3 1.5 3 1 1 1
  26. 26. Extract Method ...
  27. 27. // Customer.as // Customer.as public function checkout ():String public function checkout():String { { ... while(rentals.length > 0) while(rentals.length > 0) { { ... ... ... this_amount = amount_for(the_rental); ... switch(the_rental.get_movie().get_price_code()) } { .. case Movie.REGULAR: // } this_amount += 2; if (the_rental.get_days_rented() > 2) private function amount_for (the_rental:Rental):Number { { this_amount+=(the_rental.get_days_rented()-2)* 1.5; var result:int = 0; } switch (the_rental.get_movie().get_price_code()) break; { case Movie.REGULAR :// case Movie.NEW_RELEASE: // result += 2; this_amount += the_rental.get_days_rented() * 3; if (the_rental.get_days_rented() > 2) break; { result += (the_rental.get_days_rented() - 2) * 1.5; case Movie.CHILDRENS: // } this_amount += 1.5; break; if (the_rental.get_days_rented() > 3) { case Movie.NEW_RELEASE :// this_amount+=(the_rental.get_days_rented()-3)*1.5; result += the_rental.get_days_rented() * 3; } break; break; } case Movie.CHILDRENS :// ... result += 1.5; } if (the_rental.get_days_rented() > 3) { result += (the_rental.get_days_rented() - 3) * 1.5; } break; } return result; }
  28. 28. // Customer.as // Customer.as public function checkout ():String public function checkout():String { { ... while(rentals.length > 0) while(rentals.length > 0) { { ... ... ... this_amount = amount_for(the_rental); ... switch(the_rental.get_movie().get_price_code()) } { .. case Movie.REGULAR: // } this_amount += 2; if (the_rental.get_days_rented() > 2) private function amount_for (the_rental:Rental):Number { { this_amount+=(the_rental.get_days_rented()-2)* 1.5; var result:int = 0; } switch (the_rental.get_movie().get_price_code()) break; { case Movie.REGULAR :// case Movie.NEW_RELEASE: // result += 2; this_amount += the_rental.get_days_rented() * 3; if (the_rental.get_days_rented() > 2) break; { result += (the_rental.get_days_rented() - 2) * 1.5; case Movie.CHILDRENS: // } this_amount += 1.5; break; if (the_rental.get_days_rented() > 3) { case Movie.NEW_RELEASE :// this_amount+=(the_rental.get_days_rented()-3)*1.5; result += the_rental.get_days_rented() * 3; } break; break; } case Movie.CHILDRENS :// ... result += 1.5; } if (the_rental.get_days_rented() > 3) { result += (the_rental.get_days_rented() - 3) * 1.5; } break; } return result; }
  29. 29. // Customer.as // Customer.as public function checkout ():String public function checkout():String { { ... while(rentals.length > 0) while(rentals.length > 0) { { ... ... ... this_amount = amount_for(the_rental); ... switch(the_rental.get_movie().get_price_code()) } { .. case Movie.REGULAR: // } this_amount += 2; if (the_rental.get_days_rented() > 2) private function amount_for (the_rental:Rental):Number { { this_amount+=(the_rental.get_days_rented()-2)* 1.5; var result:int = 0; } switch (the_rental.get_movie().get_price_code()) break; { case Movie.REGULAR :// case Movie.NEW_RELEASE: // result += 2; this_amount += the_rental.get_days_rented() * 3; if (the_rental.get_days_rented() > 2) break; { result += (the_rental.get_days_rented() - 2) * 1.5; case Movie.CHILDRENS: // } this_amount += 1.5; break; if (the_rental.get_days_rented() > 3) { case Movie.NEW_RELEASE :// this_amount+=(the_rental.get_days_rented()-3)*1.5; result += the_rental.get_days_rented() * 3; } break; break; } case Movie.CHILDRENS :// ... result += 1.5; } if (the_rental.get_days_rented() > 3) { result += (the_rental.get_days_rented() - 3) * 1.5; } break; } return result; }
  30. 30. // Customer.as // Customer.as public function checkout ():String public function checkout():String { { ... while(rentals.length > 0) while(rentals.length > 0) { { ... ... ... this_amount = amount_for(the_rental); ... switch(the_rental.get_movie().get_price_code()) } { .. case Movie.REGULAR: // } this_amount += 2; if (the_rental.get_days_rented() > 2) private function amount_for (the_rental:Rental):Number { { this_amount+=(the_rental.get_days_rented()-2)* 1.5; var result:int = 0; } switch (the_rental.get_movie().get_price_code()) break; { case Movie.REGULAR :// case Movie.NEW_RELEASE: // result += 2; this_amount += the_rental.get_days_rented() * 3; if (the_rental.get_days_rented() > 2) break; { result += (the_rental.get_days_rented() - 2) * 1.5; case Movie.CHILDRENS: // } this_amount += 1.5; break; if (the_rental.get_days_rented() > 3) { case Movie.NEW_RELEASE :// this_amount+=(the_rental.get_days_rented()-3)*1.5; result += the_rental.get_days_rented() * 3; } break; break; } case Movie.CHILDRENS :// ... result += 1.5; } if (the_rental.get_days_rented() > 3) { result += (the_rental.get_days_rented() - 3) * 1.5; } break; } return result; }
  31. 31. Move Method method ...
  32. 32. // Customer.as // Customer.as public class Customer public class Customer { { .. private function amount_for (the_rental:Rental):Number private function amount_for (the_rental:Rental):Number { { return the_rental.get_charge(); var result:int = 0; } switch (the_rental.get_movie().get_price_code()) } { case Movie.REGULAR :// // Rental.as result += 2; public class Rental if (the_rental.get_days_rented() > 2) { { ... result += (the_rental.get_days_rented() - 2) * 1.5; public function get_charge():Number } { break; var result:int = 0; switch (get_movie().get_price_code()) case Movie.NEW_RELEASE :// { result += the_rental.get_days_rented() * 3; case Movie.REGULAR :// break; result += 2; if (get_days_rented() > 2) case Movie.CHILDRENS :// { result += 1.5; result += (get_days_rented() - 2) * 1.5; if (the_rental.get_days_rented() > 3) } { break; result += (the_rental.get_days_rented() - 3) * 1.5; } case Movie.NEW_RELEASE :// break; result += get_days_rented() * 3; } break; return result; case Movie.CHILDRENS :// } result += 1.5; .. if (get_days_rented() > 3) } { result += (get_days_rented() - 3) * 1.5; } break; } return result; } ... }
  33. 33. // Customer.as // Customer.as public class Customer public class Customer { { .. private function amount_for (the_rental:Rental):Number private function amount_for (the_rental:Rental):Number { { return the_rental.get_charge(); var result:int = 0; } switch (the_rental.get_movie().get_price_code()) } { case Movie.REGULAR :// // Rental.as result += 2; public class Rental if (the_rental.get_days_rented() > 2) { { ... result += (the_rental.get_days_rented() - 2) * 1.5; public function get_charge():Number } { break; var result:int = 0; switch (get_movie().get_price_code()) case Movie.NEW_RELEASE :// { result += the_rental.get_days_rented() * 3; case Movie.REGULAR :// break; result += 2; if (get_days_rented() > 2) case Movie.CHILDRENS :// { result += 1.5; result += (get_days_rented() - 2) * 1.5; if (the_rental.get_days_rented() > 3) } { break; result += (the_rental.get_days_rented() - 3) * 1.5; } case Movie.NEW_RELEASE :// break; result += get_days_rented() * 3; } break; return result; case Movie.CHILDRENS :// } result += 1.5; .. if (get_days_rented() > 3) } { result += (get_days_rented() - 3) * 1.5; } break; } return result; } ... }
  34. 34. // Customer.as // Customer.as public class Customer public class Customer { { .. private function amount_for (the_rental:Rental):Number private function amount_for (the_rental:Rental):Number { { return the_rental.get_charge(); var result:int = 0; } switch (the_rental.get_movie().get_price_code()) } { case Movie.REGULAR :// // Rental.as result += 2; public class Rental if (the_rental.get_days_rented() > 2) { { ... result += (the_rental.get_days_rented() - 2) * 1.5; public function get_charge():Number } { break; var result:int = 0; switch (get_movie().get_price_code()) case Movie.NEW_RELEASE :// { result += the_rental.get_days_rented() * 3; case Movie.REGULAR :// break; result += 2; if (get_days_rented() > 2) case Movie.CHILDRENS :// { result += 1.5; result += (get_days_rented() - 2) * 1.5; if (the_rental.get_days_rented() > 3) } { break; result += (the_rental.get_days_rented() - 3) * 1.5; } case Movie.NEW_RELEASE :// break; result += get_days_rented() * 3; } break; return result; case Movie.CHILDRENS :// } result += 1.5; .. if (get_days_rented() > 3) } { result += (get_days_rented() - 3) * 1.5; } break; } return result; } ... }
  35. 35. // Customer.as // Customer.as public class Customer public class Customer { { .. private function amount_for (the_rental:Rental):Number private function amount_for (the_rental:Rental):Number { { return the_rental.get_charge(); var result:int = 0; } switch (the_rental.get_movie().get_price_code()) } { case Movie.REGULAR :// // Rental.as result += 2; public class Rental if (the_rental.get_days_rented() > 2) { { ... result += (the_rental.get_days_rented() - 2) * 1.5; public function get_charge():Number } { break; var result:int = 0; switch (get_movie().get_price_code()) case Movie.NEW_RELEASE :// { result += the_rental.get_days_rented() * 3; case Movie.REGULAR :// break; result += 2; if (get_days_rented() > 2) case Movie.CHILDRENS :// { result += 1.5; result += (get_days_rented() - 2) * 1.5; if (the_rental.get_days_rented() > 3) } { break; result += (the_rental.get_days_rented() - 3) * 1.5; } case Movie.NEW_RELEASE :// break; result += get_days_rented() * 3; } break; return result; case Movie.CHILDRENS :// } result += 1.5; .. if (get_days_rented() > 3) } { result += (get_days_rented() - 3) * 1.5; } break; } return result; } ... }
  36. 36. // Customer.as // Customer.as public class Customer public class Customer { { .. .. public function checkout ():String public function checkout ():String { { var total_amount:Number = 0;// var total_amount:Number = 0;// var bonus_points:int = 0;// var bonus_points:int = 0;// var rentals:Array = _rentals; var rentals:Array = _rentals; var result:String = get_name() +" n"; var result:String = get_name() +" n"; while (rentals.length > 0) while (rentals.length > 0) { { var this_amount:Number = 0; var this_amount:Number = 0; var the_rental:Rental = rentals.shift() as Rental; var the_rental:Rental = rentals.shift() as Rental; this_amount = amount_for(the_rental); this_amount = the_rental.get_charge(); // // bonus_points += 1; bonus_points += 1; ... ... } } result += "n " + total_amount + "n"; result += "n " + total_amount + "n"; result += " " + bonus_points + " "; result += " " + bonus_points + " "; return result; return result; } } .. .. } }
  37. 37. // Customer.as // Customer.as public class Customer public class Customer { { .. .. public function checkout ():String public function checkout ():String { { var total_amount:Number = 0;// var total_amount:Number = 0;// var bonus_points:int = 0;// var bonus_points:int = 0;// var rentals:Array = _rentals; var rentals:Array = _rentals; var result:String = get_name() +" n"; var result:String = get_name() +" n"; while (rentals.length > 0) while (rentals.length > 0) { { var this_amount:Number = 0; var this_amount:Number = 0; var the_rental:Rental = rentals.shift() as Rental; var the_rental:Rental = rentals.shift() as Rental; this_amount = amount_for(the_rental); this_amount = the_rental.get_charge(); // // bonus_points += 1; bonus_points += 1; ... ... } } result += "n " + total_amount + "n"; result += "n " + total_amount + "n"; result += " " + bonus_points + " "; result += " " + bonus_points + " "; return result; return result; } } .. .. } }
  38. 38. // Customer.as // Customer.as public class Customer public class Customer { { .. .. public function checkout ():String public function checkout ():String { { var total_amount:Number = 0;// var total_amount:Number = 0;// var bonus_points:int = 0;// var bonus_points:int = 0;// var rentals:Array = _rentals; var rentals:Array = _rentals; var result:String = get_name() +" n"; var result:String = get_name() +" n"; while (rentals.length > 0) while (rentals.length > 0) { { var this_amount:Number = 0; var this_amount:Number = 0; var the_rental:Rental = rentals.shift() as Rental; var the_rental:Rental = rentals.shift() as Rental; this_amount = amount_for(the_rental); this_amount = the_rental.get_charge(); // // bonus_points += 1; bonus_points += 1; ... ... } } result += "n " + total_amount + "n"; result += "n " + total_amount + "n"; result += " " + bonus_points + " "; result += " " + bonus_points + " "; return result; return result; } } .. .. } }
  39. 39. Replace Temp with Query ...
  40. 40. // Customer.as // Customer.as public function checkout ():String public function checkout ():String { { var total_amount:Number = 0;// var total_amount:Number = 0;// var bonus_points:int = 0;// var bonus_points:int = 0;// var rentals:Array = _rentals; var rentals:Array = _rentals; var result:String = get_name() +" n"; var result:String = get_name() +" n"; while (rentals.length > 0) while (rentals.length > 0) { { var this_amount:Number = 0; var the_rental:Rental = rentals.shift() as Rental; var the_rental:Rental = rentals.shift() as Rental; // this_amount = amount_for(the_rental); bonus_points += 1; // // 2 1 bonus_points += 1; if ((the_rental.get_movie().get_price_code() == Movie.NEW_RELEASE) && (the_rental.get_days_rented() > 1)) // 2 1 { if ((the_rental.get_movie().get_price_code() == bonus_points += 1; Movie.NEW_RELEASE) && (the_rental.get_days_rented() > 1)) } { result += "-" + the_rental.get_movie().get_title() + " " + bonus_points += 1; the_rental.get_charge() +" n"; } total_amount += the_rental.get_charge(); result += "-" + the_rental.get_movie().get_title() + " " + } this_amount +" n"; total_amount += this_amount; result += "n " + total_amount + "n"; } result += " " + bonus_points + " "; result += "n " + total_amount + "n"; return result; result += " " + bonus_points + " "; } return result; }
  41. 41. // Customer.as // Customer.as public function checkout ():String public function checkout ():String { { var total_amount:Number = 0;// var total_amount:Number = 0;// var bonus_points:int = 0;// var bonus_points:int = 0;// var rentals:Array = _rentals; var rentals:Array = _rentals; var result:String = get_name() +" n"; var result:String = get_name() +" n"; while (rentals.length > 0) while (rentals.length > 0) { { var this_amount:Number = 0; var the_rental:Rental = rentals.shift() as Rental; var the_rental:Rental = rentals.shift() as Rental; // this_amount = amount_for(the_rental); bonus_points += 1; // // 2 1 bonus_points += 1; if ((the_rental.get_movie().get_price_code() == Movie.NEW_RELEASE) && (the_rental.get_days_rented() > 1)) // 2 1 { if ((the_rental.get_movie().get_price_code() == bonus_points += 1; Movie.NEW_RELEASE) && (the_rental.get_days_rented() > 1)) } { result += "-" + the_rental.get_movie().get_title() + " " + bonus_points += 1; the_rental.get_charge() +" n"; } total_amount += the_rental.get_charge(); result += "-" + the_rental.get_movie().get_title() + " " + } this_amount +" n"; total_amount += this_amount; result += "n " + total_amount + "n"; } result += " " + bonus_points + " "; result += "n " + total_amount + "n"; return result; result += " " + bonus_points + " "; } return result; }
  42. 42. // Customer.as // Customer.as public function checkout ():String public function checkout ():String { { var total_amount:Number = 0;// var total_amount:Number = 0;// var bonus_points:int = 0;// var bonus_points:int = 0;// var rentals:Array = _rentals; var rentals:Array = _rentals; var result:String = get_name() +" n"; var result:String = get_name() +" n"; while (rentals.length > 0) while (rentals.length > 0) { { var this_amount:Number = 0; var the_rental:Rental = rentals.shift() as Rental; var the_rental:Rental = rentals.shift() as Rental; // this_amount = amount_for(the_rental); bonus_points += 1; // // 2 1 bonus_points += 1; if ((the_rental.get_movie().get_price_code() == Movie.NEW_RELEASE) && (the_rental.get_days_rented() > 1)) // 2 1 { if ((the_rental.get_movie().get_price_code() == bonus_points += 1; Movie.NEW_RELEASE) && (the_rental.get_days_rented() > 1)) } { result += "-" + the_rental.get_movie().get_title() + " " + bonus_points += 1; the_rental.get_charge() +" n"; } total_amount += the_rental.get_charge(); result += "-" + the_rental.get_movie().get_title() + " " + } this_amount +" n"; total_amount += this_amount; result += "n " + total_amount + "n"; } result += " " + bonus_points + " "; result += "n " + total_amount + "n"; return result; result += " " + bonus_points + " "; } return result; }
  43. 43. Extract Method ...
  44. 44. // Customer.as // Customer.as public function checkout ():String public function checkout ():String { { ... while (rentals.length > 0) ... { while (rentals.length > 0) var this_amount:Number = 0; { var the_rental:Rental = rentals.shift() as Rental; var this_amount:Number = 0; var the_rental:Rental = rentals.shift() as Rental; this_amount = amount_for(the_rental); this_amount = amount_for(the_rental); // bonus_points += 1; // bonus_points += the_rental.get_bonus_points(); // 2 1 if ((the_rental.get_movie().get_price_code() == result += "-" + the_rental.get_movie().get_title() + " " + Movie.NEW_RELEASE) && (the_rental.get_days_rented() > 1)) this_amount +" n"; { total_amount += this_amount; bonus_points += 1; } } ... } result += "-" + the_rental.get_movie().get_title() + " " + this_amount +" n"; // Rental.as total_amount += this_amount; public class Rental } { ... .. } public function get_bonus_points():int { // 2 1 if ((get_movie().get_price_code() == Movie.NEW_RELEASE) && (get_days_rented() > 1)) { return 2; } else { return 1; } } .. }
  45. 45. // Customer.as // Customer.as public function checkout ():String public function checkout ():String { { ... while (rentals.length > 0) ... { while (rentals.length > 0) var this_amount:Number = 0; { var the_rental:Rental = rentals.shift() as Rental; var this_amount:Number = 0; var the_rental:Rental = rentals.shift() as Rental; this_amount = amount_for(the_rental); this_amount = amount_for(the_rental); // bonus_points += 1; // bonus_points += the_rental.get_bonus_points(); // 2 1 if ((the_rental.get_movie().get_price_code() == result += "-" + the_rental.get_movie().get_title() + " " + Movie.NEW_RELEASE) && (the_rental.get_days_rented() > 1)) this_amount +" n"; { total_amount += this_amount; bonus_points += 1; } } ... } result += "-" + the_rental.get_movie().get_title() + " " + this_amount +" n"; // Rental.as total_amount += this_amount; public class Rental } { ... .. } public function get_bonus_points():int { // 2 1 if ((get_movie().get_price_code() == Movie.NEW_RELEASE) && (get_days_rented() > 1)) { return 2; } else { return 1; } } .. }
  46. 46. // Customer.as // Customer.as public function checkout ():String public function checkout ():String { { ... while (rentals.length > 0) ... { while (rentals.length > 0) var this_amount:Number = 0; { var the_rental:Rental = rentals.shift() as Rental; var this_amount:Number = 0; var the_rental:Rental = rentals.shift() as Rental; this_amount = amount_for(the_rental); this_amount = amount_for(the_rental); // bonus_points += 1; // bonus_points += the_rental.get_bonus_points(); // 2 1 if ((the_rental.get_movie().get_price_code() == result += "-" + the_rental.get_movie().get_title() + " " + Movie.NEW_RELEASE) && (the_rental.get_days_rented() > 1)) this_amount +" n"; { total_amount += this_amount; bonus_points += 1; } } ... } result += "-" + the_rental.get_movie().get_title() + " " + this_amount +" n"; // Rental.as total_amount += this_amount; public class Rental } { ... .. } public function get_bonus_points():int { // 2 1 if ((get_movie().get_price_code() == Movie.NEW_RELEASE) && (get_days_rented() > 1)) { return 2; } else { return 1; } } .. }
  47. 47. // Customer.as // Customer.as public function checkout ():String public function checkout ():String { { ... while (rentals.length > 0) ... { while (rentals.length > 0) var this_amount:Number = 0; { var the_rental:Rental = rentals.shift() as Rental; var this_amount:Number = 0; var the_rental:Rental = rentals.shift() as Rental; this_amount = amount_for(the_rental); this_amount = amount_for(the_rental); // bonus_points += 1; // bonus_points += the_rental.get_bonus_points(); // 2 1 if ((the_rental.get_movie().get_price_code() == result += "-" + the_rental.get_movie().get_title() + " " + Movie.NEW_RELEASE) && (the_rental.get_days_rented() > 1)) this_amount +" n"; { total_amount += this_amount; bonus_points += 1; } } ... } result += "-" + the_rental.get_movie().get_title() + " " + this_amount +" n"; // Rental.as total_amount += this_amount; public class Rental } { ... .. } public function get_bonus_points():int { // 2 1 if ((get_movie().get_price_code() == Movie.NEW_RELEASE) && (get_days_rented() > 1)) { return 2; } else { return 1; } } .. }
  48. 48. Replace Temp with Query ...
  49. 49. // Customer.as // Customer.as public class Customer public class Customer { { ... public function checkout ():String public function checkout ():String { { var total_amount:Number = 0;// var total_amount:Number = 0;// var bonus_points:int = 0;// var bonus_points:int = 0;// var rentals:Array = _rentals.concat(); var rentals:Array = _rentals.concat(); var result:String = get_name() +" n"; var result:String = get_name() +" n"; while (rentals.length > 0) while (rentals.length > 0) { { var the_rental:Rental = rentals.shift() as Rental; var the_rental:Rental = rentals.shift() as Rental; // // bonus_points += the_rental.get_bonus_points(); bonus_points += the_rental.get_bonus_points(); result += "-" + the_rental.get_movie().get_title() + " " result += "-" + the_rental.get_movie().get_title() + " " + the_rental.get_charge() +" n"; + the_rental.get_charge() +" n"; } total_amount += the_rental.get_charge(); } result += "n " + get_total_charge() + "n"; result += " " + bonus_points + " "; result += "n " + total_amount + "n"; result += " " + bonus_points + " "; return result; } return result; } private function get_total_charge():Number ... { } var rentals:Array = _rentals.concat(); var result:Number = 0; while (rentals.length > 0) { var the_rental:Rental = rentals.shift() as Rental; result += the_rental.get_charge(); } return result; } }
  50. 50. // Customer.as // Customer.as public class Customer public class Customer { { ... public function checkout ():String public function checkout ():String { { var total_amount:Number = 0;// var total_amount:Number = 0;// var bonus_points:int = 0;// var bonus_points:int = 0;// var rentals:Array = _rentals.concat(); var rentals:Array = _rentals.concat(); var result:String = get_name() +" n"; var result:String = get_name() +" n"; while (rentals.length > 0) while (rentals.length > 0) { { var the_rental:Rental = rentals.shift() as Rental; var the_rental:Rental = rentals.shift() as Rental; // // bonus_points += the_rental.get_bonus_points(); bonus_points += the_rental.get_bonus_points(); result += "-" + the_rental.get_movie().get_title() + " " result += "-" + the_rental.get_movie().get_title() + " " + the_rental.get_charge() +" n"; + the_rental.get_charge() +" n"; } total_amount += the_rental.get_charge(); } result += "n " + get_total_charge() + "n"; result += " " + bonus_points + " "; result += "n " + total_amount + "n"; result += " " + bonus_points + " "; return result; } return result; } private function get_total_charge():Number ... { } var rentals:Array = _rentals.concat(); var result:Number = 0; while (rentals.length > 0) { var the_rental:Rental = rentals.shift() as Rental; result += the_rental.get_charge(); } return result; } }
  51. 51. // Customer.as // Customer.as public class Customer public class Customer { { ... public function checkout ():String public function checkout ():String { { var total_amount:Number = 0;// var total_amount:Number = 0;// var bonus_points:int = 0;// var bonus_points:int = 0;// var rentals:Array = _rentals.concat(); var rentals:Array = _rentals.concat(); var result:String = get_name() +" n"; var result:String = get_name() +" n"; while (rentals.length > 0) while (rentals.length > 0) { { var the_rental:Rental = rentals.shift() as Rental; var the_rental:Rental = rentals.shift() as Rental; // // bonus_points += the_rental.get_bonus_points(); bonus_points += the_rental.get_bonus_points(); result += "-" + the_rental.get_movie().get_title() + " " result += "-" + the_rental.get_movie().get_title() + " " + the_rental.get_charge() +" n"; + the_rental.get_charge() +" n"; } total_amount += the_rental.get_charge(); } result += "n " + get_total_charge() + "n"; result += " " + bonus_points + " "; result += "n " + total_amount + "n"; result += " " + bonus_points + " "; return result; } return result; } private function get_total_charge():Number ... { } var rentals:Array = _rentals.concat(); var result:Number = 0; while (rentals.length > 0) { var the_rental:Rental = rentals.shift() as Rental; result += the_rental.get_charge(); } return result; } }
  52. 52. // Customer.as // Customer.as public class Customer public class Customer { { ... public function checkout ():String public function checkout ():String { { var total_amount:Number = 0;// var total_amount:Number = 0;// var bonus_points:int = 0;// var bonus_points:int = 0;// var rentals:Array = _rentals.concat(); var rentals:Array = _rentals.concat(); var result:String = get_name() +" n"; var result:String = get_name() +" n"; while (rentals.length > 0) while (rentals.length > 0) { { var the_rental:Rental = rentals.shift() as Rental; var the_rental:Rental = rentals.shift() as Rental; // // bonus_points += the_rental.get_bonus_points(); bonus_points += the_rental.get_bonus_points(); result += "-" + the_rental.get_movie().get_title() + " " result += "-" + the_rental.get_movie().get_title() + " " + the_rental.get_charge() +" n"; + the_rental.get_charge() +" n"; } total_amount += the_rental.get_charge(); } result += "n " + get_total_charge() + "n"; result += " " + bonus_points + " "; result += "n " + total_amount + "n"; result += " " + bonus_points + " "; return result; } return result; } private function get_total_charge():Number ... { } var rentals:Array = _rentals.concat(); var result:Number = 0; while (rentals.length > 0) { var the_rental:Rental = rentals.shift() as Rental; result += the_rental.get_charge(); } return result; } }
  53. 53. // Customer.as // Customer.as public class Customer public class Customer { { .. .. public function checkout ():String public function checkout ():String { { var total_amount:Number = 0;// var total_amount:Number = 0;// var bonus_points:int = 0;// var bonus_points:int = 0;// var rentals:Array = _rentals.concat(); var rentals:Array = _rentals.concat(); var result:String = get_name() +" n"; var result:String = get_name() +" n"; while (rentals.length > 0) while (rentals.length > 0) { { var the_rental:Rental = rentals.shift() as Rental; var the_rental:Rental = rentals.shift() as Rental; // result += "-" + the_rental.get_movie().get_title() + " " bonus_points += the_rental.get_bonus_points(); + the_rental.get_charge() +" n"; } result += "-" + the_rental.get_movie().get_title() + " " + the_rental.get_charge() +" n"; result += "n " + get_total_charge() + "n"; total_amount += the_rental.get_charge(); result += " " + get_total_bonus_points() + " "; } return result; result += "n " + total_amount + "n"; } result += " " + bonus_points + " "; private function get_total_bonus_points():int return result; { } var rentals:Array = _rentals.concat(); .. var result:int = 0; } while (rentals.length > 0) { var the_rental:Rental = rentals.shift() as Rental; result += the_rental.get_bonus_points(); } return result; } .. }
  54. 54. // Customer.as // Customer.as public class Customer public class Customer { { .. .. public function checkout ():String public function checkout ():String { { var total_amount:Number = 0;// var total_amount:Number = 0;// var bonus_points:int = 0;// var bonus_points:int = 0;// var rentals:Array = _rentals.concat(); var rentals:Array = _rentals.concat(); var result:String = get_name() +" n"; var result:String = get_name() +" n"; while (rentals.length > 0) while (rentals.length > 0) { { var the_rental:Rental = rentals.shift() as Rental; var the_rental:Rental = rentals.shift() as Rental; // result += "-" + the_rental.get_movie().get_title() + " " bonus_points += the_rental.get_bonus_points(); + the_rental.get_charge() +" n"; } result += "-" + the_rental.get_movie().get_title() + " " + the_rental.get_charge() +" n"; result += "n " + get_total_charge() + "n"; total_amount += the_rental.get_charge(); result += " " + get_total_bonus_points() + " "; } return result; result += "n " + total_amount + "n"; } result += " " + bonus_points + " "; private function get_total_bonus_points():int return result; { } var rentals:Array = _rentals.concat(); .. var result:int = 0; } while (rentals.length > 0) { var the_rental:Rental = rentals.shift() as Rental; result += the_rental.get_bonus_points(); } return result; } .. }
  55. 55. // Customer.as // Customer.as public class Customer public class Customer { { .. .. public function checkout ():String public function checkout ():String { { var total_amount:Number = 0;// var total_amount:Number = 0;// var bonus_points:int = 0;// var bonus_points:int = 0;// var rentals:Array = _rentals.concat(); var rentals:Array = _rentals.concat(); var result:String = get_name() +" n"; var result:String = get_name() +" n"; while (rentals.length > 0) while (rentals.length > 0) { { var the_rental:Rental = rentals.shift() as Rental; var the_rental:Rental = rentals.shift() as Rental; // result += "-" + the_rental.get_movie().get_title() + " " bonus_points += the_rental.get_bonus_points(); + the_rental.get_charge() +" n"; } result += "-" + the_rental.get_movie().get_title() + " " + the_rental.get_charge() +" n"; result += "n " + get_total_charge() + "n"; total_amount += the_rental.get_charge(); result += " " + get_total_bonus_points() + " "; } return result; result += "n " + total_amount + "n"; } result += " " + bonus_points + " "; private function get_total_bonus_points():int return result; { } var rentals:Array = _rentals.concat(); .. var result:int = 0; } while (rentals.length > 0) { var the_rental:Rental = rentals.shift() as Rental; result += the_rental.get_bonus_points(); } return result; } .. }
  56. 56. // Customer.as // Customer.as public class Customer public class Customer { { .. .. public function checkout ():String public function checkout ():String { { var total_amount:Number = 0;// var total_amount:Number = 0;// var bonus_points:int = 0;// var bonus_points:int = 0;// var rentals:Array = _rentals.concat(); var rentals:Array = _rentals.concat(); var result:String = get_name() +" n"; var result:String = get_name() +" n"; while (rentals.length > 0) while (rentals.length > 0) { { var the_rental:Rental = rentals.shift() as Rental; var the_rental:Rental = rentals.shift() as Rental; // result += "-" + the_rental.get_movie().get_title() + " " bonus_points += the_rental.get_bonus_points(); + the_rental.get_charge() +" n"; } result += "-" + the_rental.get_movie().get_title() + " " + the_rental.get_charge() +" n"; result += "n " + get_total_charge() + "n"; total_amount += the_rental.get_charge(); result += " " + get_total_bonus_points() + " "; } return result; result += "n " + total_amount + "n"; } result += " " + bonus_points + " "; private function get_total_bonus_points():int return result; { } var rentals:Array = _rentals.concat(); .. var result:int = 0; } while (rentals.length > 0) { var the_rental:Rental = rentals.shift() as Rental; result += the_rental.get_bonus_points(); } return result; } .. }
  57. 57. Move Method ...
  58. 58. // Rental.as // Rental.as public class Rental public class Rental { { ... public function get_charge():Number public function get_charge():Number { { return _movie.get_charge(_days_rented); var result:int = 0; } switch (get_movie().get_price_code()) } { case Movie.REGULAR :// // Movie.as result += 2; public class Movie if (get_days_rented() > 2) { { public function get_charge(days_rented:int):Number result += (get_days_rented() - 2) * 1.5; { } var result:int = 0; break; switch (get_price_code()) { case Movie.NEW_RELEASE :// case Movie.REGULAR :// result += get_days_rented() * 3; result += 2; break; if (days_rented > 2) { case Movie.CHILDRENS :// result += (days_rented - 2) * 1.5; result += 1.5; } if (get_days_rented() > 3) break; { result += (get_days_rented() - 3) * 1.5; case Movie.NEW_RELEASE :// } result += days_rented * 3; break; break; } case Movie.CHILDRENS :// return result; result += 1.5; } if (days_rented > 3) .. { } result += (days_rented - 3) * 1.5; } break; } return result; } }
  59. 59. // Rental.as // Rental.as public class Rental public class Rental { { ... public function get_charge():Number public function get_charge():Number { { return _movie.get_charge(_days_rented); var result:int = 0; } switch (get_movie().get_price_code()) } { case Movie.REGULAR :// // Movie.as result += 2; public class Movie if (get_days_rented() > 2) { { public function get_charge(days_rented:int):Number result += (get_days_rented() - 2) * 1.5; { } var result:int = 0; break; switch (get_price_code()) { case Movie.NEW_RELEASE :// case Movie.REGULAR :// result += get_days_rented() * 3; result += 2; break; if (days_rented > 2) { case Movie.CHILDRENS :// result += (days_rented - 2) * 1.5; result += 1.5; } if (get_days_rented() > 3) break; { result += (get_days_rented() - 3) * 1.5; case Movie.NEW_RELEASE :// } result += days_rented * 3; break; break; } case Movie.CHILDRENS :// return result; result += 1.5; } if (days_rented > 3) .. { } result += (days_rented - 3) * 1.5; } break; } return result; } }
  60. 60. // Rental.as // Rental.as public class Rental public class Rental { { ... public function get_charge():Number public function get_charge():Number { { return _movie.get_charge(_days_rented); var result:int = 0; } switch (get_movie().get_price_code()) } { case Movie.REGULAR :// // Movie.as result += 2; public class Movie if (get_days_rented() > 2) { { public function get_charge(days_rented:int):Number result += (get_days_rented() - 2) * 1.5; { } var result:int = 0; break; switch (get_price_code()) { case Movie.NEW_RELEASE :// case Movie.REGULAR :// result += get_days_rented() * 3; result += 2; break; if (days_rented > 2) { case Movie.CHILDRENS :// result += (days_rented - 2) * 1.5; result += 1.5; } if (get_days_rented() > 3) break; { result += (get_days_rented() - 3) * 1.5; case Movie.NEW_RELEASE :// } result += days_rented * 3; break; break; } case Movie.CHILDRENS :// return result; result += 1.5; } if (days_rented > 3) .. { } result += (days_rented - 3) * 1.5; } break; } return result; } }
  61. 61. // Rental.as // Rental.as public class Rental public class Rental { { ... public function get_charge():Number public function get_charge():Number { { return _movie.get_charge(_days_rented); var result:int = 0; } switch (get_movie().get_price_code()) } { case Movie.REGULAR :// // Movie.as result += 2; public class Movie if (get_days_rented() > 2) { { public function get_charge(days_rented:int):Number result += (get_days_rented() - 2) * 1.5; { } var result:int = 0; break; switch (get_price_code()) { case Movie.NEW_RELEASE :// case Movie.REGULAR :// result += get_days_rented() * 3; result += 2; break; if (days_rented > 2) { case Movie.CHILDRENS :// result += (days_rented - 2) * 1.5; result += 1.5; } if (get_days_rented() > 3) break; { result += (get_days_rented() - 3) * 1.5; case Movie.NEW_RELEASE :// } result += days_rented * 3; break; break; } case Movie.CHILDRENS :// return result; result += 1.5; } if (days_rented > 3) .. { } result += (days_rented - 3) * 1.5; } break; } return result; } }
  62. 62. // Rental.as // Rental.as public class Rental public class Rental { { ... .. public function get_bonus_points():int public function get_charge():Number { { // 2 1 return _movie.get_bonus_points(_days_rented); if ((get_movie().get_price_code() == Movie.NEW_RELEASE) && } (get_days_rented() > 1)) .. { } return 2; } // Movie.as else public class Movie { { return 1; .. } public function get_bonus_points(days_rented:int):int } { .. // 2 1 } if ((get_price_code() == Movie.NEW_RELEASE) && (days_rented > 1)) { return 2; } else { return 1; } } .. }

×