博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java模式:模板模式的简单理解
阅读量:7191 次
发布时间:2019-06-29

本文共 2459 字,大约阅读时间需要 8 分钟。

1.模板模式就是用虚类作为基类将几个要执行差不多操作中相同的部分提取出来,不同的部分各自实现!

2.下面给出简单栗子:

  我要进行的操作是将大象和狐狸放入冰箱,放入大象和狐狸有相同的步骤:开冰箱和关冰箱,这个操作在基类中实现就好,而不同的在于具体操作部分:

    一,大象太胖了,要测量并切片才能放入冰箱

    二,狐狸太臭了,要洗干净并擦干

所以程序如下:

1.基类:BasicFridgeOperation.java

package com.learn.templateMode;/** * Created by garfield on 9/15/2016. */public abstract class BasicFridgeOperation {    private void openFridge(){        System.out.println("open the fridge door");    }    /**     * different parts about one of the whole steps     * the subclass will make different implements     */    protected abstract void operateFridge();    private void closeFridge(){        System.out.println("close the fridge door");    }    /**     * the same operation steps     */    public void operation(){        openFridge();        operateFridge();        closeFridge();    }}

2,放入大象类:OperateElephant.java

package com.learn.templateMode;/** * Created by garfield on 9/15/2016. */public class OperateElephant extends BasicFridgeOperation {    /**     * same function but different implement     */    protected void operateFridge() {        System.out.println("measure the elephant");        System.out.println("slice up the elephant");        System.out.println("put the elephant in");    }}

3,放入狐狸类:OperateFox.java

package com.learn.templateMode;/** * Created by garfield on 9/15/2016. */public class OperateFox extends BasicFridgeOperation {    /**     * same function but different implement     */    protected void operateFridge() {        System.out.println("clean up the fox");        System.out.println("dry the fox");        System.out.println("put the fox in");    }}

4,测试:OperationTest.java

package com.learn.templateMode;/** * Created by garfield on 9/15/2016. */public class OperationTest {    public static void main(String[] args) {        System.out.println("====== begin to deal with the elephant=========");        BasicFridgeOperation basicFridgeOperation = new OperateElephant();        basicFridgeOperation.operation();        System.out.println("====== begin to deal with the fox=========");        BasicFridgeOperation basicFridgeOperation2 = new OperateFox();        basicFridgeOperation2.operation();    }}

5,输出结果:

====== begin to deal with the elephant=========open the fridge doormeasure the elephantslice up the elephantput in the elephantclose the fridge door====== begin to deal with the fox=========open the fridge doorclean up the foxdry the foxput in the foxclose the fridge door

,that,s all.

转载地址:http://cftkm.baihongyu.com/

你可能感兴趣的文章
JBPM学习第3篇:10分钟熟悉JBPM工作台
查看>>
JS的数组,string类的定义及基本方法
查看>>
libevent使用<一> libevent导入项目
查看>>
n&(n-1)的妙用
查看>>
12月2日站立会议
查看>>
评论列表显示及排序,个人中心显示
查看>>
Least Common Ancestors
查看>>
Oracle数据库 之 使用DBLink访问时,提示ORA-01017
查看>>
「学习总结-Haskell-4」Haskell数据类型
查看>>
接口抽取及依赖版本统一介绍
查看>>
Andriod开发学习笔记
查看>>
phpcms_v9 多图字段 内容页,首页,分页自定义字段调用
查看>>
Linux下MySQL导入文件出错ERROR 1290 (HY000)
查看>>
POS开发问题 - 缓存问题 - 02
查看>>
JDBC编程,从入门到精通
查看>>
模板类中的友元函数
查看>>
Eclipse设置项目默认编码和换行符类型
查看>>
【实用性程序】弧微分计算圆周长
查看>>
算法模板——平衡树Treap
查看>>
1819: [JSOI]Word Query电子字典
查看>>