Skip to content

Commit a45c5fe

Browse files
committed
🎉 command pattern
1 parent c6ef8c7 commit a45c5fe

File tree

14 files changed

+790
-0
lines changed

14 files changed

+790
-0
lines changed

design-patterns/src/main/java/pers/huangyuhui/command_pattern/README.md

Lines changed: 444 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package pers.huangyuhui.command_pattern.command_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 客户端测试类
6+
* @author: 黄宇辉
7+
* @date: 9/20/2019-9:11 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class Client {
12+
13+
public static void main(String[] args) {
14+
FunctionButton functionButton = new FunctionButton();
15+
Command login_key = new LoginInCommand();
16+
functionButton.setCommand(login_key);
17+
functionButton.click();
18+
19+
System.out.println("--------------------");
20+
21+
Command exit_key = new LoginOutCommand();
22+
functionButton.setCommand(exit_key);
23+
functionButton.click();
24+
}
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package pers.huangyuhui.command_pattern.command_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 抽象命令类
6+
* @author: 黄宇辉
7+
* @date: 9/20/2019-9:01 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public abstract class Command {
12+
public abstract void execute();
13+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package pers.huangyuhui.command_pattern.command_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 功能键类, 充当请求调用者(请求发送者)
6+
* @author: 黄宇辉
7+
* @date: 9/20/2019-8:59 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class FunctionButton {
12+
13+
private Command command;
14+
15+
//为功能键注入命令
16+
public void setCommand(Command command) {
17+
this.command = command;
18+
}
19+
20+
//发送请求的方法
21+
public void click() {
22+
command.execute();
23+
}
24+
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package pers.huangyuhui.command_pattern.command_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 登录命令类, 充当具体命令类
6+
* @author: 黄宇辉
7+
* @date: 9/20/2019-9:07 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class LoginInCommand extends Command {
12+
13+
private LoginInSystem enterSystem;
14+
15+
public LoginInCommand() {
16+
this.enterSystem = new LoginInSystem();
17+
}
18+
19+
//命令执行方法,将调用请求接收者的业务方法
20+
@Override
21+
public void execute() {
22+
enterSystem.login();
23+
}
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package pers.huangyuhui.command_pattern.command_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 登录系统的模拟实现类, 充当请求接收者
6+
* @author: 黄宇辉
7+
* @date: 9/20/2019-9:10 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class LoginInSystem {
12+
13+
public void login() {
14+
System.out.println("login in");
15+
}
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package pers.huangyuhui.command_pattern.command_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 退出命令类, 充当具体命令类
6+
* @author: 黄宇辉
7+
* @date: 9/20/2019-9:05 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class LoginOutCommand extends Command {
12+
13+
private LoginOutSystem exitSystem;
14+
15+
public LoginOutCommand() {
16+
this.exitSystem = new LoginOutSystem();
17+
}
18+
19+
//命令执行方法,将调用请求接受者的业务方法
20+
@Override
21+
public void execute() {
22+
exitSystem.exit();
23+
}
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package pers.huangyuhui.command_pattern.command_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 退出系统的模拟实现类, 充当请求接收者
6+
* @author: 黄宇辉
7+
* @date: 9/20/2019-9:08 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class LoginOutSystem {
12+
13+
public void exit() {
14+
System.out.println("login out");
15+
}
16+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package pers.huangyuhui.command_pattern.command_pattern_2;
2+
3+
import javax.swing.*;
4+
import java.awt.*;
5+
import java.awt.event.MouseEvent;
6+
import java.awt.event.MouseMotionAdapter;
7+
8+
/**
9+
* @project: design-patterns
10+
* @description: 客户端测试类
11+
* @author: 黄宇辉
12+
* @date: 9/20/2019-9:48 PM
13+
* @version: 1.0
14+
* @website: https://yubuntu0109.github.io/
15+
*/
16+
public class Client extends JFrame {
17+
18+
//绘制的历史记录
19+
private MacroCommand history = new MacroCommand();
20+
//绘制区域(设置画板大小)
21+
private DrawCanvas canvas = new DrawCanvas(600, 400, history);
22+
//删除按钮
23+
private JButton clearButton = new JButton("clear");
24+
25+
public Client(String title) {
26+
super(title);
27+
28+
/*
29+
鼠标拖动事件
30+
*/
31+
canvas.addMouseMotionListener(new MouseMotionAdapter() {
32+
@Override
33+
public void mouseDragged(MouseEvent e) {
34+
Command command = new DrawCommand(canvas, e.getPoint());
35+
history.append(command);
36+
command.execute();
37+
}
38+
});
39+
40+
/*
41+
动作监听事件
42+
*/
43+
clearButton.addActionListener(e -> {
44+
if (e.getSource() == clearButton) {
45+
history.clear();
46+
canvas.repaint();
47+
}
48+
});
49+
50+
/*
51+
JFrame面板配置
52+
*/
53+
setLayout(new BorderLayout());
54+
add(clearButton, BorderLayout.NORTH);
55+
add(canvas, BorderLayout.SOUTH);
56+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
57+
pack();
58+
setVisible(true);
59+
}
60+
61+
public static void main(String[] args) {
62+
new Client("a simple drawing board");
63+
}
64+
65+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package pers.huangyuhui.command_pattern.command_pattern_2;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 表示"命令"的接口,充当(抽象)命令
6+
* @author: 黄宇辉
7+
* @date: 9/20/2019-9:25 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public interface Command {
12+
void execute();
13+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy