zoukankan      html  css  js  c++  java
  • 设计模式2,模板方法

    适用场景

      适用于固定逻辑的算法,但是里面的某些步骤是变化的。

    举例

      刷脸发送短信:学生->家长,访客->邀请人,家长->班主任)(注:学生->家长 ,表示学生刷脸发送给家长)

      逻辑:查询刷脸的人员 -> 查询短信要发送给谁 -> 短信模板  ->  发送短信 

    模板

    /**
     * @Auther: ZhangSuchao
     * @Date: 2020/4/27 10:03
     */
    public abstract class SendMsgTemplate {
        protected Integer userId; //刷卡用户
        protected Date inoutDate; //刷卡时间
    
        private SendMsgTemplate() {
        }
    
        public SendMsgTemplate(Integer userId, Date inoutDate) {
            this.userId = userId;
            this.inoutDate = inoutDate;
        }
    
        /**
         * 发送短信
         *
         * @return
         */
        public boolean sendMsg() {
            boolean needSendMsg = needSendMsg();
            if (!needSendMsg) return true;
            List<User> userList = select2User();
            String msg = msg();
            return trueSendMsg(userList, msg);
        }
    
        // 是否需要发送消息
        abstract boolean needSendMsg();
    
        // 查询要要送的用户
        abstract List<User> select2User();
    
        // 查询要发送的消息
        abstract String msg();
    
        abstract boolean trueSendMsg(List<User> userList, String msg);
    }

    子类继承该抽象类,实现其中的方法即可

    好处

      避免过多的判断,逻辑清晰

      

  • 相关阅读:
    python之map,filter
    python函数的闭包
    Hibernate查询对象的方法浅析
    底部浮动
    DataGrid-自定义排序
    DataGrid-1
    Alert
    2014-01-04 SQL练习
    proguard-gui 混淆代码简要笔记
    vim利用coc补全的配置过程
  • 原文地址:https://www.cnblogs.com/draymond/p/13074117.html
Copyright © 2011-2022 走看看