zoukankan      html  css  js  c++  java
  • 模板方法模式【行为模式】

    模板方法模式

    Define the skeleton of an algorithm in an operation,deferring some steps to subclasses.
    Template method lets subclass redefine certain steps of an algorithm without changing
    the algorithm structure.
    在操作中定义算法的骨架,将一些步骤推迟到子类实现。
    模板方法允许子类在不改变算法结构的情况下重新定义算法的某些步骤。
    
    @Slf4j
    public class Template {
        /**
         * 模板方法模式:
         * Define the skeleton of an algorithm in an operation,deferring some steps to subclasses.
         * Template method lets subclass redefine certain steps of an algorithm without changing
         * the algorithm structure.
         * 在操作中定义算法的骨架,将一些步骤推迟到子类实现。
         * 模板方法允许子类在不改变算法结构的情况下重新定义算法的某些步骤。
         */
        @Test
        public void all() {
            final Message message = Message.builder().title("template pattern").content("hello world").copyRight("版权所有 ZXD 15505883728").needTitle(true).build();
            final TextBuilder textBuilder = TextBuilder.builder().message(message).build();
            String msg = textBuilder.build();
            log.info(msg);
    
            message.setNeedTitle(false);
            final HtmlBuilder htmlBuilder = HtmlBuilder.builder().message(message).build();
            msg = htmlBuilder.build();
            log.info(msg);
        }
    }
    
    /**
     * 1)抽象模板类,定义算法的基本步骤和算法的骨架
     */
    abstract class ABuilder {
        /**
         * 以下方法为算法的基本步骤
         */
        abstract String title();
    
        abstract String content();
    
        abstract String copyRight();
        // 基于 title 的钩子函数,允许修改算法的逻辑
        abstract boolean needTitle();
    
        /**
         * 算法骨架需要定义为 final 类型,不允许子类修改。
         */
        public final String build() {
            final StringJoiner joiner = new StringJoiner("
    ");
            if (needTitle()) {
                joiner.add(title());
            }
            return joiner.add(content()).add(copyRight()).toString();
        }
    }
    
    @Data
    @Builder
    class Message {
        private String title;
        private String content;
        private String copyRight;
        private boolean needTitle;
    }
    
    /**
     * 2)实现算法的基本步骤以构建特定的实例。
     */
    @Builder
    class TextBuilder extends ABuilder {
        private final Message message;
    
        @Override
        String title() {
            return message.getTitle();
        }
    
        @Override
        String content() {
            return message.getContent();
        }
    
        @Override
        String copyRight() {
            return message.getCopyRight();
        }
    
        @Override
        boolean needTitle() {
            return message.isNeedTitle();
        }
    }
    /**
     * 3)实现算法的基本步骤以构建特定的实例。
     */
    @Builder
    class HtmlBuilder extends ABuilder {
        private final Message message;
    
        @Override
        String title() {
            return new StringJoiner("").add("<html><head><title>Empty</title><body><h3>").add(message.getTitle())
                    .add("</h3>").toString();
        }
    
        @Override
        String content() {
            return new StringJoiner("").add("<h5>").add(message.getContent()).add("</h5>").toString();
        }
    
        @Override
        String copyRight() {
            return new StringJoiner("").add("<h6>").add(message.getCopyRight()).add("</h6>").toString();
        }
    
        @Override
        boolean needTitle() {
            return message.isNeedTitle();
        }
    }
    
  • 相关阅读:
    #import &lt;/usr/include/objc/objc-class.h&gt; not such file or directory问题的解决方法
    关于二进制补码
    DirectSound的应用
    Qt on Android: http下载与Json解析
    双绞线的制作,T568A线序,T568B线序
    Java设计模式之从[暗黑破坏神存档点]分析备忘录(Memento)模式
    linux 系统升级中的一些配置
    malloc函数具体解释
    老鸟的Python新手教程
    ubuntu12.04 安装配置jdk1.7
  • 原文地址:https://www.cnblogs.com/zhuxudong/p/10164706.html
Copyright © 2011-2022 走看看