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. PAT 团体程序设计天梯赛-练习集 L1-020. 帅到没朋友

    原题  https://www.patest.cn/contests/gplt/L1-020 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友.本题就要求你找出那些帅到没有朋友的人. ...

  2. Java 集合 散列表hash table

    Java 集合 散列表hash table @author ixenos 摘要:hash table用链表数组实现.解决散列表的冲突:开放地址法 和 链地址法(冲突链表方式) hash table 是 ...

  3. F#中的自定义隐式转换

    我们知道隐式变换在可控情况下会使代码变得简洁.熟悉C#的都知道C#中可以自定义隐式变换,例如 public class A { private int data; public static impl ...

  4. 浙大pat 1011题解

    With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excite ...

  5. 6、Web应用程序中的安全向量 -- customErrors(适当的错误报告和堆栈跟踪)

    几乎所有的网站在开发过程中都在web.config文件中设置了特性<customErrors mode="off">. customErrors模式有3个可选的设置项: ...

  6. OMCS开发手册(02) -- 多媒体连接器

    OMCS开发手册(01) -- 多媒体设备管理器 一文,我们从Owner的角度详细描述了多媒体设备管理器的使用,本文我们将站在Guest的角度,描述OMCS中另一类组件/控件:多媒体连接器.多媒体连接 ...

  7. [转]详细的mysql时间和日期函数

    这里是一个使用日期函数的例子.下面的查询选择了所有记录,其date_col的值是在最后30天以内: mysql> SELECT something FROM table WHERE TO_DAY ...

  8. 使用freerdp远程连接Windows桌面

    之前使用的是rdesktop,但是由于其不支持NLA认证,便不能登录公司的电脑.为此,现在使用freerdp——这是package的名字,实际的可执行程序是xfreerdp.使用如下的命令行即可实现远 ...

  9. LeetCode OJ 42. Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  10. 抛弃阿里云,中国用户购买海外VPS的五个理由

    王掌柜在过去的五年多时间里,折腾过不少vps品牌,最开始玩的是一年一百多块钱的香港虚拟主机,后来业务量大了,开始折腾国内的小鸟云.阿里云.腾讯云.电信云.百度云主机,国外的linode\interse ...