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);}@Overrideprotected void makeCPU(String pcType) {System.out.println(pcType+"的CPU造好了...");}@Overrideprotected void makeMainBorad(String pcType) {System.out.println(pcType+"的主板造好了...");}@Overrideprotected void makeHD(String pcType) {System.out.println(pcType+"的硬盘造好了...");}}class PC extends Template{public PC(String pcType) {super(pcType);}@Overrideprotected void makeCPU(String pcType) {System.out.println(pcType+"的CPU造好了呢...");}@Overrideprotected void makeMainBorad(String pcType) {System.out.println(pcType+"的主板造好了呢...");}@Overrideprotected 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大话设计模式--桥接模式 桥接模式是一种结构化模式,他主要应对的是:由于实际的需要,某个类具有2个或者2个以上维度的变化,如果只是使用继承将无法实现功能,或者会使得设计变得相当的臃肿.我们 ...
- 大话设计模式—模板方法模式
在模板方法模式(TemplateMethod Pattern)中,一个抽象类公开定义了执行它的方法的方式/模板.它的子类可以按需要重写方法实现,但调用将以抽象类中定义的方式进行.这种类型的设计模式属于 ...
- 大话设计模式:模板方法模式
一.什么是模板方法模式 算法骨架的封装 使用一个算法骨架,确定算法流程,具体步骤可变化的部分交给子类实现,这样子类就可以不改变算法骨架流程,重定义部分具体步骤的实现. UML图 AbstractTem ...
- linkin大话设计模式--代理模式
代理模式是一种应用非常广泛的设计模式,当客户端代码需要调用某个对象的时候,客户端并不关心是否可以准确的得到这个对象,他只要一个能够提供该功能的对象而已,此时我们就可以返回该对象的代理.总而言之,客户端 ...
- linkin大话设计模式--简单工厂
linkin大话设计模式--工厂方法 什么是工厂方法:将多个类对象交给工厂来生成的设计被称为简单工厂模式,个人认为主要是为了实现解耦,在代码重构的时候会很重要. 代码如下: public class ...
- 在一个程序设计里,不同的功能窗口有着相似的功能实现方式,可采用设计模式---模板方法模式
前段时间学习了一些设计模式,最近在做项目的过程中恰巧用到了其中的一个--模板方法模式.这个模式在程序设计过程中可是为小编省去了不少的代码量呢.具体是怎样应用的呢?接下来小编为您分享. 前面博文 ...
- 『设计模式』瑞幸咖啡还是星巴克,一杯下午茶让我明白 设计模式--模板方法模式(Template Method Pattern)
23种设计模式+额外常用设计模式汇总 (持续更新) 简介 Define the skeleton of an algorithm in an operation,deferring some step ...
- 『设计模式』设计模式--模板方法模式
23种设计模式+额外常用设计模式汇总 (持续更新) 简介 Define the skeleton of an algorithm in an operation,deferring some step ...
最新文章
- 用SVN下载编译Spring3.2.4源码导入eclipse
- 匹配行linux中grep命令的使用
- Chapter1-1_Speech_Recognition(Overview)
- java第十四次作业
- 浅析C#中new、override、virtual关键字的区别
- kodi教程 linux,Kodi 设置教程 | Homepage of Zhikun Zhang
- Apple ID 被盗用的 5 个征兆,遇到其中一种,建议赶快改密码
- 利用Vmware workstation安装MS-DOS使用Turbo C 2.0
- 查看JVisualVM查看信息
- python编程考试_Python编程练习(一)
- 长沙哪个服务器网速最快,5G网络湖南长沙首秀?网速是4G的60倍,快的让人无法想象...
- facenet 搭建人脸识别库
- ji计算机内存不足怎么回事,Win7提示内存不足的原因及应对措施
- 交叉编译ffmpeg:aac x264 x265
- C/C++文件操作之CStdioFile
- OpenCL专题04:ViennaCL与Eigen双剑合璧
- 数据库设计-逻辑设计
- Neat Reader安装使用
- 棱镜门事件的思考:黑客是什么,如何成为黑客?[更新:2015-05-09]
- Python 3.12 目标:还可以更快!