java模式:模板模式的简单理解
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模式:模板模式的简单理解的更多相关文章
- 【设计模式】Java设计模式 - 模板模式
Java设计模式 - 模板模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 原创作品,更多关注我CSDN: 一个有梦有戏的人 准备将博客园.CSDN一起记录分享自己 ...
- Java设计模式-模板模式
介绍:模板模式定义了一个模板抽象类,这个抽象类中定义了方法调用的形式,顺序.子类通过重写对方法进行实现,但是调用方式不能改变. 模板模式中的模板中定义了核心的代码骨架,一些有着不同方式实现的代码放在子 ...
- 扯淡设计模式2:java,模板模式,
模板模式: package com.dayuanit.service; public abstract class UserService { public void login(String use ...
- spring常用模式--模板模式
引入:这几天在看一本讲spring源码的书<SPRING技术内幕>里面在讲加载配置文件的时候,可以有不同的加载方式,如根据文件系统目录加载配置文件(FileSystemXmlApplica ...
- Java设计模式(七)策略模式 模板模式
(十三)策略模式 策略图案限定了多个封装算法,该算法可以相互替换包.法的客户.借用还有一位大神的样例. interface ICalculator{ public int calculate(Stri ...
- 个人对Java中多态的一些简单理解
什么是多态 面向对象的三大特性:封装.继承.多态.从一定角度来看,封装和继承几乎都是为多态而准备的.这是我们最后一个概念,也是最重要的知识点. 多态的定义:指允许不同类的对象对同一消息做出响应.即同一 ...
- Java中多态的一些简单理解
什么是多态 .面向对象的三大特性:封装.继承.多态.从一定角度来看,封装和继承几乎都是为多态而准备的.这是我们最后一个概念,也是最重要的知识点. .多态的定义:指允许不同类的对象对同一消息做出响应.即 ...
- java并发:AQS的简单理解
简介: AQS全称 AbstractQueuedSynchronizer,提供了一个基于FIFO(先进先出)队列,可以用于构建锁或者其他相关同步装置的基础框架. ReentrantLock.Semap ...
- Java设计模式之模板模式(Template )
前言: 最近学习了Glide开源图片缓存框架,在学习到通过使用ModelLoader自定义数据源的时候,Glide巧妙的使用了Java的模板模式来对外暴露处理不同的Url数据源,今天来学习总结一下模板 ...
随机推荐
- poj1741_Tree(树的点分治入门题)
题目链接:poj1741_Tree 题意: 给你一颗n个节点的树,每条边有一个值,问有多少点对(u,v),满足u->v的最短路径小于k. 题解: 典型的树的分治,板子题. #include< ...
- openwrt的uboot环境变量分析
目前烧写完CC(chaos calmer 15.05)版本,查看其uboot变量如下: ath> printenvbootargs=console=ttyS0,115200 root=31:02 ...
- OpenGL 茶壶
void MyRenderer::Init_Teapot_VBO() { m_fun->glGenBuffers(, &m_teapot_vbo); m_fun->glBindBu ...
- HDU 2955 Robberies(01背包)
Robberies Problem Description The aspiring Roy the Robber has seen a lot of American movies, and kno ...
- NTFS 读写高手进阶 - Windows 格式硬盘 Mac存文件 开启 ...(转载)
常识: 硬盘格式: FAT32 - WIndows 硬盘分区格式, 有点通用性高, 缺点不支持单个大于 4G 的文件. exFAT - Windows 硬盘分区格式, 兼容性低. 稳定性不如 FAT3 ...
- python3 随机数
random库 random.random()返回n,则 0 <= n < 1的小数. random.uniform(a,b) 返回n ,则 a <= n <= b的浮点 ...
- 《LYFvs2013转vs2010》
<LYFvs2013转vs2010>1.修改解决方案文件(**.sln) 将-------------------------------------------------------- ...
- 用Visual Studio 2015 编写第一个UMDF驱动遇到的问题!!
前提:Visual Studio 2015已经成功安装了驱动环境,WDK都已经完全正常安装了,在Visual Studio 2015的菜单可以看到"Driver"菜单项了.这说明已 ...
- c语言_头文件
传统 C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <assert.h> //设定插入点 #include <ctyp ...
- DLL调试方法
1.已经做好的dll不能设置:你可以用AfxMessageBox把信息打印出来.2.哪个地方调用的函数 把DLL重新编译一次 在把DLL放到工程里 从新添加一下 然后在你工程调用DLL内容的地方设置断 ...