最近公司作了个项目,深深体会到架构设计以及代码优化有多么的重要。
回头看本身的代码都以为特别混乱,有时候还要看好久才能看懂,可扩展性特别差,彻底是为了完成需求而编码的。说得形象一点就像修水管,最后所有都漏水了。
我的以为代码重构很是有必要,写程序不但要给机器运行,更让人看的明白。
写代码如写诗同样才行。python
按照实例需求,常常都是相似这样子写代码的,以下:
Book书本类
主要是关于书名称和分类信息。git
/** * 书本 */ public class Book { 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 Book() { } public Book(String title, int priceCode) { this.title = title; this.priceCode = priceCode; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getPriceCode() { return priceCode; } public void setPriceCode(int priceCode) { this.priceCode = priceCode; } }
Rental 租借信息
主要是写租借信息,包括书和租借天数的关系。github
/** * 租借信息 */ public class Rental { private Book book; private int daysRented;//租借天数 public Rental() { } public Rental(Book book, int daysRented) { this.book = book; this.daysRented = daysRented; } public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } public int getDaysRented() { return daysRented; } public void setDaysRented(int daysRented) { this.daysRented = daysRented; } }
Customer 读者类
主要写租借费用计算以及租借的书的关系。golang
/** * 读者 */ public class Customer { private String name; private List<Rental> rentals = new ArrayList(); public Customer() { } public Customer(String name) { this.name = name; } //添加租书信息 public void addRental(Rental rental) { rentals.add(rental); } //生成订单 public String generateOrder() { double total = 0;//计算租借总数量 int frequentRenterPoints = 0;//计算积分 String result = "Rental Record for "+getName()+"\n"; for (Rental rental : rentals) { double thisAmount = 0; switch (rental.getBook().getPriceCode()){ case Book.REGULAR: thisAmount += 2; if (rental.getDaysRented() > 2){ thisAmount += (rental.getDaysRented() - 2) *1.5; } break; case Book.NEW_RELEASE: thisAmount += rental.getDaysRented()*3; break; case Book.CHILDRENS: thisAmount += 1.5; if (rental.getDaysRented() > 3){ thisAmount += (rental.getDaysRented() - 3) *1.5; } break; } frequentRenterPoints++; if ((rental.getBook().getPriceCode() == Book.NEW_RELEASE) && rental.getDaysRented() >1){ frequentRenterPoints++; } if ((rental.getBook().getPriceCode() == Book.NEW_RELEASE) && rental.getDaysRented() >1) { frequentRenterPoints++; } result += "\t"+rental.getBook().getTitle() + "\t"+String.valueOf(thisAmount)+"\n"; total +=thisAmount; } result += "Amount owed is "+ String.valueOf(total) +"\n"; result += "You earned "+ String.valueOf(frequentRenterPoints) +"frequent renter points"; return result; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
测试类:架构
/** * 一个图书馆出租书的程序。 * 计算每个读者的消费金额而且打印详情清单。 * 打印信息: * 读者租了哪些书、租期多长、根据租借时间和书的类型算出费用。 * 书分类:普通读本、少儿读本、新书 * 计算费用,以及计算积分。积分根据书的种类是否为新书而又有所不一样。 * */ public class Test { public static void main(String[] args) { Customer customer = new Customer(); Book book = new Book("Java入门到放弃", Book.NEW_RELEASE); Book book1 = new Book("python入门到放弃", Book.CHILDRENS); Book book2 = new Book("golang入门到放弃", Book.REGULAR); customer.addRental(new Rental(book,8)); customer.addRental(new Rental(book1,4)); customer.addRental(new Rental(book2,6)); customer.setName("zero"); System.out.println(customer.generateOrder()); } }
首先:分析一下上面的代码ide
接着:直接看下面的代码重构呗
Book类:
将按照书的不一样类型,按照不一样价格统计的方法移动到Book类中,由于这个按理应该属于Book类中的。测试
public class Book { 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 Book() { } public Book(String title, int priceCode) { this.title = title; this.priceCode = priceCode; } //1.提取统计钱的方法 public double getCharge(int daysRented) { double result = 0; switch (getPriceCode()) { case Book.REGULAR: result += 2; if (daysRented > 2) { result += (daysRented - 2) * 1.5; } break; case Book.NEW_RELEASE: result += daysRented * 3; break; case Book.CHILDRENS: result += 1.5; if (daysRented > 3) { result += (daysRented - 3) * 1.5; } break; } return result; } //1.提取计算会员积分的方法 public int getFrequentRenterPoints(int daysRented) { if ((getPriceCode() == Book.NEW_RELEASE) && daysRented > 1) { return 2; } return 1; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getPriceCode() { return priceCode; } public void setPriceCode(int priceCode) { this.priceCode = priceCode; } }
Rental 类:
主要是调用提取统计钱和积分的方法。大数据
public class Rental { private Book book; private int daysRented; public Rental() { } public Rental(Book book, int daysRented) { this.book = book; this.daysRented = daysRented; } //1.提取统计钱的方法 public double getCharge() { return book.getCharge(daysRented); } //1.提取计算会员积分的方法 public int getFrequentRenterPoints() { return book.getFrequentRenterPoints(daysRented); } public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } public int getDaysRented() { return daysRented; } public void setDaysRented(int daysRented) { this.daysRented = daysRented; } }
Customer 读者类
主要是去掉多余的临时变量total,frequentRenterPoints等。优化
public class Customer { private String name; private List<Rental> rentals = new ArrayList(); public Customer() { } public Customer(String name) { this.name = name; } //添加租书信息 public void addRental(Rental rental) { rentals.add(rental); } //生成订单 public String generateOrder() { String result = "Rental Record for " + getName() + "\n"; for (Rental rental : rentals) { result += "\t" + rental.getBook().getTitle() + "\t" + String.valueOf(rental.getCharge()) + "\n"; } result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n"; result += "You earned " + String.valueOf(getFrequentRenterPoints()) + "frequent renter points"; return result; } //获取购买总数 private double getTotalCharge() { double result = 0; for (Rental rental : rentals) { result += rental.getCharge(); } return result; } //统计积分 private double getFrequentRenterPoints() { double result = 0; for (Rental rental : rentals) { result += rental.getFrequentRenterPoints(); } return result; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
最后
测试结果跟上面的同样,就是将代码的结果调动一下。
如今大体的UML类图以下:this
通过第一次重构,仍是没有实现需求修改增长多个分类的效果。那么接下来使用接口抽象来再次重构。
Price接口
接口抽象两个规约方法,具体以下
public abstract class Price { abstract int getPriceCode(); //1.提取统计总价的方法 abstract double getCharge(int daysRented); //1.提取计算会员积分的方法 public int getFrequentRenterPoints(int daysRented) { return 1; } }
RegularPrice 普通的书价格类
public class RegularPrice extends Price { @Override int getPriceCode() { return Book.REGULAR; } @Override public double getCharge(int daysRented) { double result = 2; if (daysRented >2) { result += (daysRented - 2) * 1.5; } return result; } }
ChildrensPrice 少儿读物类价格
public class ChildrensPrice extends Price { @Override int getPriceCode() { return Book.CHILDRENS; } @Override public double getCharge(int daysRented) { double result = 1.5; if (daysRented >3) { result += (daysRented - 3) * 1.5; } return result; } }
NewReleasePrice 新书型类价格
public class NewReleasePrice extends Price { @Override int getPriceCode() { return Book.NEW_RELEASE; } @Override public double getCharge(int daysRented) { return daysRented * 3; } @Override public int getFrequentRenterPoints(int daysRented) { return (daysRented > 1)?2:1; } }
Book类
将priceCode换成Price。
public class Book { 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 Book() { } public Book(String title, int priceCode) { this.title = title; setPriceCode(priceCode); } //1.提取统计数量的方法 public double getCharge(int daysRented) { return _price.getCharge(daysRented); } //1.提取计算会员积分的方法 public int getFrequentRenterPoints(int daysRented) { if ((getPriceCode() == Book.NEW_RELEASE) && daysRented > 1) { return 2; } return 1; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } 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"); } } }
最终类图以下:
大体的工做以下:
最后想说:
若是你发现本身须要为程序添加一个特性,而代码结构使你没法很方便地达成目的,那么就先重构那个程序,使特性的添加比较容易进行,而后再添加特性。
写代码就应该像写诗同样,而不是没BUG,我就不动它。
源码:
https://github.com/xbmchina/r...
【重构】做者: Martin Fowler
若是对 Java、大数据感兴趣请长按二维码关注一波,我会努力带给大家价值。以为对你哪怕有一丁点帮助的请帮忙点个赞或者转发哦。