zoukankan      html  css  js  c++  java
  • 代码重构之以查询取代临时变量

    意图

    - 使得同一个类中的所有函数都可以获得这份信息,能够为这个类编写更清晰的代码

    示例

    /**
     * 以查询取代临时变量之前
     * Created by luo on 2017/4/19.
     */
    public class ReplaceTempWithQueryBefore {
        private double _quantity;
        private double _itemPrice;
    
        public double test() {
            double basePrice = _quantity * _itemPrice;
            if (basePrice > 1000) {
                return basePrice * 0.95;
            } else {
                return basePrice * 0.98;
            }
        }
    }
    /**
     * 以查询取代临时变量之后
     * Created by luo on 2017/4/19.
     */
    public class ReplaceTempWithQueryAfter {
        private double _quantity;
        private double _itemPrice;
    
        public double test() {
            if (basePrice() > 1000) {
                return basePrice() * 0.95;
            } else {
                return basePrice() * 0.98;
            }
        }
    
        private double basePrice() {
            return _quantity * _itemPrice;
        }
    
    }
  • 相关阅读:
    B . Block Adventure (贪心+模拟 )
    8适配器模式
    7命令模式
    5抽象工厂模式
    4工厂方法模式
    3装饰者模式
    2观察者模式
    1策略模式类图
    POJ3264(分桶法)
    POJ2104(分桶法)
  • 原文地址:https://www.cnblogs.com/luoxiaolei/p/6759114.html
Copyright © 2011-2022 走看看