zoukankan      html  css  js  c++  java
  • 设计模式-模板方法模式使用

    需求 根据不同商品 执行不同计价规则即不同折扣计费

    1.商品计算的功能接口

    2.模板类 实现功能接口

        公共计算方法

        特殊计算方法

        重写功能接口方法即模板方法  定义计算的规则

    3.水果类商品  家电类商品为 模板类子类 重写各自特殊计算方法

     结果如下

    代码如下

    public class TemplateMethodTest {
    
        public static void main(String[] args) {
            //计算水果类商品
            GoodsService fruitService = new FruitService();
            fruitService.calculate(10);
            System.out.println("======================");
            //计算家电类商品
            GoodsService homeAppliancesService = new HomeAppliancesService();
            homeAppliancesService.calculate(12);
    
        }
    
        /***
         *功能描述 商品服务接口
         */
        interface GoodsService {
    
            /**
             * 计算金额
             * @param amount 购买数量
             */
            void calculate(int amount);
        }
    
    
        /**
         * 模板类
         */
        static abstract class AbstractGoodsService implements GoodsService {
            /**
             * 公共费用计算逻辑
             * @paramt
             * @return 返回金额
             */
    
            private BigDecimal commonCalculate() {
                System.out.println("执行公共费用计算:...");
                return new BigDecimal("2.00");
            }
    
            /**
             * 特殊的计算逻辑
             * @param amount 购买数量
             * @return 返回金额
             */
            protected abstract BigDecimal specificCalculate(int amount);
    
            /**
             * 模板方法 定义计算流程规则
             */
            @Override
            public void calculate(int amount) {
                // 完成通用的逻辑
                BigDecimal money = commonCalculate();
                // 完成特殊的逻辑
                money = money.add(specificCalculate(amount));
                System.out.println("共计:"+money);
                // 其他操作....
            }
        }
    
    
        static class FruitService extends AbstractGoodsService {
            //九九折扣
            @Override
            public BigDecimal specificCalculate(int amount) {
                System.out.println("执行水果类商品的优惠政策");
                return new BigDecimal("0.99").multiply(new BigDecimal(String.valueOf(amount)));
            }
        }
    
    
    
    
        static class HomeAppliancesService extends AbstractGoodsService {
            //八折
            @Override
            public BigDecimal specificCalculate(int amount) {
                System.out.println("执行家电类商品的优惠政策");
                return new BigDecimal("0.8").multiply(new BigDecimal(String.valueOf(amount)));
            }
        }
    }
    古人学问无遗力,少壮工夫老始成。 纸上得来终觉浅,绝知此事要躬行。
  • 相关阅读:
    新闻发布项目——接口类(newsTbDao)
    Möbius strip
    The Apache Thrift API client/server architecture
    可以执行全文搜索的原因 Elasticsearch full-text search Kibana RESTful API with JSON over HTTP elasticsearch_action es 模糊查询
    SciDB
    build a real-time analytics dashboard to visualize the number of orders getting shipped every minute to improve the performance of their logistics for an e-commerce portal
    Kafka monitoring Kafka dashboard
    redundant array of independent disks
    the algebra of modulo-2 sums disk failure recovery
    define tensorflow and run it
  • 原文地址:https://www.cnblogs.com/wf-zhang/p/14884212.html
Copyright © 2011-2022 走看看