重构: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
- 重构:switch语句改成策略模式还是状态模式
- 【重构】使用简单工厂模式重构Switch语句
- 状态模式&策略模式
- 【机房重构】策略模式
- 策略模式与状态模式
- 状态模式和策略模式
- 策略模式 状态模式 比较
- 状态模式和策略模式
- 状态模式----pk------策略模式
- 策略模式 vs 状态模式
- 策略模式与状态模式
- 状态模式与策略模式
- 状态模式和策略模式
- 【机房重构】策略模式之下机
- 项目重构 策略模式应用
- 状态模式--策略模式--代理模式
- 模式开篇,策略模式,状态模式
- 设计模式笔记:状态模式&策略模式
- 强大的U盘启动盘制作工具Rufus
- 为安卓App加个退出键
- sun.misc.BASE64Encoder找不到jar包的解决方法
- 欢迎使用CSDN-markdown编辑器
- bzoj 4517: [Sdoi2016]排列计数
- 重构:switch语句改成策略模式还是状态模式
- java正则表达式去掉标点符号
- Springboot+Maven多模块开发 (一)初始化工程(新建第一个web工程)
- 代理模式
- 每日一练-----顺时针旋转矩阵
- java发送邮件
- java利用classloader实现热部署
- leetcode——268——Missing Number
- 计算质数(极速版)