1. 程式人生 > >重構:switch語句改成策略模式還是狀態模式

重構:switch語句改成策略模式還是狀態模式

在重構篇裡,可以用多型來取代switch語句,但是因為:一部影片可以在生命週期內修改自己的分類,一個物件卻不能在生命週期內修改自己所屬的類。所以這裡不能用策略模式,用多型取代switch,而應該用狀態模式(State)。

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
Price _price; public Movie(String title, int priceCode) { // TODO Auto-generated constructor stub _title = title; setPriceCode(priceCode); } public int getPriceCode() { return _price.getPriceCode(); } public void setPriceCode(int arg) { switch
(arg) { case REGULAR: _price = new RegularPrice(); break; case CHILDRENS: _price = new ChildrensPrice(); break; case NEW_RELEASE: _price = new NewReleasePrice(); break; default: throw
new IllegalArgumentException("Incorrect Price Code"); // break; } } public String getTitle() { return _title; } public double getCharge(int daysRented) { return _price.getCharge(daysRented); } public int getFrequentRenterPoints(int daysRented) { return _price.getFrequentRenterPoints(daysRented); } }
public class Rental {
    private Movie _mMovie;
    private int _daysRented;

    public Rental(Movie movie, int daysRented) {
        // TODO Auto-generated constructor stub
        _mMovie = movie;
        _daysRented = daysRented;
    }

    public int getDaysRented() {
        return _daysRented;
    }

    public Movie getMovie() {
        return _mMovie;
    }
    public double getCharge() {
        return _mMovie.getCharge(_daysRented);
    }
    public int getFrequentRenterPoints() {
        return _mMovie.getFrequentRenterPoints(_daysRented);
    }
}
public class Customer {
    private String _name;
    private Vector<Rental> _rentals = new Vector<Rental>();;

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

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

    public String getName() {
        return _name;
    }

    public String statement() {
        Enumeration<Rental> rentals = _rentals.elements();
        String result = "Rental Record For " + getName() + "\n";
        while (rentals.hasMoreElements()) {

            Rental each = (Rental) rentals.nextElement();
            result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(each.getCharge()) + "\n";

        }
        result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";
        result += "You earned " + String.valueOf(getTotalFrequentRenterPoints()) + " frequent renter points";
        return result;
    }
    private double getTotalCharge() {
        double result = 0;
        Enumeration<Rental> rentals = _rentals.elements();
        while (rentals.hasMoreElements()) {
            Rental each = (Rental) rentals.nextElement();
            result += each.getCharge();
        }
        return result;
    }
    private int getTotalFrequentRenterPoints() {
        int result = 0;
        Enumeration<Rental> rentals = _rentals.elements();
        while (rentals.hasMoreElements()) {
            Rental each = (Rental) rentals.nextElement();
            result += each.getFrequentRenterPoints();
        }
        return result;
    }
}
public abstract class Price {
    abstract int getPriceCode();

    abstract double getCharge(int daysRented);
    public int getFrequentRenterPoints(int daysRented){
        return 1;
    }
}
public class ChildrensPrice extends Price {
    int getPriceCode() {
        return Movie.CHILDRENS;
    }
    double getCharge(int daysRented) {
        double result = 1.5;
        if(daysRented > 3)result += (daysRented -3)*1.5;
        return result;
    }
}
public class NewReleasePrice extends Price {
    int getPriceCode() {
        return Movie.NEW_RELEASE;
    }
    double getCharge(int daysRented) {
        return daysRented *3;
    }
    public int getFrequentRenterPoints(int daysRented){
        return (daysRented > 1)? 2:1;
    }
}
public class RegularPrice extends Price {
    int getPriceCode() {
        return Movie.REGULAR;
    }
    double getCharge(int daysRented) {
        double result = 2;
        if(daysRented > 2)result += (daysRented -2)* 1.5;
        return result;
    }
}
public class Client {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Customer _mCustomer = new Customer("Nick");
         _mCustomer.addRental(new Rental(new Movie("Avata", 0),4));
         _mCustomer.addRental(new Rental(new Movie("WarHome", 1),2));
         _mCustomer.addRental(new Rental(new Movie("Tatanic", 2), 5));
         System.out.println(_mCustomer.statement());
    }
}