自我成长之模板方法模式

模板方法的应用场景

该模式目前还没有在实际的开发场景中用到过,不过根据理解主要体现在四个字: 封装流程,具体的流程实现是在子类实现。感觉在一些类库中应该用的比较多,Android生命周期管理应该有涉及,没有深入的了解。

模板方法的示例

创建模板的抽象框架

主要用途是定义一系列的方法,并封装成固定的流程,抽象方法的具体的实现交由子类实现。

定义一个电脑类,定义一系列的方法,开机、装载系统、登录、注销、重启、关机等流程;电脑的重启流程封装为固定的关机、开机、装载系统、用户登录,方法定义为final 防

止子类改写固定的流程框架。

/*
 * Copyright 2017 bd_liang
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package com.study.Template;

/**
 * <pre>
 * 项目名称:ModeNote   
 * 类名称:Computer   
 * 类描述:  抽象类,封装固定的流程,开启电源、装载系统、用户验证、关机等 
 * 创建人:bd_liang   
 * 创建时间:Feb 21, 2017 10:07:42 AM   
 * 修改人:bd_liang   
 * 修改时间:Feb 21, 2017 10:07:42 AM   
 * 修改备注:
 * @version
 * 
 * <pre>
 */

public abstract class Computer {
	/**
	 * 按下开机键,开启电源
	 */
	protected void powerOn() {
		System.out.println("开启电源");
	}

	/**
	 * 系统加载
	 */
	protected void loadOS() {
		System.out.println("系统加载");
	}

	/**
	 * 进入用户验证界面
	 */
	protected void logIn() {
		System.out.println("用户登录界面");
	}

	/**
	 * 注销
	 */
	protected void writeOff() {
		System.out.println("注销用户");
	}

	/**
	 * 关机
	 */
	protected void powerOff() {
		System.out.println("关机");
	}

	/**
	 * 重启,定义为final 防止算法重写
	 */
	public final void reboot(String name) {
		System.out.println("-------------" + name + "系统重启::");
		powerOff();
		powerOn();
		loadOS();
		logIn();
		System.out.println("-------------" + name + "重启成功!\n");
	}
}

具体的实现类A:Windows电脑产品

重写loadOS()、logIn()方法,跟不同的系统实现不同的登录界面。

/*
 * Copyright 2017 bd_liang
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package com.study.Template;

/**
 * <pre>
 * 项目名称:ModeNote   
 * 类名称:WindowsComputer   
 * 类描述:   
 * 创建人:bd_liang   
 * 创建时间:Feb 21, 2017 10:27:35 AM   
 * 修改人:bd_liang   
 * 修改时间:Feb 21, 2017 10:27:35 AM   
 * 修改备注:
 * @version
 * 
 * <pre>
 */

public class WindowsComputer extends Computer {

	@Override
	protected void loadOS() {
		// TODO Auto-generated method stub
		System.out.println("加载Windows系统");
	}

	@Override
	protected void logIn() {
		// TODO Auto-generated method stub
		System.out.println("Windows界面登录");
	}

}

具体的产品类B:Ubuntu系统电脑

/*
 * Copyright 2017 bd_liang
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package com.study.Template;

/**
 * <pre>
 * 项目名称:ModeNote   
 * 类名称:PublicComputer   
 * 类描述:   
 * 创建人:bd_liang   
 * 创建时间:Feb 21, 2017 10:24:15 AM   
 * 修改人:bd_liang   
 * 修改时间:Feb 21, 2017 10:24:15 AM   
 * 修改备注:
 * @version
 * 
 * <pre>
 */

public class UbuntuComputer extends Computer {

	@Override
	protected void loadOS() {
		// TODO Auto-generated method stub
		System.out.println("加载Ubuntu系统");
	}

	@Override
	protected void logIn() {
		// TODO Auto-generated method stub
		System.out.println("Ubuntu命令行模式登录");
	}

}

测试代码:

/*
 * Copyright 2017 bd_liang
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package com.study.Template;

/**
 * <pre>
 * 项目名称:ModeNote   
 * 类名称:Test   
 * 类描述:   
 * 创建人:bd_liang   
 * 创建时间:Feb 21, 2017 10:28:46 AM   
 * 修改人:bd_liang   
 * 修改时间:Feb 21, 2017 10:28:46 AM   
 * 修改备注:
 * @version
 * 
 * <pre>
 */

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		UbuntuComputer ubuntuComputer = new UbuntuComputer();
		ubuntuComputer.reboot(UbuntuComputer.class.getSimpleName());

		WindowsComputer windowsComputer = new WindowsComputer();
		windowsComputer.reboot(WindowsComputer.class.getSimpleName());

	}

}



测试结果:
-------------UbuntuComputer系统重启::
关机
开启电源
加载Ubuntu系统
Ubuntu命令行模式登录
-------------UbuntuComputer重启成功!

-------------WindowsComputer系统重启::
关机
开启电源
加载Windows系统
Windows界面登录
-------------WindowsComputer重启成功!




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值