本文主要是介绍《重构:改善既有代码的设计》-学习笔记二(+实战解析),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
我不是个伟大的程序员;我只是个有着一些优秀习惯的好程序员而己
本人比较直接,不说虚的,直接上干货。
目录
Long Parameter List(过长参数列)
优化思路
public double getPrice() {int basePrice = _quantity * _itemPrice;int discountLevel;if (_quantity > 100) discountLevel = 2;else discountLevel = 1;double finalPrice = discountedPrice (basePrice, discountLevel);return finalPrice;}private double discountedPrice (int basePrice, int discountLevel) {if (discountLevel == 2) return basePrice * 0.1;else return basePrice * 0.05;}
优化 1,2,3步骤 优化参数basePrice
public double getPrice() {int basePrice = getBasePrice();int discountLevel;if (_quantity > 100) discountLevel = 2;else discountLevel = 1;double finalPrice = discountedPrice ( discountLevel);return finalPrice;}private double discountedPrice ( int discountLevel) {if (discountLevel == 2) return getBasePrice() * 0.1;else return getBasePrice() * 0.05;}private int getBasePrice(){return _quantity * _itemPrice;}
优化4步骤 去掉参数basePrice
public double getPrice() {int discountLevel;if (_quantity > 100) discountLevel = 2;else discountLevel = 1;double finalPrice = discountedPrice ( discountLevel);return finalPrice;}private double discountedPrice ( int discountLevel) {if (discountLevel == 2) return getBasePrice() * 0.1;else return getBasePrice() * 0.05;}private int getBasePrice(){return _quantity * _itemPrice;}
优化1,2,3,4步骤,去掉discountLevel参数,独立函数返回值要为discountLevel 最后赋值的值
public double getPrice() {double finalPrice = discountedPrice ();return finalPrice;}private double discountedPrice () {if (getDiscountLevel() == 2) return getBasePrice() * 0.1;else return getBasePrice() * 0.05;}private double getDiscountLevel(){if (_quantity > 100) return 2;else return 1;} private double getBasePrice(){return _quantity * _itemPrice;}
从上述代码可看出,getPrice主函数finalPrice参数已经可以直接优化了。
public double getPrice() {return discountedPrice ();}
public double getPrice() {if (getDiscountLevel() == 2) return getBasePrice() * 0.1;else return getBasePrice() * 0.05;}private double getDiscountLevel(){if (_quantity > 100) return 2;else return 1;} private double getBasePrice(){return _quantity * _itemPrice;}
Divergent Change(发散式变化)
优化思路
Shotgun Surgery(散弹式修改)
优化思路
Feature Envy(依恋情结)
优化思路
Data Clumps(数据泥团)
优化思路
public class Car{// 奔驰public void printBenz(String brand, String model, Integer price, double power) {printBasicInfo(brand, model, price, power);getTax(power, price);}// 宝马public void printBmw(String brand, String model, Integer price, double power) {printBasicInfo(brand, model, price, power);getTax(power, price);}// 提炼打印基本信息方法private void printBasicInfo(String brand, String model, Integer price, double power) {System.out.println("品牌" + brand);System.out.println("型号:" + model);System.out.println("动力:" + power);System.out.println("价格:" + price);}// 提炼计算税费的方法private double getTax(double power, Integer price){double salePrice = price;if (price > 200000) {salePrice = price * 0.98;}if (power <= 1.6) {return salePrice * 0.05;} else {return salePrice * 0.1;}}
}
public class CarEntity {private String brand;private String model;private Integer price;private Double power;public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public String getModel() {return model;}public void setModel(String model) {this.model = model;}public Integer getPrice() {return price;}public void setPrice(Integer price) {this.price = price;}public Double getPower() {return power;}public void setPower(Double power) {this.power = power;}}
优化3,4步骤
// 奔驰public void printBenz(CarEntity carEntity) {printBasicInfo(carEntity);// 计算税费getTax(carEntity);}// 宝马public void printBmw(CarEntity carEntity) {printBasicInfo(carEntity);getTax(carEntity);}private void printBasicInfo(CarEntity carEntity) {System.out.println("品牌" + carEntity.getBrand());System.out.println("型号:" + carEntity.getModel());System.out.println("动力:" + carEntity.getPower());System.out.println("价格:" + carEntity.getPrice());}// 计算税费private double getTax(CarEntity carEntity) {// 打折后价格double salePrice = carEntity.getPrice();if (carEntity.getPrice() > 200000) {salePrice = carEntity.getPrice() * 0.98;}if (carEntity.getPower() <= 1.6) {return salePrice * 0.05;} else {return salePrice * 0.1;}}
Primitive Obsession(基本型别偏执)
优化思路
String[] row = new String[3];row [0] = "Liverpool";row [1] = "15";//对象取代数组Performance row = new Performance();row.setName("Liverpool");row.setWins("15");
Switch Statements(switch惊悚现身)
优化思路
作者:小虚竹
欢迎任何形式的转载,但请务必注明出处。
限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。
这篇关于《重构:改善既有代码的设计》-学习笔记二(+实战解析)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!