首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

设计方式之模板模式

2012-08-25 
设计模式之模板模式模板模式:定义:父类定义流程,子类实现.目的:A:减少重复代码B:防止调用出错好处:将子类

设计模式之模板模式
       模板模式:
       定义:父类定义流程,子类实现.
       目的:A:减少重复代码
            B:防止调用出错
       好处:将子类里面公用的代码提取到父类里面进行共享.增加了代码的复用性.只需要子类实现里面的流程方法,这样为团队开发提供了方便,使代码更加清晰,增加了代码的安全性.
      生活中的例子:比如戴尔电脑,在很多个地方进行制造,那么最主要的厂家把基本的流程定义好,其他的厂家根据这个流程来实现就行了,可以增加他们自己有特色的组件.
      代码实现:

package com.mode;public abstract class DELLComputerFlow {public void init(){System.out.println("check start.........");initMainframe();initScreem();System.out.println("check .....");initMemory();}public abstract void initMemory();public abstract void initScreem();public abstract void initMainframe();}package com.mode;public class ChinaDELLComputerFlow extends DELLComputerFlow{@Overridepublic void initMemory() {System.out.println("ChinaDELLComputer.initMemory()");}@Overridepublic void initScreem() {System.out.println("ChinaDELLComputer.initScreem()");}@Overridepublic void initMainframe() {System.out.println("ChinaDELLComputer.initMainframe()");}}package com.mode;public class Client {public static void main(String[] args) {DELLComputerFlow dellComputerFlow = new ChinaDELLComputerFlow();dellComputerFlow.init();}}

热点排行