设计模式之模板模式
模板模式:
定义:父类定义流程,子类实现.
目的: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();}}