Skip to content

Commit eb829c4

Browse files
author
‘mxg133’
committed
2020-8-12
1 parent 270f6a8 commit eb829c4

39 files changed

Lines changed: 1192 additions & 81 deletions

_1DesignPattern/src/_15command/Client.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,47 @@ public class Client {
44

55
public static void main(String[] args) {
66
// TODO Auto-generated method stub
7-
8-
//ʹ���������ģʽ�����ͨ��ң�������Ե�ƵIJ���
9-
10-
//������ƵĶ���(������)
7+
8+
//使用命令设计模式,完成通过遥控器,对电灯的操作
9+
10+
//创建电灯的对象(接受者)
1111
LightReceiver lightReceiver = new LightReceiver();
12-
13-
//���������صĿ�������
12+
13+
//创建电灯相关的开关命令
1414
LightOnCommand lightOnCommand = new LightOnCommand(lightReceiver);
1515
LightOffCommand lightOffCommand = new LightOffCommand(lightReceiver);
16-
17-
//��Ҫһ��ң����
16+
17+
//需要一个遥控器
1818
RemoteController remoteController = new RemoteController();
19-
20-
//�����ǵ�ң������������, ���� no = 0 �ǵ�ƵĿ��͹صIJ���
19+
20+
//给我们的遥控器设置命令, 比如 no = 0 是电灯的开和关的操作
2121
remoteController.setCommand(0, lightOnCommand, lightOffCommand);
22-
23-
System.out.println("--------���µƵĿ���ť-----------");
22+
23+
System.out.println("--------按下灯的开按钮-----------");
2424
remoteController.onButtonWasPushed(0);
25-
System.out.println("--------���µƵĹذ�ť-----------");
25+
System.out.println("--------按下灯的关按钮-----------");
2626
remoteController.offButtonWasPushed(0);
27-
System.out.println("--------���³�����ť-----------");
27+
System.out.println("--------按下撤销按钮-----------");
2828
remoteController.undoButtonWasPushed();
29-
30-
31-
System.out.println("=========ʹ��ң�����������ӻ�==========");
32-
29+
30+
31+
System.out.println("=========使用遥控器操作电视机==========");
32+
3333
TVReceiver tvReceiver = new TVReceiver();
34-
34+
3535
TVOffCommand tvOffCommand = new TVOffCommand(tvReceiver);
3636
TVOnCommand tvOnCommand = new TVOnCommand(tvReceiver);
37-
38-
//�����ǵ�ң������������, ���� no = 1 �ǵ��ӻ��Ŀ��͹صIJ���
37+
38+
//给我们的遥控器设置命令, 比如 no = 1 是电视机的开和关的操作
3939
remoteController.setCommand(1, tvOnCommand, tvOffCommand);
40-
41-
System.out.println("--------���µ��ӻ��Ŀ���ť-----------");
40+
41+
System.out.println("--------按下电视机的开按钮-----------");
4242
remoteController.onButtonWasPushed(1);
43-
System.out.println("--------���µ��ӻ��Ĺذ�ť-----------");
43+
System.out.println("--------按下电视机的关按钮-----------");
4444
remoteController.offButtonWasPushed(1);
45-
System.out.println("--------���³�����ť-----------");
45+
System.out.println("--------按下撤销按钮-----------");
4646
remoteController.undoButtonWasPushed();
4747

4848
}
4949

50-
}
50+
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package _15command;
22

3-
4-
//��������ӿ�
3+
//创建命令接口
54
public interface Command {
65

7-
//ִ�ж���(����)
6+
//执行动作(操作)
87
public void execute();
9-
//��������(����)
8+
//撤销动作(操作)
109
public void undo();
1110
}

_1DesignPattern/src/_15command/LightOffCommand.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22

33
public class LightOffCommand implements Command {
44

5-
// �ۺ�LightReceiver
5+
// 聚合LightReceiver
66

77
LightReceiver light;
88

9-
// ������
9+
// 构造器
1010
public LightOffCommand(LightReceiver light) {
11-
super();
12-
this.light = light;
13-
}
11+
super();
12+
this.light = light;
13+
}
1414

1515
@Override
1616
public void execute() {
1717
// TODO Auto-generated method stub
18-
// ���ý����ߵķ���
18+
// 调用接收者的方法
1919
light.off();
2020
}
2121

2222
@Override
2323
public void undo() {
2424
// TODO Auto-generated method stub
25-
// ���ý����ߵķ���
25+
// 调用接收者的方法
2626
light.on();
2727
}
28-
}
28+
}

_1DesignPattern/src/_15command/LightOnCommand.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@
22

33
public class LightOnCommand implements Command {
44

5-
//�ۺ�LightReceiver
6-
5+
//聚合LightReceiver
6+
77
LightReceiver light;
8-
9-
//������
8+
9+
//构造器
1010
public LightOnCommand(LightReceiver light) {
1111
super();
1212
this.light = light;
1313
}
14-
14+
1515
@Override
1616
public void execute() {
1717
// TODO Auto-generated method stub
18-
//���ý����ߵķ���
18+
//调用接收者的方法
1919
light.on();
2020
}
2121

22-
22+
2323

2424
@Override
2525
public void undo() {
2626
// TODO Auto-generated method stub
27-
//���ý����ߵķ���
27+
//调用接收者的方法
2828
light.off();
2929
}
3030

31-
}
31+
}

