重构:switch语句改成策略模式还是状态模式

来源:互联网 发布:淘宝评价回复语大全 编辑:程序博客网 时间:2024/06/06 16:25

在重构篇里,可以用多态来取代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());    }}
1 0
原创粉丝点击