模板方法模式
1.引出模板模式
1.豆浆制作问题
![image-20240610185411255](https://img-blog.csdnimg.cn/img_convert/dcc5748d4ff6a7b1794d6e68716fc7e7.png)
2.基本介绍
![image-20240610185358339](https://img-blog.csdnimg.cn/img_convert/d40cdeb4253a1314506807b2e3caba22.png)
3.原理类图
![image-20240610190510860](https://img-blog.csdnimg.cn/img_convert/7f97c039fb8c656e1910ab510e90a79e.png)
2.豆浆制作代码实现
1.类图
![image-20240610190959555](https://img-blog.csdnimg.cn/img_convert/d7bc08cfdb7c9f9ccdc36f45531117cb.png)
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.结果
![image-20240610192531544](https://img-blog.csdnimg.cn/img_convert/a9197452861d79cf8717311e7fb11a31.png)
3.钩子方法
1.基本介绍
![image-20240610192609996](https://img-blog.csdnimg.cn/img_convert/088ad45cf8c36a70bc82db3bcdc34d02.png)
2.代码实现
1.SoyaMilk.java 添加钩子方法,由子类决定是否要重写
![image-20240610193556772](https://img-blog.csdnimg.cn/img_convert/e414a013e38b368c6e44ace26e9fd093.png)
![image-20240610193603768](https://img-blog.csdnimg.cn/img_convert/a6d1ae24ade2e95ae7ab751b1e93c2fb.png)
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.结果
![image-20240610193704548](https://img-blog.csdnimg.cn/img_convert/f96ec5b363d369e2ad1241205c54b7ab.png)
4.模板方法在IOC中的应用
![](https://img-blog.csdnimg.cn/img_convert/996bbff097a4c9369402d462cee40f18.png)
命令模式
1.引出命令模式
1.智能生活项目
![image-20240611202059368](https://img-blog.csdnimg.cn/img_convert/d54391176f07d91fc22c0fb8bb180126.png)
2.命令模式基本介绍
![image-20240611202403568](https://img-blog.csdnimg.cn/img_convert/40ba7552548207ab82e0d792a4b2a7a8.png)
3.原理类图
![image-20240611203518430](https://img-blog.csdnimg.cn/img_convert/cd9a7b1fb2ac0975b54138b0aa50b6d6.png)
2.命令模式解决智能生活项目
1.类图
![image-20240611205740021](https://img-blog.csdnimg.cn/img_convert/22fddb9e54561c5702fa8b481d5f0714.png)
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();
}
}
![image-20240612193941032](https://img-blog.csdnimg.cn/img_convert/2013832b4c877f1d9ab00a23b9d2d893.png)
3.命令模式在JdbcTemplate的使用
![image-20240612194143664](https://img-blog.csdnimg.cn/img_convert/595a52c19ca7bd629262570c43a54da2.png)