Skip to content

Commit e26217f

Browse files
committed
设计模式-状态模式,命令模式
1 parent 32deed8 commit e26217f

13 files changed

+246
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.design.command;
2+
3+
/**
4+
* 抽象命令
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/2 16:05
8+
*/
9+
public abstract class Command {
10+
11+
protected RadioReceived radioReceived = new RadioReceived();
12+
13+
public abstract void execute();
14+
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.design.command;
2+
3+
/**
4+
* 使用命令对象的入口
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/2 16:05
8+
*/
9+
public class Invoker {
10+
11+
private Command command;
12+
13+
public void setCommand(Command command) {
14+
this.command = command;
15+
}
16+
17+
public void action(){
18+
this.command.execute();
19+
}
20+
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.design.command;
2+
3+
/**
4+
* 命令模式
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/2 16:05
8+
*/
9+
public class Main {
10+
11+
public static void main(String[] args) {
12+
// 定义调用者
13+
Invoker invoker = new Invoker();
14+
// 创建命令,启动收音机
15+
Command command = new StartCommandImpl();
16+
// 调用者接受命令
17+
invoker.setCommand(command);
18+
// 执行命令
19+
invoker.action();
20+
// 换下个频道
21+
command = new NextCommandImpl();
22+
invoker.setCommand(command);
23+
invoker.action();
24+
// 关闭收音机
25+
command = new ShutdownCommandImpl();
26+
invoker.setCommand(command);
27+
invoker.action();
28+
}
29+
30+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.design.command;
2+
3+
/**
4+
* 切换频道具体命令
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/2 16:05
8+
*/
9+
public class NextCommandImpl extends Command {
10+
@Override
11+
public void execute() {
12+
super.radioReceived.next();
13+
}
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.design.command;
2+
3+
/**
4+
* 真正的命令执行对象
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/2 16:05
8+
*/
9+
public class RadioReceived {
10+
11+
public void start() {
12+
System.out.println("启动收音机");
13+
}
14+
15+
public void next() {
16+
System.out.println("切换频道");
17+
}
18+
19+
public void shutdown() {
20+
System.out.println("关闭收音机");
21+
}
22+
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.design.command;
2+
3+
/**
4+
* 关闭收音机具体命令
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/2 16:05
8+
*/
9+
public class ShutdownCommandImpl extends Command {
10+
@Override
11+
public void execute() {
12+
super.radioReceived.shutdown();
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.design.command;
2+
3+
/**
4+
* 启动收音机具体命令
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/2 16:05
8+
*/
9+
public class StartCommandImpl extends Command {
10+
@Override
11+
public void execute() {
12+
super.radioReceived.start();
13+
}
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.design.state;
2+
3+
/**
4+
* 状态模式 - 状态机的扭转
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/2 15:29
8+
*/
9+
public class Main {
10+
11+
public static void main(String[] args) {
12+
// 声明环境类
13+
OrderContext orderContext = new OrderContext();
14+
// 初始化状态
15+
OrderState orderState = new OrderCreateStateImpl();
16+
// OrderState orderState = new OrderPayStateImpl();
17+
orderState.handle(orderContext);
18+
// 状态机扭转
19+
orderContext.handle();
20+
orderContext.handle();
21+
orderContext.handle();
22+
orderContext.handle();
23+
orderContext.handle();
24+
}
25+
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.design.state;
2+
3+
/**
4+
* 订单环境类
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/2 15:12
8+
*/
9+
public class OrderContext {
10+
11+
private OrderState orderState;
12+
13+
/**
14+
* 处理方法
15+
*/
16+
public void handle() {
17+
orderState.handle(this);
18+
}
19+
20+
public void setOrderState(OrderState orderState) {
21+
this.orderState = orderState;
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.design.state;
2+
3+
/**
4+
* 订单下单具体状态
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/2 15:12
8+
*/
9+
public class OrderCreateStateImpl extends OrderState {
10+
11+
@Override
12+
public void handle(OrderContext orderContext) {
13+
System.out.println("下单");
14+
// 可以和享元模式组合,不需要每次都去new具体状态,可以创建一次后重复使用
15+
orderContext.setOrderState(new OrderPayStateImpl());
16+
}
17+
}

0 commit comments

Comments
 (0)