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. WEB前端开发中的图片压缩

    web前端开发中,图片的重要性不言而喻,而由于一些图片的大小加上现在国内的网速不给力等种种原因,我们非常有必要对网站使用的图片进行压缩,压缩图片必然会带来图片质量的损失,我们要尽可能的在质量降低很小的 ...

  2. 【LeetCode】423. Reconstruct Original Digits from English

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  3. 一步一步学EF系列【4、升级篇 实体与数据库的映射】live writer真坑,第4次补发

    前言 之前的几篇文章,被推荐到首页后,又被博客园下了,原因内容太少,那我要写多点呢,还是就按照这种频率进行写呢?本身我的意图这个系列就是想已最简单最容易理解的方式进行,每篇内容也不要太多,这样初学者容 ...

  4. 利用scrollTop 制作图片无缝滚动

    <!doctype html><title>javascript无缝滚动 by 司徒正美</title><meta charset="utf-8&q ...

  5. table边框1px

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. Arch最小化安装X

    Xorg xorg-server 安装xorg-server pacman -S xorg-server 可选:xorg-server-utils pacman -S xorg-server-util ...

  7. dell 去鼠标版功能widnows

    桌面计算机(点击右键)----管理----设备管理器-----鼠标------选择触摸板(ps/2 兼容鼠标)---右击------跟新驱动-------浏览计算机查找------从计算机列表中选择- ...

  8. Keyboard Test Utility v1.0.1.0 电脑键盘测试软件绿色版

    软件名称: 电脑键盘测试软件绿色版软件语言: 简体中文授权方式: 免费软件运行环境: Win8 / Win7 / Vista / WinXP软件大小: 917KB图片预览: 软件简介:Keyboard ...

  9. 使用gulp构建nodejs,你只需要记住5个函数

    gulp gulp是一个nodejs的streaming构建工具,所谓的streaming大致意思就是把构建流程想成一个个链接的管道(pipe). 为什么要这样做呢? 要解释原因,就不得不提到unix ...

  10. C++设计模式-Iterator迭代器模式

    ref: http://www.cnblogs.com/onlycxue/archive/2013/12/25/3490738.html