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数据源,今天来学习总结一下模板 ...
随机推荐
- CSS3的基础知识点
面临找工作之际,又将CSS3的基础知识撸了一把,做了相应的笔记,主要是方便自己查阅,参考的是W3C的知识. 1.CSS背景 (1).background-size 属性 background-s ...
- 苹果dock效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 浙大pat 1037
1037. Magic Coupon (25) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The magi ...
- putty 中使用git
提交git pwd 查看当前目录 进入目录cd /home/gongfu/tripb/master 修改代码目录 git status 查看 修改的文件(新增的,删除的) git commit -m ...
- Java 4
1.继承的问题 子类是父类的一个扩展,子类可以利用父类的属性与行为,这种情况子类会破坏父类的封装 为了保持父类良好的封装性,设计父类有以下规则: 如果要把某类设计为最终类则需要添加final修饰符,或 ...
- VBO
#include <GL/glew.h> #include <GL/glut.h> #include <iostream> #pragma comment(lib, ...
- 递归删除本地目录和ftp目录
本地目录: void CAutoDelete::DoRecursionLocalDelete(CString& localDirectory) { CFileFind finder; CStr ...
- 极光推送 PHP sdk
<?php defined('IN_WZ') or exit('No direct script access allowed'); /** * Created by PhpStorm. * U ...
- mysql表备份及还原
备份 导出数据库所有表结构 ? 1 mysqldump -uroot -ppassword -d dbname > db.sql 导出数据库某个表结构 ? 1 mysqldump -uroot ...
- html_web存储
HTML5存储 HTML5 web存储,一个比cookie更好的本地存储方式. 什么是HTML5 Web存储? 使用HTML5可以在本地存储用户的浏览数据. 早些时候,本地存储使用的是cookie.但 ...