38. 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) {
_title = title;
_priceCode = priceCode;
}
public int getPriceCode() {
return _priceCode;
}
public void setPriceCode(int arg) {
_priceCode = arg;
}
public String getTitle (){
return _title;
}
}
class Rental {
private Movie _movie;
private int _daysRented;
public Rental(Movie movie, int daysRented) {
_movie = movie;
_daysRented = daysRented;
}
public int getDaysRented() {
return _daysRented;
}
public Movie getMovie() {
return _movie;
}
}
Movie
212. public String statement() {
double totalAmount = 0;
int frequentRenterPoints = 0;
Enumeration rentals = _rentals.elements();
String result = Rental Record for + getName() + n;
while (rentals.hasMoreElements()) {
double thisAmount = 0;
Rental each = (Rental) rentals.nextElement();
thisAmount = each.getCharge();
// add frequent renter points
frequentRenterPoints ++;
// add bonus for a two day new release rental
if ((each.getMovie().getPriceCode() ==
Movie.NEW_RELEASE)
each.getDaysRented() 1)
frequentRenterPoints ++;
//show figures for this rental
result += t + each.getMovie().getTitle()+ t +
String.valueOf(each.getCharge()) + n;
totalAmount += each.getCharge();
}
โฆ
}
public String statement() {
double totalAmount = 0;
int frequentRenterPoints = 0;
Enumeration rentals = _rentals.elements();
String result = Rental Record for + getName() + n;
while (rentals.hasMoreElements()) {
double thisAmount = 0;
Rental each = (Rental) rentals.nextElement();
thisAmount = each.getCharge();
// add frequent renter points
frequentRenterPoints ++;
// add bonus for a two day new release rental
if ((each.getMovie().getPriceCode() ==
Movie.NEW_RELEASE)
each.getDaysRented() 1)
frequentRenterPoints ++;
//show figures for this rental
result += t + each.getMovie().getTitle()+ t +
String.valueOf(thisAmount) + n;
totalAmount += thisAmount;
}
โฆ
}
์ค๋ณต๋
304. ย ์์ฑ
public String htmlStatement() {
Enumeration rentals = _rentals.elements();
String result = H1Rentals for EM + getName() + /EM/H1Pn;
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
result += each.getMovie().getTitle()+ : + String.valueOf(each.getCharge()) + BRn;
}
!
result += PYou owe EM + String.valueOf(getTotalCharge()) + /EMPn;
result += On this rental you earned EM +
String.valueOf(getTotalFrequentRenterPoints()) + /EM frequent renter pointsP;
return result;
}
โข ๋ฆฌํฉํ ๋ง์
378. ย ๊ฒฐ๊ณผ
44
Figure 1.17. Class diagram after addition of the state pattern
Final Thoughts
This is a simple example, yet I hope it gives you the feeling of what refactoring is like. I've used
several refactorings, including Extract Method, Move Method, and Replace Conditional
with Polymorphism. All these lead to better-distributed responsibilities and code that is easier
to maintain. It does look rather different from procedural style code, and that takes some getting
used to. But once you are used to it, it is hard to go back to procedural programs.
13
So I'm going to start this book with an example of refactoring. During the process I'll tell you a lot
about how refactoring works and give you a sense of the process of refactoring. I can then
provide the usual principles-style introduction.
With an introductory example, however, I run into a big problem. If I pick a large program,
describing it and how it is refactored is too complicated for any reader to work through. (I tried
and even a slightly complicated example runs to more than a hundred pages.) However, if I pick a
program that is small enough to be comprehensible, refactoring does not look like it is worthwhile.
Thus I'm in the classic bind of anyone who wants to describe techniques that are useful for real-
world programs. Frankly it is not worth the effort to do the refactoring that I'm going to show you
on a small program like the one I'm going to use. But if the code I'm showing you is part of a
larger system, then the refactoring soon becomes important. So I have to ask you to look at this
and imagine it in the context of a much larger system.
The Starting Point
The sample program is very simple. It is a program to calculate and print a statement of a
customer's charges at a video store. The program is told which movies a customer rented and for
how long. It then calculates the charges, which depend on how long the movie is rented, and
identifies the type movie. There are three kinds of movies: regular, children's, and new releases.
In addition to calculating charges, the statement also computes frequent renter points, which vary
depending on whether the film is a new release.
Several classes represent various video elements. Here's a class diagram to show them (Figure
1.1).
Figure 1.1. Class diagram of the starting-point classes. Only the most important features
are shown. The notation is Unified Modeling Language UML [Fowler, UML].
I'll show the code for each of these classes in turn.
Movie
Movie is just a simple data class.
โข ๋ฆฌํฉํ ๋ง