zoukankan      html  css  js  c++  java
  • 【设计模式】—— 模板方法Template

      前言:【模式总览】——————————by xingoo

      模式意图

      定义一个类的框架,当它有不同的类时,再具体实现。

      比如,我们设计一个跨系统的客户端软件,Windows需要一套展现类,Linux需要一套,mac还需要一套。这样,只需要抽取他们的共同操作编程一个框架类,具体使用到哪个系统时,再使用对应的类,有点像C++里面的模板。

      应用场景

      1 一次性实现一个类的不变部分,其他的部分留到子类实现。

      2 各个子类提取公共部分成为超类

      3 控制子类的扩展。

      模式结构

      AbstractClass 抽象类框架

    abstract class AbstractClass{
        public void action(){
            step1();
            step2();
            newMethod();
        }
        abstract protected void step1();
        abstract protected void step2();
        abstract protected void newMethod();
    }

      Class 具体的子类,进行扩展

    class Class1 extends AbstractClass{
        protected void newMethod() {
            System.out.println("class1 newMethod");
        }
        protected void step1() {
            System.out.println("class1 step1");
        }
        protected void step2() {
            System.out.println("class1 step2");
        }
    }
    class Class2 extends AbstractClass{
        protected void newMethod() {
            System.out.println("class2 newMethod");
        }
        protected void step1() {
            System.out.println("class2 step1");
        }
        protected void step2() {
            System.out.println("class2 step2");
        }
    }

      全部代码

     1 package com.xingoo.test.design.template;
     2 abstract class AbstractClass{
     3     public void action(){
     4         step1();
     5         step2();
     6         newMethod();
     7     }
     8     abstract protected void step1();
     9     abstract protected void step2();
    10     abstract protected void newMethod();
    11 }
    12 class Class1 extends AbstractClass{
    13     protected void newMethod() {
    14         System.out.println("class1 newMethod");
    15     }
    16     protected void step1() {
    17         System.out.println("class1 step1");
    18     }
    19     protected void step2() {
    20         System.out.println("class1 step2");
    21     }
    22 }
    23 class Class2 extends AbstractClass{
    24     protected void newMethod() {
    25         System.out.println("class2 newMethod");
    26     }
    27     protected void step1() {
    28         System.out.println("class2 step1");
    29     }
    30     protected void step2() {
    31         System.out.println("class2 step2");
    32     }
    33 }
    34 public class Client {
    35     private static AbstractClass class1 = new Class1();
    36     private static AbstractClass class2 = new Class2();
    37     public static void main(String[] args) {
    38         class1.action();
    39         class2.action();
    40     }
    41 }
    View Code

      运行结果

    class1 step1
    class1 step2
    class1 newMethod
    class2 step1
    class2 step2
    class2 newMethod
  • 相关阅读:
    while 循环 。。
    数据运算,运算符
    字符串常用操作
    列表常用操作
    三级菜单
    杂七杂八
    简单的登陆程序001
    猜年龄游戏
    实现密文输入密码
    使用urllib2打开网页的三种方法
  • 原文地址:https://www.cnblogs.com/xing901022/p/4084944.html
Copyright © 2011-2022 走看看