重构学习实例

来源:互联网 发布:韶关仁化网络问政平台 编辑:程序博客网 时间:2024/06/07 01:24

重构前的代码:

package com.jenny.refactor.example1;


import java.util.Enumeration;
import java.util.Vector;


public class Customer
{
private String _name;// 姓名
private Vector<Rental> _rentals = new Vector<Rental>();// 租借记录


public Customer(String name)
{
this._name = name;
}


public String getName()
{
return _name;
}


public void addRental(Rental arg)
{
this._rentals.addElement(arg);
}


public String statement()
{
double totalAmount = 0;// 总消费金额
int frequentRenterPoints = 0;// 常客积点
Enumeration rentals = this._rentals.elements();
String result = "Rental Record for" + get_name() + "n";

while(rentals.hasMoreElements())
{
double thisAmount = 0;
Rental each = (Rental)rentals.nextElement();

// determin amounts for each line
switch(each.get_movie().get_priceCode())
{
case Movie.REGULAR:
thisAmount += 2;
if(each.get_dayRented() > 2)
thisAmount += (each.get_dayRented() - 2) * 1.5;
break;
case Movie.NEW_RELEASE:
thisAmount += each.get_dayRented() * 3;
break;
case Movie.CHILDRENS:
thisAmount += 1.5;
if(each.get_dayRented() > 3)
thisAmount += (each.get_dayRented() - 3) * 1.5;
break;
}

// add frequent renter points(累加 常客积点)
frequentRenterPoints++;

// add bonus for a two day new release rental
if((each.get_movie().get_priceCode()) == Movie.NEW_RELEASE
&& each.get_dayRented() > 1)
frequentRenterPoints++;
// show figures for this rental(显示此笔租借数据)
result += "t" + each.get_movie().get_title() + "t"
+ String.valueOf(thisAmount) + "n";

totalAmount += thisAmount;
}

// add footer lines(结尾打印)
result += "Amount owed is " + String.valueOf(totalAmount) + "n";
result += "You earned " + String.valueOf(frequentRenterPoints)
+ "frequent renter points";
return result;
}
}

package com.jenny.refactor.example1;


public class Movie
{
public static final int CHILDRENS = 2;
public static final int REGULAR = 0;
public static final int NEW_RELEASE = 1;

private String _title; // 名称
private int _priceCode;// 价格


public Movie(String _title, int _priceCode)
{
this._title = _title;
this._priceCode = _priceCode;
}


/**
* @return 返回 _priceCode。
*/
public int get_priceCode()
{
return _priceCode;
}


/**
* @param code
* 要设置的 _priceCode。
*/
public void set_priceCode(int code)
{
_priceCode = code;
}


/**
* @return 返回 _title。
*/
public String get_title()
{
return _title;
}
}

package com.jenny.refactor.example1;


public class Rental
{
private Movie _movie;// 影片
private int _dayRented;// 租期


public Rental(Movie _movie, int _dayRented)
{
this._movie = _movie;
this._dayRented = _dayRented;
}


public int get_dayRented()
{
return _dayRented;
}


public Movie get_movie()
{
return _movie;
}
}

重构后的代码:

package com.jenny.refactor.example1;

import java.util.Enumeration;
import java.util.Vector;

public class Customer {
 
 private String _name;// 姓名
 private Vector<Rental> _rentals = new Vector<Rental>();// 租借记录

 public Customer(String name) {
  // TODO Auto-generated constructor stub
  this._name = name;
 }

 /**
  * @return the _name
  */
 public String get_name() {
  return _name;
 }

 public void addRental(Rental arg)
 {
  this._rentals.addElement(arg);
 }


 public String statement()
 {
// double totalAmount = 0;// 总消费金额
// int frequentRenterPoints = 0;// 常客积点
 Enumeration rentals = this._rentals.elements();
 String result = "Rental Record for " + get_name() + ":/n";

 while(rentals.hasMoreElements())
 {
//  double thisAmount = 0;
  Rental each = (Rental)rentals.nextElement();
  // determin amounts for each line
//  thisAmount=each.getAmount();
 
  // add frequent renter points(累加 常客积点)
//  frequentRenterPoints++;
  // add bonus for a two day new release rental
//  frequentRenterPoints+=each.getFrequentRenterPoints();
  // show figures for this rental(显示此笔租借数据)
  result += each.get_movie().get_title() + ":"+ String.valueOf(each.getAmount()) + "/n";
//  totalAmount += each.getAmount();
 }

 // add footer lines(结尾打印)
 result += "Amount owed is " + String.valueOf(getTotalAmout()) + "/n";
 result += "You earned " + String.valueOf(getTotalFrequentRenterPoints())
 + " frequent renter points.";
 return result;
 }
 
 public double getTotalAmout(){
  Enumeration rentals = this._rentals.elements();
  double result=0;
  while(rentals.hasMoreElements())
  {
   Rental each = (Rental)rentals.nextElement();
   result+= each.getAmount();
  }
  return result;
 }
 
 public int getTotalFrequentRenterPoints(){
  Enumeration rentals = this._rentals.elements();
  int result=0;
  while(rentals.hasMoreElements())
  {
   Rental each = (Rental)rentals.nextElement();
   result+= each.getFrequentRenterPoints();
  }
  return result;
 }
 
// public double getAmount(Rental each){
//  return each.getAmount();
// }
 
