zoukankan      html  css  js  c++  java
  • 设计模式(模板方法)

    模板方法是通过抽象类定义各一个基础模板,这个模板中有已经实现的,也有要求强制子类实现的。

    通过这个模板的定义来实现有约束的业务继承。代码如下:

    • Template
    public abstract class Template {
        public void templateMethod(){
            baseMethod();
            subMehtod();
        }
        
        public void baseMethod(){
            System.out.println("baseMethod");
        }
        
        public abstract void subMehtod();
    }
    • ConcreteOne&ConcreteTwo
    public class ConcreteOne extends Template {
    
        @Override
        public void subMehtod() {
            System.out.println("ConcreteOne");
        }
    }
    
    public class ConcreteTwo extends Template {
    
        @Override
        public void subMehtod() {
            System.out.println("ConcreteTwo");
        }
    }
    • App 测试类
    public class App {
    
        public static void main(String[] args) {
            Template temp = new ConcreteOne();
            temp.templateMethod();
            temp = new ConcreteTwo();
            temp.templateMethod();
        }
    }
  • 相关阅读:
    codeforce1028A Find Square
    2018ccpc网络赛 Buy and Resell
    差分约束
    Lost Cows
    动态查询区间第k大
    疫情控制
    天天爱跑步
    次小生成树
    树上差分闇の連锁
    Stars in Your Window
  • 原文地址:https://www.cnblogs.com/Fredric-2013/p/4576576.html
Copyright © 2011-2022 走看看