文章目录
模板方法模式 1.引出模板模式
2.豆浆制作代码实现 1.类图 2.SoyaMilk.java 豆浆的抽象类 3.PeanutSoyaMilk.java 花生豆浆 4.RedBeanSoyaMilk.java 红豆豆浆 5.Client.java 6.结果
3.钩子方法 1.基本介绍 2.代码实现 1.SoyaMilk.java 添加钩子方法,由子类决定是否要重写 2.NoSoyaMilk.java 新的子类,不加配料 3.Client.java 4.结果
4.模板方法在IOC中的应用
命令模式 1.引出命令模式
2.命令模式解决智能生活项目 1.类图 2.代码实现 1.LightReceiver.java 实现灯具体操作的类 2.Command.java 命令接口 3.LightOnCommand.java 开灯命令,构造器聚合LightReceiver,调用对象逻辑 4.LightOffCommand.java 关灯命令,构造器聚合LightReceiver,调用对象逻辑 5.NoCommand.java 空命令,即空执行,用于初始化每个按钮 6.RemoteController.java 相当于遥控器,聚合所有命令 7.Client.java 调用命令
3.命令模式在JdbcTemplate的使用
模板方法模式
1.引出模板模式
1.豆浆制作问题
2.基本介绍
3.原理类图
2.豆浆制作代码实现
1.类图
2.SoyaMilk.java 豆浆的抽象类
package com. sun ;
public abstract class SoyaMilk {
final void make ( ) {
select ( ) ;
add ( ) ;
beat ( ) ;
}
abstract void select ( ) ;
void add ( ) {
System . out. println ( "添加豆子" ) ;
}
void beat ( ) {
System . out. println ( "放到豆浆机去打碎" ) ;
}
}
3.PeanutSoyaMilk.java 花生豆浆
package com. sun ;
public class PeanutSoyaMilk extends SoyaMilk {
@Override
void select ( ) {
System . out. println ( "加入上好的花生豆" ) ;
}
}
4.RedBeanSoyaMilk.java 红豆豆浆
package com. sun ;
public class RedBeanSoyaMilk extends SoyaMilk {
@Override
void select ( ) {
System . out. println ( "加入上好的红豆" ) ;
}
}
5.Client.java
package com. sun ;
public class Client {
public static void main ( String [ ] args) {
System . out. println ( "制作红豆豆浆" ) ;
SoyaMilk soyaMilk = new RedBeanSoyaMilk ( ) ;
soyaMilk. make ( ) ;
System . out. println ( "制作花生豆浆" ) ;
SoyaMilk soyaMilk1 = new PeanutSoyaMilk ( ) ;
soyaMilk1. make ( ) ;
}
}
6.结果
3.钩子方法
1.基本介绍
2.代码实现
1.SoyaMilk.java 添加钩子方法,由子类决定是否要重写
2.NoSoyaMilk.java 新的子类,不加配料
package com. sun ;
public class NoSoyaMilk extends SoyaMilk {
@Override
void select ( ) {
System . out. println ( "选择豆子,不准备加配料" ) ;
}
@Override
boolean customerWantCondiments ( ) {
return false ;
}
}
3.Client.java
package com. sun ;
public class Client {
public static void main ( String [ ] args) {
System . out. println ( "制作红豆豆浆" ) ;
SoyaMilk soyaMilk = new RedBeanSoyaMilk ( ) ;
soyaMilk. make ( ) ;
System . out. println ( "制作花生豆浆" ) ;
SoyaMilk soyaMilk1 = new PeanutSoyaMilk ( ) ;
soyaMilk1. make ( ) ;
System . out. println ( "制作不加配料的豆浆" ) ;
SoyaMilk soyaMilk2 = new NoSoyaMilk ( ) ;
soyaMilk2. make ( ) ;
}
}
4.结果
4.模板方法在IOC中的应用
命令模式
1.引出命令模式
1.智能生活项目
2.命令模式基本介绍
3.原理类图
2.命令模式解决智能生活项目
1.类图
2.代码实现
1.LightReceiver.java 实现灯具体操作的类
package com. sun ;
public class LightReceiver {
public void on ( ) {
System . out. println ( "点灯打开了" ) ;
}
public void off ( ) {
System . out. println ( "电灯关闭了" ) ;
}
}
2.Command.java 命令接口
package com. sun ;
public interface Command {
void execute ( ) ;
void undo ( ) ;
}
3.LightOnCommand.java 开灯命令,构造器聚合LightReceiver,调用对象逻辑
package com. sun ;
public class LightOnCommand implements Command {
LightReceiver lightReceiver;
public LightOnCommand ( LightReceiver lightReceiver) {
this . lightReceiver = lightReceiver;
}
@Override
public void execute ( ) {
lightReceiver. on ( ) ;
}
@Override
public void undo ( ) {
lightReceiver. off ( ) ;
}
}
4.LightOffCommand.java 关灯命令,构造器聚合LightReceiver,调用对象逻辑
package com. sun ;
public class LightOffCommand implements Command {
LightReceiver lightReceiver;
public LightOffCommand ( LightReceiver lightReceiver) {
this . lightReceiver = lightReceiver;
}
@Override
public void execute ( ) {
lightReceiver. off ( ) ;
}
@Override
public void undo ( ) {
lightReceiver. on ( ) ;
}
}
5.NoCommand.java 空命令,即空执行,用于初始化每个按钮
package com. sun ;
public class NoCommand implements Command {
@Override
public void execute ( ) {
}
@Override
public void undo ( ) {
}
}
6.RemoteController.java 相当于遥控器,聚合所有命令
package com. sun ;
public class RemoteController {
Command [ ] onCommands;
Command [ ] offCommands;
Command undoCommand;
public RemoteController ( ) {
onCommands = new Command [ 5 ] ;
offCommands = new Command [ 5 ] ;
for ( int i = 0 ; i < 5 ; i++ ) {
onCommands[ i] = new NoCommand ( ) ;
offCommands[ i] = new NoCommand ( ) ;
}
}
public void setCommand ( int no, Command onCommand, Command offCommand) {
this . onCommands[ no] = onCommand;
this . offCommands[ no] = offCommand;
}
public void onButtonWasPushed ( int no) {
onCommands[ no] . execute ( ) ;
undoCommand = onCommands[ no] ;
}
public void offButtonWasPushed ( int no) {
offCommands[ no] . execute ( ) ;
undoCommand = offCommands[ no] ;
}
public void undoButtonWasPushed ( ) {
undoCommand. undo ( ) ;
}
}
7.Client.java 调用命令
package com. sun ;
public class Client {
public static void main ( String [ ] args) {
LightReceiver lightReceiver = new LightReceiver ( ) ;
LightOffCommand lightOffCommand = new LightOffCommand ( lightReceiver) ;
LightOnCommand lightOnCommand = new LightOnCommand ( lightReceiver) ;
RemoteController remoteController = new RemoteController ( ) ;
remoteController. setCommand ( 0 , lightOnCommand, lightOffCommand) ;
remoteController. onButtonWasPushed ( 0 ) ;
remoteController. offButtonWasPushed ( 0 ) ;
remoteController. undoButtonWasPushed ( ) ;
}
}
3.命令模式在JdbcTemplate的使用