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 door
measure the elephant
slice up the elephant
put in the elephant
close the fridge door
====== begin to deal with the fox=========
open the fridge door
clean up the fox
dry the fox
put in the fox
close the fridge door

,that,s all.

java模式:模板模式的简单理解的更多相关文章

  1. 【设计模式】Java设计模式 - 模板模式

    Java设计模式 - 模板模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 原创作品,更多关注我CSDN: 一个有梦有戏的人 准备将博客园.CSDN一起记录分享自己 ...

  2. Java设计模式-模板模式

    介绍:模板模式定义了一个模板抽象类,这个抽象类中定义了方法调用的形式,顺序.子类通过重写对方法进行实现,但是调用方式不能改变. 模板模式中的模板中定义了核心的代码骨架,一些有着不同方式实现的代码放在子 ...

  3. 扯淡设计模式2:java,模板模式,

    模板模式: package com.dayuanit.service; public abstract class UserService { public void login(String use ...

  4. spring常用模式--模板模式

    引入:这几天在看一本讲spring源码的书<SPRING技术内幕>里面在讲加载配置文件的时候,可以有不同的加载方式,如根据文件系统目录加载配置文件(FileSystemXmlApplica ...

  5. Java设计模式(七)策略模式 模板模式

    (十三)策略模式 策略图案限定了多个封装算法,该算法可以相互替换包.法的客户.借用还有一位大神的样例. interface ICalculator{ public int calculate(Stri ...

  6. 个人对Java中多态的一些简单理解

    什么是多态 面向对象的三大特性:封装.继承.多态.从一定角度来看,封装和继承几乎都是为多态而准备的.这是我们最后一个概念,也是最重要的知识点. 多态的定义:指允许不同类的对象对同一消息做出响应.即同一 ...

  7. Java中多态的一些简单理解

    什么是多态 .面向对象的三大特性:封装.继承.多态.从一定角度来看,封装和继承几乎都是为多态而准备的.这是我们最后一个概念,也是最重要的知识点. .多态的定义:指允许不同类的对象对同一消息做出响应.即 ...

  8. java并发:AQS的简单理解

    简介: AQS全称 AbstractQueuedSynchronizer,提供了一个基于FIFO(先进先出)队列,可以用于构建锁或者其他相关同步装置的基础框架. ReentrantLock.Semap ...

  9. Java设计模式之模板模式(Template )

    前言: 最近学习了Glide开源图片缓存框架,在学习到通过使用ModelLoader自定义数据源的时候,Glide巧妙的使用了Java的模板模式来对外暴露处理不同的Url数据源,今天来学习总结一下模板 ...

随机推荐

  1. POJ 2305 Basic remains(进制转换)

    题目链接:http://poj.org/problem?id=2305 ime Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5326 ...

  2. JavaScript忍者秘籍——函数(下)

    概要:本篇博客主要介绍函数的一些类型以及常见示例 1.匿名函数 使用匿名函数的常见示例: window.onload = function(){ assert(true,'power!'); }; / ...

  3. aps.net 页面事件执行顺序

  4. MVC5 + EF6酒店项目笔记

    最近项目组准备用MVC5开发酒店模板包括后台.在此第一次学MVC5一个礼拜,看着组长给我的MVC5模板从一脸懵逼到懵懂. 慢慢学习,成长. 未完待续........

  5. ggplot2 geom相关设置—点重合处理(jitter)

    在R中散点图的时候会经常出现,点重合比较严重的现象,这对我们寻找数据规律或者观察数据有很大的干扰. 所幸的是R中,可以用geom_jitter()函数来调整,消除点的重合. geom_jitter(m ...

  6. tar的打包-压缩与解压缩,并解压到指定的目录

    tar在linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数:-x : 解压缩压缩档案的参数:-z : 是否需 ...

  7. 一步一步学EF系列2【最简单的一个实例】

    整个文章我都会用最简单,最容易让人理解的方式给大家分享和共同学习.(由于live Writer不靠谱 又得补发一篇) 一.安装 Install-Package EntityFramework 二.简单 ...

  8. matlab显示原图和灰度直方图

    **只会显示灰度直方图I = imread('*.bmp') %图必须是灰度图,或者转换为灰度图I=rgb2gray(I);imshow(I);imhist(I); **同时出现在一个平面上I = i ...

  9. The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class files

    JDK版本的问题. 解决方法: 原来jdk1.8不向下兼容,用回1.6的就可以了. 下图有三个jdk,前两个自己装的,第三个MyEclipse自带的.

  10. 移动前端不得不了解的HTML5 head 头标签(中下篇)

    SEO 优化部分 页面标题<title>标签(head 头部必须)        <title>your title</title>    页面关键词 keywor ...