Skip to content

Commit b3293e0

Browse files
committed
完成命令模式
1 parent 4e50138 commit b3293e0

12 files changed

+803
-14
lines changed
18.7 KB
Loading
33.1 KB
Loading

command/lijunhuayc/readme.md

Lines changed: 513 additions & 14 deletions
Large diffs are not rendered by default.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.command;
2+
3+
/**
4+
* @Desc: 命令对象和接受者对象的组装类[客户角色]
5+
* ps:我这把类名定义成ClientRole更方便读者理解这只是命令模式中的一个客户角色,而不是我们常规意义上说的客户端
6+
* @author ljh
7+
* @date 2015-3-16 上午11:08:03
8+
*/
9+
public class ClientRole {
10+
11+
/**
12+
* @Description: 组装操作
13+
* @author (ljh) @date 2015-3-16 上午11:13:06
14+
* @return void
15+
*/
16+
public void assembleAction() {
17+
ReceiverRole receiverRole1 = new ReceiverRole();//创建一个命令接收者
18+
Command command1 = new ConcreteCommandImpl1(receiverRole1);//创建一个命令的具体实现对象,并指定命令接收者
19+
20+
ReceiverRole receiverRole2 = new ReceiverRole(new PeopleBean(9999, "神仙"));
21+
Command command2 = new ConcreteCommandImpl2(receiverRole2);
22+
23+
//PS:command1 修改people为(18, "西北狼")
24+
//PS:command2 修改people为(35, "lijunhuayc")
25+
26+
InvokerRole invokerRole = new InvokerRole();//创建一个命令调用者
27+
invokerRole.setCommand1(command1);//为调用者指定命令对象1
28+
invokerRole.setCommand2(command2);//为调用者指定命令对象2
29+
invokerRole.invoke(null);//发起调用命令请求
30+
31+
ReceiverRole receiverRole3 = new ReceiverRole(new PeopleBean(9999, "神仙"));
32+
Command command3 = new ConcreteCommandImpl3(receiverRole2);
33+
invokerRole.invoke(command3);//发起调用命令请求
34+
35+
}
36+
37+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.command;
2+
3+
/**
4+
* @Desc: 命令接口[命令角色]
5+
* ps:你也可以定义成abstract class类型 *_*
6+
* @author ljh
7+
* @date 2015-3-16 上午11:01:01
8+
*/
9+
public interface Command {
10+
public void execute();
11+
public void undo();
12+
public void redo();
13+
14+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.command;
2+
3+
/**
4+
* @Desc: 命令接口的实现类[具体命令角色]
5+
* @author ljh
6+
* @date 2015-3-16 上午11:04:51
7+
*/
8+
public class ConcreteCommandImpl1 implements Command{
9+
private ReceiverRole receiverRole1;
10+
11+
public ConcreteCommandImpl1(ReceiverRole receiverRole1) {
12+
this.receiverRole1 = receiverRole1;
13+
}
14+
15+
@Override
16+
public void execute() {
17+
/*
18+
* 可以加入命令排队等等,未执行的命令支持redo操作
19+
*/
20+
receiverRole1.opAction(18, "西北狼");//执行具体的命令操作
21+
}
22+
23+
@Override
24+
public void undo() {
25+
receiverRole1.rollBack();//执行具体的撤销回滚操作
26+
}
27+
28+
@Override
29+
public void redo() {
30+
//在命令执行前可以修改命令的执行
31+
}
32+
33+
34+
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.command;
2+
3+
/**
4+
* @Desc: 命令接口的实现类[具体命令角色]
5+
* @author ljh
6+
* @date 2015-3-16 上午11:04:51
7+
*/
8+
public class ConcreteCommandImpl2 implements Command{
9+
private ReceiverRole receiverRole1;
10+
11+
public ConcreteCommandImpl2(ReceiverRole receiverRole1) {
12+
this.receiverRole1 = receiverRole1;
13+
}
14+
15+
@Override
16+
public void execute() {
17+
/*
18+
* 可以加入命令排队等等,未执行的命令支持redo操作
19+
*/
20+
receiverRole1.opAction(35, "lijunhuayc");//执行具体的命令操作
21+
}
22+
23+
@Override
24+
public void undo() {
25+
receiverRole1.rollBack();//执行具体的撤销回滚操作
26+
}
27+
28+
@Override
29+
public void redo() {
30+
//在命令执行前可以修改命令的执行
31+
}
32+
33+
34+
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.command;
2+
3+
/**
4+
* @Desc: 命令接口的实现类[具体命令角色]
5+
* @author ljh
6+
* @date 2015-3-16 上午11:04:51
7+
*/
8+
public class ConcreteCommandImpl3 implements Command{
9+
private ReceiverRole receiverRole1;
10+
11+
public ConcreteCommandImpl3(ReceiverRole receiverRole1) {
12+
this.receiverRole1 = receiverRole1;
13+
}
14+
15+
@Override
16+
public void execute() {
17+
/*
18+
* 可以加入命令排队等等,未执行的命令支持redo操作
19+
*/
20+
receiverRole1.opAction(9999, "神仙");//执行具体的命令操作
21+
}
22+
23+
@Override
24+
public void undo() {
25+
receiverRole1.rollBack();//执行具体的撤销回滚操作
26+
}
27+
28+
@Override
29+
public void redo() {
30+
//在命令执行前可以修改命令的执行
31+
}
32+
33+
34+
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.command;
2+
3+
/**
4+
* @Desc: 命令调用[调用者角色]
5+
* ps:使用命令对象的入口,扶着调用命令对象执行请求
6+
* @author ljh
7+
* @date 2015-3-16 上午11:16:15
8+
*/
9+
public class InvokerRole {
10+
private Command command1;
11+
private Command command2;
12+
//持有多个命令对象[实际的情况也可能是一个命令对象的集合来保存命令对象]
13+
14+
public void setCommand1(Command command1) {
15+
this.command1 = command1;
16+
}
17+
public void setCommand2(Command command2) {
18+
this.command2 = command2;
19+
}
20+
21+
public void invoke(Command command) {
22+
//根据具体情况选择执行某些命令
23+
if(null != command){
24+
command.execute();
25+
return;
26+
}
27+
command1.execute();
28+
command2.execute();
29+
}
30+
31+
32+
33+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.command;
2+
3+
4+
public class MainTest {
5+
6+
public static void main(String[] args) {
7+
ClientRole client = new ClientRole();
8+
client.assembleAction();
9+
10+
}
11+
12+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.command;
2+
3+
/**
4+
* @Desc: 辅助类,作为接收者Receiver的成员,包含两个属性,用来观察命令的执行情况
5+
* @author ljh
6+
* @date 2015-3-16 上午11:29:11
7+
*/
8+
public class PeopleBean {
9+
private int age = -1; //年龄
10+
private String name = "NULL"; //姓名
11+
12+
public PeopleBean() {
13+
}
14+
public PeopleBean(int age, String name) {
15+
this.age = age;
16+
this.name = name;
17+
}
18+
19+
public void update(int age, String name) {
20+
this.age = age;
21+
this.name = name;
22+
}
23+
24+
/**
25+
* @return 返回一个PeopleBean的克隆对象
26+
*/
27+
protected PeopleBean clone(){
28+
return new PeopleBean(age, name);
29+
}
30+
31+
/**
32+
* @Description: 还原操作
33+
* @author (ljh) @date 2015-3-16 上午11:45:23
34+
* @param cache
35+
* @return void
36+
*/
37+
protected void undo(PeopleBean cache) {
38+
this.age = cache.age;
39+
this.name = cache.name;
40+
}
41+
42+
@Override
43+
public String toString() {
44+
return " 【姓名:" + name + "\t年龄:" + age + "】\n";
45+
}
46+
47+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.command;
2+
3+
/**
4+
* @Desc: 命令的具体执行类[接受者角色]
5+
* ps:命令接收者可以是任意的类,只要实现了命令要求实现的相应功能即可。
6+
* @author ljh
7+
* @date 2015-3-16 上午11:06:14
8+
*/
9+
public class ReceiverRole {
10+
private PeopleBean people;
11+
private PeopleBean peopleCache = new PeopleBean(); //具体命令操作的缓存栈,用于回滚。这里为了方便就用一个PeopleBean来代替[实际的使用情况可能是需要回滚多个命令,这里只回滚一次]
12+
13+
public ReceiverRole() {
14+
this.people = new PeopleBean(-1, "NULL");//初始化年龄为-1,姓名为NULL
15+
}
16+
17+
public ReceiverRole(PeopleBean people) {
18+
this.people = people;
19+
}
20+
21+
/**
22+
* @Description: 具体操作方法[修改年龄和姓名]
23+
* @author (ljh) @date 2015-3-16 上午11:07:32
24+
* @return void
25+
*/
26+
public void opAction(int age, String name) {
27+
peopleCache = people.clone();//缓存people
28+
people.update(age, name);
29+
System.out.println("执行命令后:"+people.toString());
30+
}
31+
32+
/**
33+
* @Description: 回滚操作,用于撤销opAction执行的改变
34+
* @author (ljh) @date 2015-3-16 上午11:34:41
35+
* @return void
36+
*/
37+
public void rollBack() {
38+
people.undo(peopleCache);
39+
System.out.println("命令回滚后:"+people.toString());
40+
}
41+
42+
}

0 commit comments

Comments
 (0)