 public String htmlStatement()
 {
 Enumeration rentals = this._rentals.elements();
 String result = "<H1>Rentals for <EM>" + get_name() + "</EM></H1><p>/n";

 while(rentals.hasMoreElements())
 {
  Rental each = (Rental)rentals.nextElement();
  result += each.get_movie().get_title() + ": "+ String.valueOf(each.getAmount()) + "<BR>/n";
 }

 // add footer lines(结尾打印)
 result += "<p>You owe <EM>" + String.valueOf(getTotalAmout()) + "</EM><p>/n";
 result += "on this rental you earned <EM>" + String.valueOf(getTotalFrequentRenterPoints())
 + "</EM> frequent renter points<p>";
 return result;
 }

}
package com.jenny.refactor.example1;

public class Movie {

 public static final int CHILDRENS = 2;
 public static final int REGULAR = 0;
 public static final int NEW_RELEASE = 1;

 private String _title; // 名称
 private int _priceCode;// 价格

 private Price _price;

 public Movie(String _title, int _priceCode) {
  // TODO Auto-generated constructor stub
  this._title = _title;
  set_priceCode(_priceCode);
 }


 /**
  * @return the _priceCode
  */
 public int get_priceCode() {
  return _priceCode;
 }


 /**
  * @param code the _priceCode to set
  */
 public void set_priceCode(int code) {
  switch(code){
  case REGULAR:
   _price=new RegularPrice();
   break;
  case NEW_RELEASE:
   _price=new NewReleasePrice();
   break;
  case CHILDRENS:
   _price=new ChildrensPrice();

   break;
  default:
   throw new IllegalArgumentException("Incorrect Price Code!");
  }
 }


 /**
  * @return the _title
  */
 public String get_title() {
  return _title;
 }

 public double getAmount(int dayRented){
  return _price.getAmount(dayRented);
 }
 
 public int getFrequentRenterPoints(int dayRented){
  return _price.getFrequentRenterPoints(dayRented);
 }
}
package com.jenny.refactor.example1;

public class Rental {

 private Movie _movie;// 影片
 private int _dayRented;// 租期

 public Rental(Movie _movie, int _dayRented) {
  // TODO Auto-generated constructor stub
  this._movie = _movie;
  this._dayRented = _dayRented;

 }

 /**
  * @return the _movie
  */
 public Movie get_movie() {
  return _movie;
 }

 /**
  * @return the _dayRented
  */
 public int get_dayRented() {
  return _dayRented;
 }

 public double getAmount(){
  return _movie.getAmount(_dayRented);
 }
 
 public int getFrequentRenterPoints(){
  return _movie.getFrequentRenterPoints(_dayRented);
 }
}
package com.jenny.refactor.example1;

public abstract class Price {
 
 public abstract double getAmount(int dayRented);
 
 public int getFrequentRenterPoints(int dayRented){
   return 1;
 }

}
package com.jenny.refactor.example1;

public class ChildrensPrice extends Price {

 @Override
 public double getAmount(int dayRented) {
  // TODO Auto-generated method stub
  double result = 1.5;
  if(dayRented > 3)
   result += (dayRented - 3) * 1.5;
  return result;
 }

}package com.jenny.refactor.example1;

public class RegularPrice extends Price {

 @Override
 public double getAmount(int dayRented) {
  // TODO Auto-generated method stub
  double result= 2;
  if(dayRented > 2)
   result += (dayRented - 2) * 1.5;
  return result;
 }

}
package com.jenny.refactor.example1;

public class NewReleasePrice extends Price {

 @Override
 public double getAmount(int dayRented) {
  // TODO Auto-generated method stub
  return dayRented * 3;
 }
 
 @Override
 public int getFrequentRenterPoints(int dayRented) {
  // TODO Auto-generated method stub
  return (dayRented>1?2:1);
 }

}package com.jenny.refactor.example1;

public class Run {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Customer user1=new Customer("admin");
  user1.addRental(new Rental(new Movie("天龙八部1",0),3));
  user1.addRental(new Rental(new Movie("奋斗1",0),1));
  user1.addRental(new Rental(new Movie("兄弟1",0),1));
  System.out.println(user1.statement());
  System.out.println(user1.htmlStatement());
  Customer user2=new Customer("admin");
  user2.addRental(new Rental(new Movie("天龙八部2",1),5));
  System.out.println(user2.statement());
  Customer user3=new Customer("admin");
  user3.addRental(new Rental(new Movie("天龙八部3",2),5));
  System.out.println(user3.statement());

 }

}
运行结果:

Rental Record for admin:
天龙八部1:3.5
奋斗1:2.0
兄弟1:2.0
Amount owed is 7.5
You earned 3 frequent renter points.
<H1>Rentals for <EM>admin</EM></H1><p>
天龙八部1: 3.5<BR>
奋斗1: 2.0<BR>
兄弟1: 2.0<BR>
<p>You owe <EM>7.5</EM><p>
on this rental you earned <EM>3</EM> frequent renter points<p>
Rental Record for admin:
天龙八部2:15.0
Amount owed is 15.0
You earned 2 frequent renter points.
Rental Record for admin:
天龙八部3:4.5
Amount owed is 4.5
You earned 1 frequent renter points.

原创粉丝点击