_1DesignPattern/src/_15command/LightReceiver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
public class LightReceiver {
44

55
public void on() {
6-
System.out.println(" ��ƴ���.. ");
6+
System.out.println(" 电灯打开了.. ");
77
}
8-
8+
99
public void off() {
10-
System.out.println(" ��ƹر���.. ");
10+
System.out.println(" 电灯关闭了.. ");
1111
}
1212
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package _15command;
22

33
/**
4-
* û���κ��������ִ��: ���ڳ�ʼ��ÿ����ť, �����ÿ�����ʱ������ʲô������
5-
* ��ʵ��������һ�����ģʽ, ����ʡ���Կ��ж�
4+
* 没有任何命令,即空执行: 用于初始化每个按钮, 当调用空命令时,对象什么都不做
5+
* 其实,这样是一种设计模式, 可以省掉对空判断
66
* @author Administrator
77
*
88
*/
@@ -11,13 +11,13 @@ public class NoCommand implements Command {
1111
@Override
1212
public void execute() {
1313
// TODO Auto-generated method stub
14-
14+
1515
}
1616

1717
@Override
1818
public void undo() {
1919
// TODO Auto-generated method stub
20-
20+
2121
}
2222

2323
}

_1DesignPattern/src/_15command/RemoteController.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
public class RemoteController {
44

5-
// �� ��ť����������
5+
// 开 按钮的命令数组
66
Command[] onCommands;
77
Command[] offCommands;
88

9-
// ִ�г���������
9+
// 执行撤销的命令
1010
Command undoCommand;
1111

12-
// ����������ɶ԰�ť��ʼ��
12+
// 构造器,完成对按钮初始化
1313

1414
public RemoteController() {
1515

@@ -22,33 +22,33 @@ public RemoteController() {
2222
}
2323
}
2424

25-
// �����ǵİ�ť��������Ҫ������
25+
// 给我们的按钮设置你需要的命令
2626
public void setCommand(int no, Command onCommand, Command offCommand) {
2727
onCommands[no] = onCommand;
2828
offCommands[no] = offCommand;
2929
}
3030

31-
// ���¿���ť
31+
// 按下开按钮
3232
public void onButtonWasPushed(int no) { // no 0
33-
// �ҵ��㰴�µĿ��İ�ť�� �����ö�Ӧ����
33+
// 找到你按下的开的按钮, 并调用对应方法
3434
onCommands[no].execute();
35-
// ��¼��εIJ��������ڳ���
35+
// 记录这次的操作,用于撤销
3636
undoCommand = onCommands[no];
3737

3838
}
3939

40-
// ���¿���ť
40+
// 按下开按钮
4141
public void offButtonWasPushed(int no) { // no 0
42-
// �ҵ��㰴�µĹصİ�ť�� �����ö�Ӧ����
42+
// 找到你按下的关的按钮, 并调用对应方法
4343
offCommands[no].execute();
44-
// ��¼��εIJ��������ڳ���
44+
// 记录这次的操作,用于撤销
4545
undoCommand = offCommands[no];
4646

4747
}
48-
49-
// ���³�����ť
48+
49+
// 按下撤销按钮
5050
public void undoButtonWasPushed() {
5151
undoCommand.undo();
5252
}
5353

54-
}
54+
}

_1DesignPattern/src/_15command/TVOffCommand.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
public class TVOffCommand implements Command {
44

5-
// �ۺ�TVReceiver
5+
// 聚合TVReceiver
66

77
TVReceiver tv;
88

9-
// ������
9+
// 构造器
1010
public TVOffCommand(TVReceiver tv) {
1111
super();
1212
this.tv = tv;
@@ -15,14 +15,14 @@ public TVOffCommand(TVReceiver tv) {
1515
@Override
1616
public void execute() {
1717
// TODO Auto-generated method stub
18-
// ���ý����ߵķ���
18+
// 调用接收者的方法
1919
tv.off();
2020
}
2121

2222
@Override
2323
public void undo() {
2424
// TODO Auto-generated method stub
25-
// ���ý����ߵķ���
25+
// 调用接收者的方法
2626
tv.on();
2727
}
28-
}
28+
}

_1DesignPattern/src/_15command/TVOnCommand.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
public class TVOnCommand implements Command {
44

5-
// �ۺ�TVReceiver
5+
// 聚合TVReceiver
66

77
TVReceiver tv;
88

9-
// ������
9+
// 构造器
1010
public TVOnCommand(TVReceiver tv) {
1111
super();
1212
this.tv = tv;
@@ -15,14 +15,14 @@ public TVOnCommand(TVReceiver tv) {
1515
@Override
1616
public void execute() {
1717
// TODO Auto-generated method stub
18-
// ���ý����ߵķ���
18+
// 调用接收者的方法
1919
tv.on();
2020
}
2121

2222
@Override
2323
public void undo() {
2424
// TODO Auto-generated method stub
25-
// ���ý����ߵķ���
25+
// 调用接收者的方法
2626
tv.off();
2727
}
28-
}
28+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package _15command;
22

33
public class TVReceiver {
4-
4+
55
public void on() {
6-
System.out.println(" ���ӻ�����.. ");
6+
System.out.println(" 电视机打开了.. ");
77
}
8-
8+
99
public void off() {
10-
System.out.println(" ���ӻ��ر���.. ");
10+
System.out.println(" 电视机关闭了.. ");
1111
}
1212
}

0 commit comments

Comments
 (0)