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的时候也有看到.设计模式的概念最早源于建筑设计大师<建筑的永恒算法>一书,它表 ...
随机推荐
- iOS开发的另类神器:libimobiledevice开源包【类似android adb 方便获取iOS设备信息】
简介 libimobiledevice又称libiphone,是一个开源包,可以让Linux支持连接iPhone/iPod Touch等iOS设备.由于苹果官方并不支持Linux系统,但是Linux上 ...
- PyQt4 开发入门
参考资料:PyQt4教程
- Jerry的通过CDS view + Smart Template 开发Fiori应用的blog合集
S4/HANA里有一个新的UI框架叫做Smart template, 配合ABAP后台的CDS view技术,能够让developer以Metadata driven的方式来开发Fiori应用, 这种 ...
- python将数据写入excel代码,python与office交互
# -*- coding: utf-8 -*- from smartframe.header import * import pymysql import json import importlib, ...
- 【转载】从头编写 asp.net core 2.0 web api 基础框架 (4) EF配置
Github源码地址:https://github.com/solenovex/Building-asp.net-core-2-web-api-starter-template-from-scratc ...
- vue路由对象($route)参数简介
路由对象在使用了 vue-router 的应用中,路由对象会被注入每个组件中,赋值为 this.$route ,并且当路由切换时,路由对象会被更新. so , 路由对象暴露了以下属性: 1.$rout ...
- angular2+ 组件中用@import进来的css不起作用
一般来说是作用域的问题,首先你应该先看标签内是否有angular2内置生成的自定义属性比如: 在我们的@Component中,这三个是基本的设置. 页面上的标签会生成带有 _nghost-c1 和 ...
- dotnet core cli 命令
1 dotnet new 2 创建code 程序 dotnet new console using System; namespace cli { class Program { static voi ...
- Centos7安装ES 和 Docker搭建ES
本文版权归博客园和作者吴双本人共同所有 转载和爬虫请注明原文地址 www.cnblogs.com/tdws 一.linux centos7.x安装ES 1.下载java sudo yum instal ...
- 【JavaScript函数】
函数的定义 : [完成某一个功能的代码段] 1.方便维护 2.重复利用 3.执行代码段 函数的一些要求: function 定义某一个函数 命名最好要有语义化, 函数名称最好是驼峰, 严格区分大小写, ...