linkin大话设计模式--模板方法模式
linkin大话设计模式--模板方法模式
//模板抽象类
abstract class Template {
protected String pcType; public Template(String pcType){
this.pcType=pcType;
} //定义3个抽象方法,推迟到子类实现
abstract protected void makeCPU(String pcType);
abstract protected void makeMainBorad(String pcType);
abstract protected void makeHD(String pcType); //定义一个基本方法
public final void makeOver(String pcType){
System.out.println(pcType+"造好了...");
} //模板方法,造电脑的方法
public final void makePC(){
makeCPU(pcType);
makeMainBorad(pcType);
makeHD(pcType);
makeOver(pcType);
}
} //2个不同的子类 实现了上面的模板中定义的抽象方法
class NotePC extends Template{ public NotePC(String pcType) {
super(pcType);
} @Override
protected void makeCPU(String pcType) {
System.out.println(pcType+"的CPU造好了...");
} @Override
protected void makeMainBorad(String pcType) {
System.out.println(pcType+"的主板造好了...");
} @Override
protected void makeHD(String pcType) {
System.out.println(pcType+"的硬盘造好了...");
} } class PC extends Template{ public PC(String pcType) {
super(pcType);
} @Override
protected void makeCPU(String pcType) {
System.out.println(pcType+"的CPU造好了呢...");
} @Override
protected void makeMainBorad(String pcType) {
System.out.println(pcType+"的主板造好了呢...");
} @Override
protected void makeHD(String pcType) {
System.out.println(pcType+"的硬盘造好了呢...");
} } public class TemplateTest { public static void main(String[] args) {
Template template = new NotePC("笔记本");
template.makePC();
Template template1 = new PC("台式机");
template1.makePC();
} }
1,接口类似于整个系统的总纲,它制定了系统各模块应该遵守的标准,因此一个系统的接口不应该经常改变。接口一旦变化,系统中的大部分类都需要重写
2,抽象类不一样,抽象类作为系统中多个子类的共同父类,它所体现的是一种模板式设计。它可以当做系统实现过程中的中间产品,这个中间产品已经实现了系统的部分功能,但是这个产品任然不是最终产品,必须要有更进一步的完善,具体的推迟到子类实现。
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod(); if (method.equals("GET")) {
long lastModified = getLastModified(req);
if (lastModified == -1L)
{
doGet(req, resp); } else {
long ifModifiedSince;
try {
ifModifiedSince = req.getDateHeader("If-Modified-Since");
}
catch (IllegalArgumentException iae) {
ifModifiedSince = -1L;
}
if (ifModifiedSince < lastModified / 1000L * 1000L)
{
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(304);
}
}
}
else if (method.equals("HEAD")) {
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
}
else if (method.equals("POST")) {
doPost(req, resp);
}
else if (method.equals("PUT")) {
doPut(req, resp);
}
else if (method.equals("DELETE")) {
doDelete(req, resp);
}
else if (method.equals("OPTIONS")) {
doOptions(req, resp);
}
else if (method.equals("TRACE")) {
doTrace(req, resp);
}
else
{
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs); resp.sendError(501, errMsg);
}
}
linkin大话设计模式--模板方法模式的更多相关文章
- linkin大话设计模式--常用模式总结
linkin大话设计模式--常用模式总结 一,常用设计模式定义 Abstract Factory(抽象工厂模式):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. Adapter( ...
- linkin大话设计模式--建造模式
linkin大话设计模式--建造模式 建造模式是对象的创建模式,可以讲一个产品的内部表象与产品的生成过程分割开来,从而可以使一个建造过程生成具有不同的内部表象的产品对象. 建造模式的结构: 抽象建造者 ...
- linkin大话设计模式--桥接模式
linkin大话设计模式--桥接模式 桥接模式是一种结构化模式,他主要应对的是:由于实际的需要,某个类具有2个或者2个以上维度的变化,如果只是使用继承将无法实现功能,或者会使得设计变得相当的臃肿.我们 ...
- linkin大话设计模式--门面模式
linkin大话设计模式--门面模式 随着系统的不断改进和开发,他们会变得越来越复杂,系统会生成大量的类,这使得程序的流程更加难以理解.门面模式可以为这些类提供一个简易的接口,从而简化访问这些类的复杂 ...
- linkin大话设计模式--策略模式
linkin大话设计模式--策略模式 Strategy [ˈstrætədʒi] 策略 策略模式用于封装系列的算法,这些算法通常被封装在一个称为Context的类中,客户端程序可以自由的选择任何一种 ...
- linkin大话设计模式--命令模式
linkin大话设计模式--命令模式 首先考虑一种应用情况,某个方法需要完成某一个功能,这个功能的大部分功能已经确定了,但是有可能少量的步骤没法确定,必须等到执行这个方法才可以确定. 也就是说,我们写 ...
- 大话设计模式--模板方法模式 TemplateMethod -- C++ 实现
1. 模板方法模式: 定义一个操作中的算法骨架,而将一些操作延迟到子类, 模板方法模式使得子类可以不改变一个算法的结构既可以重定义该算法的某些特定步骤. 当不变和可变的行为在方法的子类实现中混在一起的 ...
- linkin大话设计模式--代理模式
代理模式是一种应用非常广泛的设计模式,当客户端代码需要调用某个对象的时候,客户端并不关心是否可以准确的得到这个对象,他只要一个能够提供该功能的对象而已,此时我们就可以返回该对象的代理.总而言之,客户端 ...
- linkin大话设计模式--单例模式
linkin大话设计模式 开文前先弱弱的问一句:什么是设计模式?我在研究java2ee的时候有研究过,在学js的时候也有看到.设计模式的概念最早源于建筑设计大师<建筑的永恒算法>一书,它表 ...
随机推荐
- adb 获取Android手机信息命令(2)
#Android命令 #获取手机名称 GET_PHONE_NAME = 'adb shell getprop ro.product.model' #获取手机版本 GET_PHONE_VERSION = ...
- 文件及Linux目录结构
什么是文件 在linux系统上,所有的资源都是文件,Linux系统下的文件类型包括 普通文件(-) 目录(d) 符号链接(l) 字符设备文件(c) 块设备文件(b) 套接字(s) 命令管道(p) 普通 ...
- IO多路复用
1.事件驱动模型 上一篇写的协程仅仅是切换,本身不能实现并发,什么时候切换也不知道 那么什么时候切回去呢?怎么确定IO操作完了?通过回调函数 对于事件驱动型程序模型,它的流程大致如下: 开始---& ...
- FreeMarker处理json
在后台返回一个json结构时,在ftl处理方式如下 <#assign json="${text}"?eval /> ${json.test} 说明:json为接收的值, ...
- App开发 对生命周期的处理
//获取到当前所在的视图 - (UIViewController *)presentingVC:(UIApplication *)application{ UIWindow * window = ap ...
- python 单下划线/双下划线使用总结
文章转自:http://blog.csdn.net/pfm685757/article/details/45918575
- random seed()函数
用seed()生成随机数字,生成的法则与seed内部的数字相关,如果数字相同,则生成的随机数是相同的. 刷题宝上面的题目: >>> import random >>> ...
- Uva 10339 - Watching Watches【数论,暴力】
题目链接:10339 - Watching Watches 题意:两个时钟,一个每天慢a秒,一个每天慢b秒,问两钟重新相遇的时刻 1圈有12 * 60 * 60秒,然后1圈 / abs(a - b), ...
- 北京师范大学校赛C
https://www.nowcoder.com/acm/contest/submit/0dff89ad7b8444719df155d507f3e1dd?ACMContestId=3&tagI ...
- hdu_1698Just a Hook(线段树)
hdu_1698Just a Hook(线段树) 标签: 线段树 题目链接 题意: 一个英雄的技能是发射一个长度为n的金属链,初始的金属链都是铁做的,标记为1,我们可以对于某个区间修改它的金属材质,如 ...