Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 270f6a8

Browse filesBrowse files
author
‘mxg133’
committed
2020-8-13
1 parent 98623cb commit 270f6a8
Copy full SHA for 270f6a8

File tree

Expand file treeCollapse file tree

12 files changed

+1179
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

12 files changed

+1179
-0
lines changed
Open diff view settings
Collapse file
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package _15command;
2+
3+
public class Client {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
8+
//ʹ���������ģʽ�����ͨ��ң�������Ե�ƵIJ���
9+
10+
//������ƵĶ���(������)
11+
LightReceiver lightReceiver = new LightReceiver();
12+
13+
//���������صĿ�������
14+
LightOnCommand lightOnCommand = new LightOnCommand(lightReceiver);
15+
LightOffCommand lightOffCommand = new LightOffCommand(lightReceiver);
16+
17+
//��Ҫһ��ң����
18+
RemoteController remoteController = new RemoteController();
19+
20+
//�����ǵ�ң������������, ���� no = 0 �ǵ�ƵĿ��͹صIJ���
21+
remoteController.setCommand(0, lightOnCommand, lightOffCommand);
22+
23+
System.out.println("--------���µƵĿ���ť-----------");
24+
remoteController.onButtonWasPushed(0);
25+
System.out.println("--------���µƵĹذ�ť-----------");
26+
remoteController.offButtonWasPushed(0);
27+
System.out.println("--------���³�����ť-----------");
28+
remoteController.undoButtonWasPushed();
29+
30+
31+
System.out.println("=========ʹ��ң�����������ӻ�==========");
32+
33+
TVReceiver tvReceiver = new TVReceiver();
34+
35+
TVOffCommand tvOffCommand = new TVOffCommand(tvReceiver);
36+
TVOnCommand tvOnCommand = new TVOnCommand(tvReceiver);
37+
38+
//�����ǵ�ң������������, ���� no = 1 �ǵ��ӻ��Ŀ��͹صIJ���
39+
remoteController.setCommand(1, tvOnCommand, tvOffCommand);
40+
41+
System.out.println("--------���µ��ӻ��Ŀ���ť-----------");
42+
remoteController.onButtonWasPushed(1);
43+
System.out.println("--------���µ��ӻ��Ĺذ�ť-----------");
44+
remoteController.offButtonWasPushed(1);
45+
System.out.println("--------���³�����ť-----------");
46+
remoteController.undoButtonWasPushed();
47+
48+
}
49+
50+
}
Collapse file
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package _15command;
2+
3+
4+
//��������ӿ�
5+
public interface Command {
6+
7+
//ִ�ж���(����)
8+
public void execute();
9+
//��������(����)
10+
public void undo();
11+
}
Collapse file
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package _15command;
2+
3+
public class LightOffCommand implements Command {
4+
5+
// �ۺ�LightReceiver
6+
7+
LightReceiver light;
8+
9+
// ������
10+
public LightOffCommand(LightReceiver light) {
11+
super();
12+
this.light = light;
13+
}
14+
15+
@Override
16+
public void execute() {
17+
// TODO Auto-generated method stub
18+
// ���ý����ߵķ���
19+
light.off();
20+
}
21+
22+
@Override
23+
public void undo() {
24+
// TODO Auto-generated method stub
25+
// ���ý����ߵķ���
26+
light.on();
27+
}
28+
}
Collapse file
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package _15command;
2+
3+
public class LightOnCommand implements Command {
4+
5+
//�ۺ�LightReceiver
6+
7+
LightReceiver light;
8+
9+
//������
10+
public LightOnCommand(LightReceiver light) {
11+
super();
12+
this.light = light;
13+
}
14+
15+
@Override
16+
public void execute() {
17+
// TODO Auto-generated method stub
18+
//���ý����ߵķ���
19+
light.on();
20+
}
21+
22+
23+
24+
@Override
25+
public void undo() {
26+
// TODO Auto-generated method stub
27+
//���ý����ߵķ���
28+
light.off();
29+
}
30+
31+
}
Collapse file
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package _15command;
2+
3+
public class LightReceiver {
4+
5+
public void on() {
6+
System.out.println(" ��ƴ���.. ");
7+
}
8+
9+
public void off() {
10+
System.out.println(" ��ƹر���.. ");
11+
}
12+
}
Collapse file
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package _15command;
2+
3+
/**
4+
* û���κ��������ִ��: ���ڳ�ʼ��ÿ����ť, �����ÿ�����ʱ������ʲô������
5+
* ��ʵ��������һ�����ģʽ, ����ʡ���Կ��ж�
6+
* @author Administrator
7+
*
8+
*/
9+
public class NoCommand implements Command {
10+
11+
@Override
12+
public void execute() {
13+
// TODO Auto-generated method stub
14+
15+
}
16+
17+
@Override
18+
public void undo() {
19+
// TODO Auto-generated method stub
20+
21+
}
22+
23+
}
Collapse file
+54Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package _15command;
2+
3+
public class RemoteController {
4+
5+
// �� ��ť����������
6+
Command[] onCommands;
7+
Command[] offCommands;
8+
9+
// ִ�г���������
10+
Command undoCommand;
11+
12+
// ����������ɶ԰�ť��ʼ��
13+
14+
public RemoteController() {
15+
16+
onCommands = new Command[5];
17+
offCommands = new Command[5];
18+
19+
for (int i = 0; i < 5; i++) {
20+
onCommands[i] = new NoCommand();
21+
offCommands[i] = new NoCommand();
22+
}
23+
}
24+
25+
// �����ǵİ�ť��������Ҫ������
26+
public void setCommand(int no, Command onCommand, Command offCommand) {
27+
onCommands[no] = onCommand;
28+
offCommands[no] = offCommand;
29+
}
30+
31+
// ���¿���ť
32+
public void onButtonWasPushed(int no) { // no 0
33+
// �ҵ��㰴�µĿ��İ�ť�� �����ö�Ӧ����
34+
onCommands[no].execute();
35+
// ��¼��εIJ��������ڳ���
36+
undoCommand = onCommands[no];
37+
38+
}
39+
40+
// ���¿���ť
41+
public void offButtonWasPushed(int no) { // no 0
42+
// �ҵ��㰴�µĹصİ�ť�� �����ö�Ӧ����
43+
offCommands[no].execute();
44+
// ��¼��εIJ��������ڳ���
45+
undoCommand = offCommands[no];
46+
47+
}
48+
49+
// ���³�����ť
50+
public void undoButtonWasPushed() {
51+
undoCommand.undo();
52+
}
53+
54+
}
Collapse file
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package _15command;
2+
3+
public class TVOffCommand implements Command {
4+
5+
// �ۺ�TVReceiver
6+
7+
TVReceiver tv;
8+
9+
// ������
10+
public TVOffCommand(TVReceiver tv) {
11+
super();
12+
this.tv = tv;
13+
}
14+
15+
@Override
16+
public void execute() {
17+
// TODO Auto-generated method stub
18+
// ���ý����ߵķ���
19+
tv.off();
20+
}
21+
22+
@Override
23+
public void undo() {
24+
// TODO Auto-generated method stub
25+
// ���ý����ߵķ���
26+
tv.on();
27+
}
28+
}
Collapse file
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package _15command;
2+
3+
public class TVOnCommand implements Command {
4+
5+
// �ۺ�TVReceiver
6+
7+
TVReceiver tv;
8+
9+
// ������
10+
public TVOnCommand(TVReceiver tv) {
11+
super();
12+
this.tv = tv;
13+
}
14+
15+
@Override
16+
public void execute() {
17+
// TODO Auto-generated method stub
18+
// ���ý����ߵķ���
19+
tv.on();
20+
}
21+
22+
@Override
23+
public void undo() {
24+
// TODO Auto-generated method stub
25+
// ���ý����ߵķ���
26+
tv.off();
27+
}
28+
}
Collapse file
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package _15command;
2+
3+
public class TVReceiver {
4+
5+
public void on() {
6+
System.out.println(" ���ӻ�����.. ");
7+
}
8+
9+
public void off() {
10+
System.out.println(" ���ӻ��ر���.. ");
11+
}
12+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.